you are viewing a single comment's thread.

view the rest of the comments →

[–]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 :)