Skip to content

Instantly share code, notes, and snippets.

@quant61
Last active May 26, 2019 18:06
Show Gist options
  • Save quant61/24bb90113b7be52a0658c89ebdb15e75 to your computer and use it in GitHub Desktop.
Save quant61/24bb90113b7be52a0658c89ebdb15e75 to your computer and use it in GitHub Desktop.
classes.php
<?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);
}
}
}
}
<?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