Software Engineer at InVision. Full-Stack JavaScript dev, with passion for front-end development.
Psytrance DJ on weekends, playing in local clubs (yeah, that means: Goa Parties!) :D
This kind of function is a higer order function. Higher order functions are functions, that take a function as parameter or return a function.
functioncompose(g,f){returnfunction(x){// returns a function, that expects one parameterreturng(f(x));}}
compose is a higher order function in both ways: It takes functions (g and f) as parameters and returns a function as result. The returned function is a function, that expects takes a parameter (x) and returns the result of calling g with the result of calling f with x.
Your second question:
constcompose=(...fns)=>x=>
Yes, this is an array of functions but as parameter spread. Spreading a parameter gives you a so called variadic function. For example:
functionexample(...args){return`I was called with ${args.length} args: ${args.join(", ")}.`;}console.log(example("foo","bar"));// -> "I was called with 2 args: foo, bar."console.log(example("a","b","c"));// -> "I was called with 3 args: a, b, c."console.log(example("a",2,"c"));// -> "I was called with 3 args: a, 2, c."console.log(example("a",2,"c","d"));// -> "I was called with 4 args: a, 2, c, d."
That was a complicated read ...
I guess I need to study more about javascript before I come back to this article.
How do you call those kind of function?
Where is x coming from?
Also about this line.
what is fns?
Is it an array of functions?
This kind of function is a higer order function. Higher order functions are functions, that take a function as parameter or return a function.
compose
is a higher order function in both ways: It takes functions (g
andf
) as parameters and returns a function as result. The returned function is a function, that expects takes a parameter (x
) and returns the result of callingg
with the result of callingf
withx
.Your second question:
Yes, this is an array of functions but as parameter spread. Spreading a parameter gives you a so called variadic function. For example:
raaa ... now that you say it, I see the
return function(x)
while when I posted my comment I read it as below in my head ...
Sorry about that!
And also, thanks for the detailed explanation! I will go back to your article!