3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php function powerSet(Iterator $iterator){ if(!$iterator->valid()){ return yield new EmptyIterator(); } $one = $iterator->current(); $rest = new NoRewindIterator($iterator); $rest->next(); foreach(powerSet($rest) as $set){ $set = iterator_to_array($set); yield new ArrayIterator($set); yield (function($set, $one){ yield from $set; yield $one; })($set, $one); } } function baz(){ yield 1; yield 2; yield 3; yield 4; } $sets = powerSet(baz()); foreach($sets as $i => $set){ echo $i+1 . " {"; $arr = iterator_to_array($set); echo implode(", ", $arr); echo "}\n"; }
based on tBY4O
Output for 7.0.0 - 7.3.1
1 {} 2 {1} 3 {2} 4 {1} 5 {3} 6 {1} 7 {2} 8 {1} 9 {4} 10 {1} 11 {2} 12 {1} 13 {3} 14 {1} 15 {2} 16 {1}
Output for 5.5.0 - 5.6.28
Parse error: syntax error, unexpected 'new' (T_NEW) in /in/sbaou on line 5
Process exited with code 255.