Skip to content

Instantly share code, notes, and snippets.

@asm89 asm89/lambda.php
Last active Aug 29, 2015

Embed
What would you like to do?
<?php
mb_internal_encoding("UTF-8");
function λ($fn) {
list($args, $body) = explode('=>', $fn, 2);
$args = trim($args);
$args = ltrim($args, '(');
$args = rtrim($args, ')');
$body = 'return ' . $body . ';';
return create_function($args, $body);
}
$xs = [1, 2, 3];
$ys = array_map(λ('$x => $x * 2'), $xs);
print_r($ys);
$adder = λ('($x, $y) => $x + $y');
var_dump($adder(42, 1337));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.