Last active
May 26, 2019 18:06
-
-
Save quant61/24bb90113b7be52a0658c89ebdb15e75 to your computer and use it in GitHub Desktop.
classes.php
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 | |
class CartesianIterator implements IteratorAggregate { | |
private $first; | |
private $second; | |
private $add; | |
public function __construct(iterable $first, iterable $second, callable $add) { | |
$this->first = $first; | |
$this->second = $second; | |
$this->add = $add; | |
} | |
protected function add(){ | |
return call_user_func_array($this->add, func_get_args()); | |
} | |
public function getIterator() { | |
foreach($this->first as $a){ | |
foreach($this->second as $b){ | |
yield $this->add($a, $b); | |
} | |
} | |
} | |
} |
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 | |
class SumIterator implements IteratorAggregate { | |
private $iterables = []; | |
public function __construct(...$iterables) { | |
$this->iterables = $iterables; | |
} | |
public function append(iterable $it) | |
{ | |
$this->iterables[] = $it; | |
} | |
public function getIterator() { | |
foreach($this->iterables as $it){ | |
foreach($it as $value){ | |
yield $value; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment