1

In Javascript there is reduce function which takes function and array, map over the array and return whatever the function return.

For example:

[1, 2, 3].reduce(function(acc, x) {
  acc += x 
  return acc;
}, 0); // 6 

In Haskell there is fold which for me do the same:

foldl (+) 0 [1,2,3]  -> 6

If i want to create that kind of function as library is it safe to call it fold instead of reduce and is there any difference between the two.

Does both functions the same except the name or there is some difference

I demonstrate with different languages because Js doesnt have foldl function.

2 Answers 2

6

Naming is inconsistent, and depends on the language.

In some contexts like Kotlin, reduce doesn't take an initial value, but fold does. In Haskell, there's foldl and foldl1 to differentiate between the two kinds. In Clojure, the same function reduce has different overloads for taking an initial value, or not taking one.

They're basically describing the same concept, and I've never found any clear difference between the two names.

Sign up to request clarification or add additional context in comments.

6 Comments

small clarification: if fold can be replaced with recursion why it exists - i suggest only for simpler syntax and shorter code?
@user2693928 Reductions are a generalization of a common recursion pattern. You use reduce so you don't need to use recursion directly.
Yes, but in Haskell there are no for loops - they are replaced with recursion. Why reduce is not replaced with recursion as for loops, too
@user2693928 Sorry, I'm not sure what you're asking. Are you asking why reduce isn't implemented with a for-loop in languages that have for-loops?
I wonder why in Haskell for example they added fold since every operation with fold can be done with recursion. Just like they dont put for loops in Haskell why they add fold. For example sum of list can be implemented with recursion.
|
1

Another perspective would be: reduce returns a scalar value while fold returns a function:

function dyadic(x, y) {
    return x + y;
};

initialValue = 0;

a_list = [1, 2, 3];

// --------- REDUCE -----------------------------
// reduce returns a scalar value:
console.log(a_list.reduce(dyadic, initialValue));

// --------- FOLD -------------------------------
// fold returns a polyadic function:
function fold(d_func, initialValue) {
    return function (...list) {
        return list.reduce(d_func, initialValue);
    };
};

// using fold to build the polyadic function build_sum:
build_sum = fold(dyadic, initialValue);
// finally obtain the scalar value by calling built function:
console.log(build_sum(...a_list));



// or using arrow notation:
dyadic2 = (x, y) => x + y;
fold2 = (d_func, initialValue) => (...list) => list.reduce(d_func, initialValue);
build_sum2 = fold2(dyadic2, initialValue);
console.log(build_sum2(...a_list));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.