Published in

Nerd For Tech

# Functional Programming in PHP (Part 2) — Currying

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 cmyc(a)(b)(c)     =>     a b cmyc(a, b)(c)     =>     a b cmyc(a)(b, c)     =>     a b cmyca(b, c)       =>     a b cmyca(b)(c)       =>     a b cmycab(c)         =>     a b cmycabc           =>     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);`

# 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!

--

--

## More from Nerd For Tech

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 software engineer focusing on the fun part of computing.