Vin Souza
Created on:
October 8, 2018

Λ (λ) - From Half-a-Life, to PHP

From Wikipedia:

is the 11th letter of the Greek alphabet. In the system of Greek numerals lambda has a value of 30. 

From Half-life wikia:

In the original Half-Life, Gordon Freeman’s trademark HEV Suit was marked with a Lambda Logo on the chest, as were other HEV Suits. A section of Black Mesa dedicated to teleport research was also called the “Lambda Complex”, and is identifiable by the Lambda logo at its entrance.

But what does this symbol has to do with PHP?

Well, it’s the name we at computer programming gives to anonymous functions, which, again with the definitions, from Wikipedia:

In computer programming, an anonymous function (also function literal or lambda abstraction) is a function”) definition that is not bound to an identifier”). Anonymous functions are often:

1 - arguments being passed to higher-order functions, or 2 - used for constructing the result of a higher-order function that needs to return a function.

We all know that computer languages has functions, and if it’s a OOP language, has classes, interfaces, packages, and the list goes on. But with all these organizations, how can we make simple functions, that are used only in a piece of the code? Rapid functions, rapid development (ain’t that what the web is all about? Rapid, rapid, rapid)

The anonymous functions came to help. Imagine the situation (i’m gonna give the example in PHP, but you would port it to any language you imagine):

I have an template, and this template uses a function, which gets an array, and transforms it in a HTML select, this function, runs this array and use the key as the HTML option value, and the value, as the HTML option text. (I’m talking about html_options from Smarty here), but my original array, which cames from the database is a multidimensional array, like this one here:

Array( Array( ‘databaseId’ => 1, ‘databaseValue’ => ‘Hello’) )

How could I transform this into:

Array(1 => ‘Hello’)

You could make something like this:

https://gist.github.com/tryvin/430e0e74a50d8b8bd03b

But we could revamp this a while, and use array_map, it takes a PHP callback as first argument, and an array as the second (and so on) argument. Then this function starts executing the callback for every element in the input arrays. But how could this use lambda functions? Well, as we all know, PHP callbacks are functions, whatever they are from a class, a main function, or even a LAMBDA! See the code above, using lambda, and see it became more readable:

https://gist.github.com/tryvin/8c00e95e2775ed4ad757

Yes, I know, it’s the same “size”, but now imagine, you have some processing to do. You need to insert some filters into your array, change some values, execute something while it’s mapping, the uses are endless. And this is only one use of lambda with array_map. Lamdbas can be used for anything, it’s a function for god sake.

Say yes to lamdba, and no to creating __functionToMapping, or __mySecretFunctionWhichOnlyAddUserCalls and so on!

Oh, and don’t forget, even if it’s a pretty oldie game, you could give Half-life a try, even if playing it in a big screen gives me headaches =/