This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Assuming https://wiki.php.net/rfc/arrow_functions makes it into PHP core, we can do this: | |
class Arr extends ArrayObject | |
{ | |
static function make($array) | |
{ | |
return new self($array); | |
} | |
function map($func) | |
{ | |
$res = new self(); | |
foreach ($this as $k => $v) | |
$res[$k] = $func($k, $v); | |
return $res; | |
} | |
function filter($func) | |
{ | |
$res = new self(); | |
foreach ($this as $k => $v) | |
if ($func($k, $v)) | |
$res[$k] = $v; | |
return $res; | |
} | |
} | |
$nums = Arr::make([10, 20, 30, 40]) | |
return $nums->filter( fn($v) => $v > 15 )->map( fn($v) => $v * 2 ); | |
// Additionally, also there's this: | |
$x = rand(3,6); | |
return $nums->filter( fn($v) => $v > 15 )->map( fn($v) => $v * 2 + $x ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment