Earlier quoted context omitted.
From my comment below: "Thank you for this! I've been learning JS for React and .reduce, .every and .some are new to me! Super helpful" I feel like learning is always easier in smaller bitesizes and the person wrote something very clear and digestible. Sounds like it wasn't aimed at more advanced programmers like yourself
The blog post has tremendous value to the right audience, no doubt about that. But it's worse than worthless for its actual target audience because the article makes those people feel like they're supposed to get it, and they probably feel shitty when they don't (I did). Even though they shouldn't. No one expects you to read that blog post and get it if you've never used Map or Reduce before. It's misleading and dish…
Javascript Arrays and Functional Programming
61–70 of 92 posts
Re: Javascript Arrays and Functional Programming
#62Some more practical examples // Grab unique [1,1,2,3,4].filter( ( item, index, array ) => array.indexOf( item ) === index ) // => [1,2,3,4] // Flatten [[1,2],[3,4]].reduce( ( result, item ) => result.concat(item), [] ); // => [1,2,3,4] Not sure why the author missed `sort`. // Sort [1,2,4,3].sort( ( a, b ) => a - b )
The first is slow (O(n²) where O(n) will do), and the second is very likely to be slow (probably the same).
[1, 1, 2, 3, 4].filter(
(obj => x => {
if (obj[x]) {
return false;
}
obj[x] = true;
return true;
})({}),
);
What's happening is that you're passing an object (hashmap / hashset) into a function that returns a filtering function, and that object is used inside the closure to track the dupes. It's still a pure function because even though you're mutating the passed in object, the filtering function is still deterministic and referentially transparent.Re: Javascript Arrays and Functional Programming
#63Some more practical examples // Grab unique [1,1,2,3,4].filter( ( item, index, array ) => array.indexOf( item ) === index ) // => [1,2,3,4] // Flatten [[1,2],[3,4]].reduce( ( result, item ) => result.concat(item), [] ); // => [1,2,3,4] Not sure why the author missed `sort`. // Sort [1,2,4,3].sort( ( a, b ) => a - b )
// Grab unique
[...new Set([1,1,2,3,4])]Re: Javascript Arrays and Functional Programming
#64Some more practical examples // Grab unique [1,1,2,3,4].filter( ( item, index, array ) => array.indexOf( item ) === index ) // => [1,2,3,4] // Flatten [[1,2],[3,4]].reduce( ( result, item ) => result.concat(item), [] ); // => [1,2,3,4] Not sure why the author missed `sort`. // Sort [1,2,4,3].sort( ( a, b ) => a - b )
For unique, I prefer to let javascript do the work: // Grab unique [...new Set([1,1,2,3,4])]
[...new Set([{x: 1}, {x: 1}])]Re: Javascript Arrays and Functional Programming
#65Earlier quoted context omitted.
For unique, I prefer to let javascript do the work: // Grab unique [...new Set([1,1,2,3,4])]
Although it wouldn't work if you were using objects and wanted to do a deep comparison e.g. [...new Set([{x: 1}, {x: 1}])]
Re: Javascript Arrays and Functional Programming
#66Earlier quoted context omitted.
For unique, I prefer to let javascript do the work: // Grab unique [...new Set([1,1,2,3,4])]
Although it wouldn't work if you were using objects and wanted to do a deep comparison e.g. [...new Set([{x: 1}, {x: 1}])]
Javascript is great but sometimes I miss the predicability of the STL.
Re: Javascript Arrays and Functional Programming
#67I accept that this is a widely used phrase now but I never understood why just using map/filter/reduce and avoiding state is enough for code to be called "functional programming". Most functional languages feature pattern matching, algebraic data types, purity, currying, strong typing, type inference, recursion instead of loops and types that cannot be null. It's a completely different style of coding. Adding map/fil…
Robust programs can be written in many languages, including 'many languages' in the sense of heterogeneity as demonstrated when a Haskell program communicates over the internet hopping through routers and switches running embedded and iOT and OS quality C code...and worse.
Functional programming techniques are great, until something else is needed. At some point databases become useful. At some point caches become important. At some point everyone winds up needing random numbers. Relying on statistics and probability isn't a bad idea either, right Siri and Cortana and Alexa?
Re: Javascript Arrays and Functional Programming
#68Also read about using flow over chain
Re: Javascript Arrays and Functional Programming
#69JavaScript has an advantage on Python in this style, because Python lambdas are hideously ugly and JS anonymous functions are less hideously ugly. In Python it tends to be cleaner (and sometimes faster) to write list comprehensions instead of map() and filter(), especially since Python also has lazy generator comprehensions.
Python actually discourages using map anf filter. Which is kind of weird to me but whatever.
[func(x) if x in collection if cond(x)]
makes a single pass over the data and gives you a list, but list(map(func, filter(cond, collection)))
makes two passes and requires some extra function calls.I think the real reason is perceived legibility on the part of the Python core devs. I seem to recall a document from a while ago in which GvR himself came out in favor of list comprehension style. "Explicit is better than implicit".
Re: Javascript Arrays and Functional Programming
#70In his example Array.prototype.reduce, isn't this almost the same as just doing a foreach loop? I don't understand why doing this, it's pretty much the same number of lines.
In this example it is, but a nice thing about using map, reduce etc is that you can chain them together, ex: let out = ["a", "b", "c", 1, 2, 3] // Turn letters to upper case .map(i => { return typeof i === "string" ? i.toUpperCase() : i; }) // Add one to numbers .map(i => { return typeof i === "number" ? i + 1 : i; }) // split letters and numbers up .reduce((all, i) => { typeof i === "string" ? all.letters.push(i) :…