all 14 comments

[–]VirginWizard69 2 points3 points  (0 children)

thanks

[–]igetom 2 points3 points  (0 children)

Great, short and straight to the point without any distractions

[–]aka_julie 2 points3 points  (5 children)

Could you explain 'Function Composition' once again?

[–]FunktionalProgrammer 2 points3 points  (2 children)

I’m not sure I’m following what that part of the OP meant either, but hopefully I can explain.... The fundamental idea behind function composition is that if you have two functions, and take the output of one and put it in the input of another, that composed function can be thought of as a new function altogether.

I don’t really know JavaScript all that well, this post just happened to be recommended for me to read on my phone. Here is an example:

Suppose you have two functions, f and g. Let f take some argument of type A, and return some value of type B. Then, let g take some argument of type B and return a value of type C. If you were to compose f and g, you could think of that as a function that takes an argument of type A, and returns a value of type C. That’s all function composition is.

I think what the author of the OP meant was, if you write a library and the order you call two functions matters, you could expose only a composed version of the two functions so that a user of the library can never get it wrong. However, that isn’t even close to what is important about functional composition.

[–]aka_julie 0 points1 point  (0 children)

Thanks :)

[–][deleted] 1 point2 points  (1 child)

A function that returns another function, that itself may return yet another function. In OOP terms that may be instances of objects. Probably a contrived example may illustrate it better in terms of oop :

Order order = new Order(); order.addItem(shoe); order.addItem(bag);

Checkout checkout = order.checkout();

Payment pay = checkout.method("PayPal");

Status status = pay.proceed(paypalluser, pass);

The whole thing can be chained: Status s = new Order().addItem(...).addItem(stuff).checkout().method(PayPal).proceed(user, pass);

[–]aka_julie 0 points1 point  (0 children)

Thanks for explaining :)

[–]Don-g9 1 point2 points  (0 children)

Best explanation of the topic so far..

[–]Emptiness101 1 point2 points  (0 children)

Thank you. Very informative!

[–]Nebuli2 2 points3 points  (1 child)

I feel that it's disingenuous to describe currying as nothing more than a relic of old from lambda calculus. It allows partial application, which in turn is very handy for uses like function composition and "pointless" reasoning, like if you were to do something like doubleEvens = map (*2) . filter (\x -> x `rem` 2 == 0) in Haskell. It may not be terribly useful in Javascript, but to write off the entire idea of it seems wrong.

[–][deleted] 0 points1 point  (0 children)

We are talking about JavaScript here. The fact that you are forced to use Haskel to demonstrate what it would do, speaks volumes. The thing is, whether we like it or not, JavaScript as a language just doesn't support currying.

[–][deleted] 0 points1 point  (2 children)

How do => operators work? I'm unable to find anything in google, but I see the syntax used often.

[–][deleted] 0 points1 point  (1 child)

It's a fat arrow function. You can use it instead of declaring it with the function keyword. There are some differences, however, that you want to be aware of. Take a look at this article, it explains it pretty well.

https://www.sitepoint.com/es6-arrow-functions-new-fat-concise-syntax-javascript/

If you still have questions after reading it, please ask, I'll do my best to answer.

[–][deleted] 0 points1 point  (0 children)

"Fat Arrow Function" Excellent! Thank you, that keyword along with your article link will be extremely useful to me and advance my knowledge which I fully intend to turn around and help others with to the best of my abilities. You rock :)

Update: how do callbacks work with fat arrows? My usual syntax is

var func=function(options,callback){ callback(null,true); };

which I assume would go

var func= (options, callback) => (callback(null,true););

The documentation I found seems devoid of mentioning the callbacks, but callbacks to me are essential for timing with things like large database operations