Nerd For Tech
Published in

Nerd For Tech

Functional Programming in PHP (Part 2) — Currying

Photo by Sharon McCutcheon on Unsplash

Spicing your code up

Although it sounds fancy, currying has actually nothing to do with your favorite turmeric mix, but derives its name from the American mathematician Haskell Curry (you will definitely remember this name). Nevertheless, we will see that currying provides nothing less than spicing our code up.

<input_type> → <output_type>
<input_type_1>, <input_type_2>, <input_type_3> → <output_type>
<input_type> → (<input_type_2> → (<input_type_3> → <output_type>))
<input_type_1>, ...<input_type_n> → <output_type>
<input_type_1> → (... → (<input_type_n> → <output_type>))
<input_type_1> → ... → <input_type_n> → <output_type>

“a boa constrictor digesting an elephant”

Just like the snake on the famous drawing, our functions are also capable to swallow and digest almost anything if we let them do so. That means that they can accept a variable amount of arguments, and we don’t need to specify how many of them. It can be zero, one or more.

function variadic(...$arguments) {}

Cooking with curry

Our first attempt to use our curry function (which you either already have implemented, or will use my implementation that I provide later) may look something like the following:

myc(a, b, c)     =>     a b c
myc(a)(b)(c) => a b c
myc(a, b)(c) => a b c
myc(a)(b, c) => a b c
myca(b, c) => a b c
myca(b)(c) => a b c
mycab(c) => a b c
mycabc => a b c
curry($myfunc)('a', 'b', 'c')
curry($myfunc, 'a', 'b', 'c'); // 1.
curry($myfunc, 'a')('b', 'c'); // 2.
curry($myfunc, 'a', 'b', 'c')();
curry(callable $fn)

Serving dishes

One does not simply take pictures on food if also wants to stimulate appetite. It turns out that serving plays a crucial role in that. How the food is placed on a dish, how it’s lit, what kind of camera is used, etc.

function immutable(): string { return 'totally immutable'; }
function immutableFn(): callable { return fn() => 'yeah'; }
Immutable greeting: Good day, Tülin!
Good day, Tülin!
Hello, Tülin!
greet()(formal(), tulin())
greetFormally()(tulin())
greet(formal, tulin)
greetFormally(tulin)
let callable greet = fn(callable $form, string $name): string => $form() . ', ' . $name . '!';let callable formal = fn(): string => "Good day";
let callable informal = fn(): string => "Hello";
let callable greetFormally = curry(greet)(formal);
let callable greetInformally = curry(greet)(informal);
let string tulin = "Tülin";let string girl = tulin;// reassignment does not work:
// let string girl = "Somebody else";
greet(formal, tulin);
greetFormally(tulin);
greetInformally(girl);
Here’s a beautiful Bavarian scenery to help you prepare for the mental load — courtesy of the author

Implementing curry in PHP

There are several ways to implement currying in a language, and also several ways to implement it in PHP. For now you might have your own version, which you checked against the usage examples. If it worked well, congratulations, you did it right!

--

--

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store