Created
April 5, 2016 16:12
-
-
Save akostylev0/0cbcfcbf48ddd224a6dec3f560e2c979 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
function map($collection) : \Generator | |
{ | |
if (is_array($collection) || $collection instanceof \Traversable) | |
{ | |
foreach($collection as $element) { | |
yield $element; | |
} | |
} | |
throw new \InvalidArgumentException(); | |
} | |
function reduce($collection, callable $callback, $initializeValue = null) | |
{ | |
if (is_array($collection) || $collection instanceof \Traversable) | |
{ | |
foreach($collection as $element) { | |
$initializeValue = $callback($element, $initializeValue); | |
} | |
return $initializeValue; | |
} | |
throw new \InvalidArgumentException(); | |
} | |
function filter($collection, callable $callback) : \Generator | |
{ | |
if (is_array($collection) || $collection instanceof \Traversable) | |
{ | |
foreach($collection as $element) { | |
if ($callback($element)) { | |
yield $element; | |
} | |
} | |
} | |
throw new \InvalidArgumentException(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment