marijnh / Eloquent-JavaScript

The sources for the Eloquent JavaScript book
https://eloquentjavascript.net
3.01k stars 793 forks source link

Update 05_higher_order.md #471

Closed thesnabe closed 5 years ago

thesnabe commented 5 years ago

Hello Marijn,

First of all I really enjoy reading you book so far. It's very detailed and up to date. I'd recommend this to everyone to read.

However I've found an incorrect explanation inside the Higher-order functions chapter about a 'pure function', here is the anchor of the paragraph https://eloquentjavascript.net/05_higher_order.html#h_MM7RF32uzF .

You're saying that the filter function is pure function (which is correct) but you're also saying that it's pure because it returns a new list instead of mutating the given list. This is not what a pure function is. A pure function is a function that always gives the same output given a certain input. It doesn't rely on globals or other side effects that have happened outside the function. You could instead perhaps write something about immutability which is more related to what you're describing.

I've added a commit which removes the pure function reference, so if you want you can use it ;)

Thanks again for writing this book and I will probably enjoy reading the rest of it!

odus13 commented 5 years ago

Well, from your definition, the function is a pure function, because the filter function “Always gives the same output given a certain input. Also doesn’t rely on globals or other side effects...”

So about your “incorrect explanation” there is a period(.) present between sentences.

Look closely. (I’ll increase the periods)

This function is pure....... It does not modify the array it is given.

He’s saying the function is pure, and also just adding that this function does not modify the array it is given, which is also correct. He isn’t necessarily “incorrectly” defining pure functions as functions which do not modify variables.

Even if he did, he would be justified, as making a function pure by not allowing it to modify variables.

This in fact is a good practice in writing ‘eloquent’ programs.

marijnh commented 5 years ago

Thanks for your suggestion.

This is not what a pure function is.

I don't see how the text can be construed as claiming that that's what a pure function is, though.

Pure functions are described more precisely in chapter 3. This is just a reference to that.

thesnabe commented 5 years ago

The way it's writting let's me think that you're saying. That it is a pure function because you're not adjusting the given array. Which is not what makes it pure. Yes obviously it is a pure function, but not for that reason.

odus13 commented 5 years ago

The author has explicitly resolved this. It is just a reference. Again, There is a period present.

For Clear-thinking;

Think of it as an Object , with two different properties or methods in it.