Skip to content

Instantly share code, notes, and snippets.

@akostylev0
Created April 5, 2016 16:12
Show Gist options
  • Save akostylev0/0cbcfcbf48ddd224a6dec3f560e2c979 to your computer and use it in GitHub Desktop.
Save akostylev0/0cbcfcbf48ddd224a6dec3f560e2c979 to your computer and use it in GitHub Desktop.
<?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