1

Just wondering to know , why initialValue is given to functional programming methods, such as to reduce()? What is specific reason for having it this way?

Instead of giving it to the bottom of the function, it is also possible to have it as the first element of the array, as it behaves the same, means same. Trying to learn functional programming now, but this point doesn't make sense.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Tiyor
  • 119
  • 1
  • 1
  • 8
  • If I want to sum the ages of the three bears (`[5, 42, 69]`) via a `reduce` function, how do I do so without providing an initial value? – Oliver Charlesworth Dec 19 '17 at 18:45
  • I think it's given *only* to `reduce`. What other "functional programming methods" do you know that take this parameter? – Bergi Dec 19 '17 at 18:53
  • @OliverCharlesworth, You are summing three ages in your array, you have initial value already which is 5 [0]. Why do you need another initial value? – Tiyor Dec 19 '17 at 18:59
  • 1
    And what if the array is actually `[Bear(5), Bear(42), Bear(69)]`? How does the initial value get extracted? – Oliver Charlesworth Dec 19 '17 at 19:00
  • The very basic reason is the need for the result to be of different type than the array items. For istance if you want to group an array of integers as odd and even then you better start with an initial value of `[[],[]]` or `{ odds: [], evens: [] }`. – Redu Apr 01 '18 at 17:37

3 Answers3

3

it is also possible to have it as the first element of the array, as it behaves the same, means same.

Well, no. reduce is a generic function and supposed to work with arbitrary arrays, not just those that have our expected value as the first element of the array. The most important case would be empty arrays here.

Yes, it would be possible to push (unshift) the initial value as the first element of the array, then call reduce without the initial value, then remove it again from the array and you'd have the same result. But this is cumbersome, and might not always work (e.g. with immutable arrays). Also the accumulator might not even have the same type as the array elements.

Just think of the implementation of reduce, and the pattern which it is supposed abstract:

function reduce(callback, iterable) {
    let val = ???;
    for (const element of iterable)
        val = callback(val, element);
    return val;
}

Taking the ??? as a parameter is the sensible choice.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
1

The main reason for this is in the need to correctly handle zero-length data structures.

For multiplication, good choice is 1, for addition it's 0. For some data types it's empty string, unit matrix and so on. Those are zeros (or units) of algebraic structures with binary operations.

With this arrangement, there are fewer exceptions. For analogy, why do we need to use signed integers? It's there so we can express subtraction more naturally, without exceptions when 5 - 7 operations occurs.

Most of the functional programming is deeply rooted in mathematical (algebraic) structures. Knowledge of those really helps and simplify finding beautiful and generic solutions.

Also this question and answers may be of interest: Difference between fold and reduce? .

Roman Susi
  • 4,135
  • 2
  • 32
  • 47
0

If the array is empty and no initialValue is provided, TypeError will be thrown.

MDN web docs

LShapz
  • 1,738
  • 11
  • 19
  • it looks like we are both don't understand this thing. :) This is not answer for my question – Tiyor Dec 19 '17 at 19:01