Argument Adapters
The shape of a function is a description of the number and kind of things you pass into a function and the number and kind of things that come out of it.
A function that takes a single value in and returns a single value is called a unary function.
A function that takes in 2 inputs is called a binary function.
Finally, any function that takes more than 2 inputs is called an n-ary function.
The shape of a function matters in functional programming because it affects how functions fit together. Just like lego pieces, you want your functions to have similar shapes, so they can be attached together smoothly.
Pro tip: Functional programmers prefer unary functions the most and binary functions second most.
Shape Adapters
It's possible to create a higher-order function that adapts n-ary functions into unary or binary functions.
Flip and Reverse Adapters
Sometimes you need to flip arguments, so you can create a higher-order adapter function to do that:
A more extreme version of a flip adapter would be a reverse adapter, where you reverse the order of every argument:
Spread Adapter
Sometimes you want to change the shape of a function where it can take an array of values for all its arguments. In functional programming, that is known as apply.
Note: The inverse, unapply, accepts multiple arguments and passes them as an array to the underlying function.
Last updated