Skip to content

Instantly share code, notes, and snippets.

@naderman
Created January 17, 2014 16:21
Show Gist options
  • Save naderman/8476251 to your computer and use it in GitHub Desktop.
Save naderman/8476251 to your computer and use it in GitHub Desktop.
Pipe - useful or not?
<?php
require __DIR__.'/../vendor/autoload.php';
use EasyBib\Pipe;
use iter\fn;
use React\Partial;
$leapCounter = new Pipe\Pipe(array(
Partial\bind('iter\map', fn\method('format', array('L'))),
Partial\bind('iter\reduce', function ($acc, $v) {
if (!isset($acc['leap'])) {
$acc = array('non leap' => 0, 'leap' => 0);
}
return array(
'non leap' => $acc['non leap'] + !$v,
'leap' => $acc['leap'] + $v
);
}),
));
$result = $leapCounter(new \DatePeriod(new \DateTime('1900-01-01'), new \DateInterval('P1Y'), new \DateTime()));
foreach ($result as $type => $num) {
echo "There have been $num $type years since 1900.\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment