Build Complex Functions with Function Composition in JavaScript

InstructorKyle Shevlin

Share this video with your friends

Send Tweet

Composing functions is fundamental to functional programming. Luckily, you probably have already done some functional composition in high school or college math class. Understanding the compose() function will enable you to easily build complex functionality in your applications and programs.

Farzad YZ
~ 7 years ago

I personally used to avoid using compose function in utils such as Redux. but thanks to your simple and concise explanation about it, I can now use it confidently :)

Eric Gu (@ericguuu)
~ 7 years ago

Was there any reason for using reduceRight instead of reduce and going naturally from left to right?

Kyle Shevlininstructor
~ 7 years ago

Yes, traditionally compose uses reduceRight because arguments are supplied in the same order that you would read them if you were to write them out and nest them. Notice that the words of compose(foo, bar, baz)(x) have the same order as foo(bar(baz(x))).

I think we should be careful with the use of the word "natural" here. To the mathematician, compose would be "natural", as it is derived from mathematical composition. Now, JavaScript is a left-to-right read language and we should admit inherent bias/preference for left-to-right functionality while recognizing that does not make it "natural". It is a cultural bias brought to the language. JavaScript (or any programming language) could have just as easily been written right-to-left and be considered equally "natural".

Lastly, if you prefer left-to-right function composition, it can be accomplished by swapping reduceRight with reduce and renaming the function pipe. This is the traditional name for left-to-right composition and is what the potential pipeline operator |> is based on.

Daniel Ram
~ 6 years ago

Can you make a series on similar topics? :)

Kyle Shevlininstructor
~ 6 years ago

Sure, do you have any similar topics you know you would like covered? I'm working on a few about factories and factory composition right now, but I could put some work in on a series if that would be helpful.

jorge martinez
~ 6 years ago

what does 'acc' stands for? thx

Farzad YZ
~ 6 years ago

@jorge accumulator.

Teddy Odhiambo
~ 6 years ago

hi @Kyle, love your course on "Data Structures and Algorithms in JavaScript". Great stuff! Could you please do a course on FP? From basics to using lodash FP? Something along the lines of this composition lesson.

Thanks in advance

Kyle Shevlininstructor
~ 6 years ago

Hey Teddy, lucky for you, I already have an intro to functional programming course made. It's in the course queue for egghead, but courses outside of this promotion are pretty backed up and I expect it won't come out until somewhere between January-March of next year.

I am considering selling it on my own website for a small price as well, so it might be available to you sooner. I have some other options as well, but it's coming. Hang tight. Thanks!

Teddy Odhiambo
~ 6 years ago

Thanks Kyle. I'll definitely keep an eye out for that.