Combining Currying and Composition
Learn how to use currying and composition together for functions that take multiple arguments.
We'll cover the following
Function with one parameter value
Composing is easy when we have a scenario like this:
- Function
ONE
accepts a parameter of typeA
and returns a value of typeB
. - Function
TWO
accepts a parameter of typeB
and returns a value of typeC
. - Function
THREE
accepts a parameter of typeC
and returns a value of typeD
.
Note how function TWO
needs a value that function ONE
can provide, while function THREE
needs a value produced by function TWO
. This means that these functions can be composed. We run function ONE
with the value of type A
, which returns a value of type B
. Next up is function TWO
, which receives the value of type B
and returns a value of type C
. This value of type C
is passed to function THREE
. Finally, this gives us our result, a value of type D
.
Note: In a
Haskell
orF#
type of notation, the signatures of the functions above will be as follows:ONE :: A -> B TWO :: B -> C THREE :: C -> D COMBINED :: A -> D
Function with multiple parameter values
We saw that if the output of one function matches the input of the other, we can combine them. However, what if function TWO
needs multiple arguments? Let’s look at an example.
Get hands-on with 1400+ tech skills courses.