Skip to content

Instantly share code, notes, and snippets.

@asm89
Last active August 29, 2015 13:57
Show Gist options
  • Save asm89/09ea6eef7e5dbbdf7a6a to your computer and use it in GitHub Desktop.
Save asm89/09ea6eef7e5dbbdf7a6a to your computer and use it in GitHub Desktop.
<?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