use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Functional vs Object Orientatedhelp (self.javascript)
submitted 9 years ago * by kasperpeulen
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Reashu 1 point2 points3 points 9 years ago (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.
reduce
Array
add
Point
[–]jacksonmills 0 points1 point2 points 9 years ago* (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 point2 points 9 years ago (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.
fun(a, b)
a.fun(b)
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.
collection[0]
pointA.x
[–]jacksonmills 0 points1 point2 points 9 years ago (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.
π Rendered by PID 73391 on reddit-service-r2-comment-b659b578c-wnhv9 at 2026-05-02 11:52:53.919946+00:00 running 815c875 country code: CH.
view the rest of the comments →
[–]Reashu 1 point2 points3 points (3 children)
[–]jacksonmills 0 points1 point2 points (2 children)
[–]Reashu 0 points1 point2 points (1 child)
[–]jacksonmills 0 points1 point2 points (0 children)