Answers for "functools.reduce"

4

reduce in python

'''try this'''
from functools import reduce
a = [1,2,3,4]

SUM = reduce(lambda n,n2:n+n2,a)

print(SUM)
Posted by: Guest on October-15-2020
43

javascript reduce

var array = [36, 25, 6, 15];

array.reduce(function(accumulator, currentValue) {
  return accumulator + currentValue;
}, 0); // 36 + 25 + 6 + 15 = 82
Posted by: Guest on May-21-2020
1

js reduce

// syntax: array.reduce(function, accumulator-initial-value)
let array = [3, 7, 2, 9, 5]
const result = array.reduce((accumulator, currentValue, currentIndex, arr) => {
  // code
}, initialValue)

// accumulator = will store return value of the function
// currentValue = iterate through each array element
// currentIndex = index of currentValue
// arr = original array
// initialValue = set accumulator initial value
Posted by: Guest on July-21-2021
2

reduce()

const array1 = [1, 2, 3, 4];
const reducer = (accumulator, currentValue) => accumulator + currentValue;

// 1 + 2 + 3 + 4
console.log(array1.reduce(reducer));
// expected output: 10

// 5 + 1 + 2 + 3 + 4
console.log(array1.reduce(reducer, 5));
// expected output: 15
Posted by: Guest on January-17-2021
3

syntax of reduce in js

[1,2,3,4,5].reduce((acc, current)=>acc+current, 0)
Posted by: Guest on May-15-2020
3

reduce javascript

/* this is our initial value i.e. the starting point*/
const initialValue = 0;

/* numbers array */
const numbers = [5, 10, 15];

/* reducer method that takes in the accumulator and next item */
const reducer = (accumulator, item) => {
  return accumulator + item;
};

/* we give the reduce method our reducer function
  and our initial value */
const total = numbers.reduce(reducer, initialValue)
Posted by: Guest on August-12-2020

Python Answers by Framework

Browse Popular Code Answers by Language