Currying Functions for Animals (and a singing robot)

Currying is a Functional Programming technique that can improve your code readability, reduce it’s length, diminish bugs but most importantly, known for causing you to fall in love with your code often.

Why Currying?

Suppose we have this data structure:

And suppose we must use selectAnimal() if we want to peek from the data structure, this can be reasonable if the structure is not exposed to us, or it is much more complex or large…

How can we take care of every animal in the zoo?
A procedural way to code it would be:

But this code has many disadvantages and duplicated segments that are not reused. Imagine what would happen if the list of animals was much longer. Of course we could loop over each type, but still the word zoo, for instance would appear several times. One could argue that zoo could have been a global variable for the function selectAnimal() but I disagree because I’d rather keep selectAnimal() a Pure Function for many reasons.

Notice the “salmon reptile” bug that is hiding like a needle inside a haystack in the code above.

Currying could help us solving these issues.

What is a Currying Function?

A Currying Function is converting a function with multiple arguments to a series of functions each accepts 1 argument, where each wraps the other. For example
(x, y, z) => z + y + z curried, would have been written like this:
x => y => z => x + y + z.
When you call it and send x, you get a function that accepts y, when y is sent, you get a function that accepts z, when z is sent then the answer returns.

How to Curry?

Lets use some Wrapper Functions on selectAnimal() and create a Currying Function:

or simply:

The entity above can be used in several ways:

Lets use this Currying Function to refactor the care taking code into something nice and less verbose.

Notice that the “salmon reptile” bug is less likely to happen in this code.

If we wanted to increase the reusabillity (degree of code reuse), a functional way to do so would be to create a high-order function like:

A high-order function is simply a function that either accepts a function as a parameter, or return a function or both.

The function takeCareMulti() is designed to take care of multiple animals, it accepts a typeSelector function, a typeCare function, and a list of animal names. Needless to say, It takes care on each animal.

Usage:

Notice the improvement from where we started. It is almost as if a robot is singing it. One could easily wrap each function to output its action and it would look like:

Summary

Use currying functions to increase code readability, reusabillity and your affection for the code. It might also decrease the length of your code and the chance to make a mistake.

where was that tiger all along?

namer

Out beyond ideas of wrongdoing
and rightdoing there is a field.
I’ll meet you there.

Rumi

ripple

Liked the article?

Subscribe

Leave a Reply

Your email address will not be published. Required fields are marked *