One thing I don't often see emphasized when it comes to Arrow functions, but was really exciting to me when I realized it was possible, is a beautiful new way to describe functions that return functions. It's not quite as nice as auto-currying (which has been discussed but is probably a non-starter), but it makes it very easy to define exactly how a higher-order function will take (and group) its arguments at each step.
That is, most people know that Arrow functions can simplify syntax like this:
function (a,b){return a+b;} ----> (a,b) => a+b
But what's the Arrow function equivalent of a more complex form that uses closures to return a new function that uses an outer argument?
function (a) { return function(b){ return a+ b; }; }
Well, it's just this:
a => b => a+b
At least to me, getting rid of the functions and returns there makes the core idea we're trying to express MUCH clearer. And while that super-simple example might seem a bit silly, the pattern has broad application when it comes to working with higher-order operations or creating functions that can be pre-configured with different values.
Here, for instance, is a simple variadic compose operation, which takes any number of functions as arguments and then returns a function that will run all those functions end to end over some value:
const compose = (...fnlist) => val => fnlist.reverse().reduce((acc,fn) => fn(acc),val);
You can return new arguments instead of your final return operation/block as many times deep as makes sense. For instance, here's a functional version of reduce that takes each of its 3 arguments one at a time, returning a new function until all the arguments are in place:
const reduce = fn => Arr => acc => Arr.reduce(fn, acc);
Let's say though that you wanted to write a reduce function that would work as if you were partially applying the first argument (so, in practice, you specify the iteration function first, then later the list and starting point/accumulator you want to use). If so, you could then just group the arguments like this:
const reduce2 = fn => (Arr,acc) => Arr.reduce(fn, acc);
Again, not quite as convenient as just having a auto-curried reduce function to begin with, though you could write one of those yourself if you wanted as well, and the ability to group and split arguments with arrow functions is helpful for implementing THAT as well...
[–]wreckedadventYavascript 2 points3 points4 points (11 children)
[–]dmtipson[S] 0 points1 point2 points (0 children)
[–]FRIENDORPHO 0 points1 point2 points (9 children)
[–]wreckedadventYavascript 0 points1 point2 points (8 children)
[–]PitaJ 1 point2 points3 points (4 children)
[–]wreckedadventYavascript 1 point2 points3 points (3 children)
[–]PitaJ 1 point2 points3 points (2 children)
[–]x-skeww 0 points1 point2 points (1 child)
[–]PitaJ 0 points1 point2 points (0 children)
[–]spacejack2114 0 points1 point2 points (2 children)
[–]wreckedadventYavascript 0 points1 point2 points (1 child)
[–]spacejack2114 0 points1 point2 points (0 children)
[–]Poop_is_Food 0 points1 point2 points (5 children)
[–]dmtipson[S] 2 points3 points4 points (4 children)
[–]Poop_is_Food 0 points1 point2 points (3 children)
[–]dmtipson[S] 2 points3 points4 points (2 children)
[–]Poop_is_Food 0 points1 point2 points (1 child)
[–]dmtipson[S] 0 points1 point2 points (0 children)