The minimumSize
function is a curried function.
Currying is a way to split a function call into multiple and sequential subfunction calls.
There are some many good advantages to curry function, one is that it allows your function to be more composable, by deferring the real data source.
Let's depict the usage of:
n => email => email.text.size >= n
We can first call this function by passing a parameter for n
only:
minimumSize(2) // partially applies the minimumSize function with 2 as n
You will get at this time:
val nextFunction = email => email.text.size >= 2
Next you call nextFunction
with an email:
nextFunction(Email("anemail@domain.com"))
You will get at this time a boolean:
val bool = Email("anemail@domain.com").text.size >= 2
So if we sum up:
We started with an Int
, then an Email
, then a Boolean
:
Int => Email => Boolean
And by looking at this signature more carefully, you will recognize the EmailFilter
signature.
Let's substitute:
Int => EmailFilter
The idea is to make the EmailFilter
acts as a template, that you can parameterize with some higher functions.
Here we parameterized the email text size comparison so that we can keep the EmailFilter
generic.
Keep in mind that functional programming is all about composing functions.