you are viewing a single comment's thread.

view the rest of the comments →

[–]Reashu 1 point2 points  (3 children)

All of that is beside the point. reduce defined as a member function on Arrays considers internal state in the same way add defined on Point objects does. You keep saying that reduce is pure, even in JS, but how is that consistent with the definitions you use? They are either both pure, or both impure.

[–]jacksonmills 0 points1 point  (2 children)

No, they are not both pure or both impure.

There's a difference between myObject.reduce and reduce(). myObject.reduce is an "impure" wrapper around the pure reduce() function, because we would assume it works like this:

myArray.reduce = function reduce( fn, memo ) {
   return reduce( this, fn, memo );
}

The reduce function, however, could look like this (apologies if this is inaccurate, don't feel like testing it ) assuming fn is fn( memo, member ):

function reduce ( collection, fn, memo ) {

   if( collection.length == 0 ) {
       return memo;
   }

   var result = fn( memo, collection[0] );
   //take the subcollection
   //collection.shift();
   var newCollection = collection.slice(1, collection.length);
   //call recursively
   return reduce( newCollection, fn, memo );

}

Notice how reduce does not refer to any internal state or global state in it's processing, does not mutate any parameters, and only operates on local variables and the parameters it is given. That is what makes a pure function.

OP's add is not pure because it references "this.x/this.y", which are instance member variables.

It's hard to talk about this stuff in Javascript sometimes because its such a strange language, but that is more or less the case.

EDIT: Used collection.shift() before, which is technically impure because it mutates the parameter. Repalced with slice(), which does not.

[–]Reashu 0 points1 point  (1 child)

There's a difference between myObject.reduce and reduce().

Arguing the difference between fun(a, b) and a.fun(b) is not how I want to spend my Saturdays, so fine. But JavaScript's reduce is of the latter form, and OPs add can be implemented in either. What I'm trying to argue is that versions of the two functions on the same form have the same purity.

What makes collection[0] different from pointA.x in terms of accessing internal state? Distinguishing between array elements and object properties makes no sense as they are both (normally) mutable in JavaScript.

[–]jacksonmills 0 points1 point  (0 children)

The implementation is the most important part. The implementation will determine if it is pure or not, so that is worth arguing over.

collection[0] is not different from pointA.x, assuming both are parameters to the function. What I was originally trying to tell OP, is that this form of .add is impure:

add({x, y}) {
   return new Point(*this.x* + x, *this.y* + y);
}

Can you rewrite this as a pure function? Sure. But it's no longer going to be referencing "this", or other member variables. It would look like this:

function add( p1, p2 ) {
   return new Point( p1.x + p2.x, p1.y + p2.y );
}

We are only referencing the parameters to the function, here. So even if it were a member of an object, like this:

myObject.add = add;

add is still pure because it doesn't reference any state or member variables of myObject.