Last active
December 26, 2015 11:49
-
-
Save kaja47/7146882 to your computer and use it in GitHub Desktop.
yield examples
This file contains 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 | |
function countChange($amount) { | |
return cc($amount, 5); | |
} | |
function cc($amount, $kindOfCoins) { | |
if ($amount === 0) return 1; | |
if ($amount < 0) return 0; | |
if ($kindOfCoins === 0) return 0; | |
return cc($amount, $kindOfCoins - 1) + cc($amount - firstDenomination($kindOfCoins), $kindOfCoins); | |
} | |
function firstDenomination($kindOfCoins) { | |
if ($kindOfCoins === 1) return 1; | |
if ($kindOfCoins === 2) return 5; | |
if ($kindOfCoins === 3) return 10; | |
if ($kindOfCoins === 4) return 25; | |
if ($kindOfCoins === 5) return 50; | |
} | |
var_dump(countChange(100)); |
This file contains 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 | |
function countChange($amount) { | |
return cc($amount, 5); | |
} | |
function cc($amount, $kindOfCoins) { | |
if ($amount === 0) { yield []; return; } | |
if ($amount < 0) return; | |
if ($kindOfCoins === 0) return; | |
foreach (cc($amount, $kindOfCoins - 1) as $c) | |
yield $c; | |
foreach (cc($amount - firstDenomination($kindOfCoins), $kindOfCoins) as $c) | |
yield array_merge([firstDenomination($kindOfCoins)], $c); | |
} | |
function firstDenomination($kindOfCoins) { | |
if ($kindOfCoins === 1) return 1; | |
if ($kindOfCoins === 2) return 5; | |
if ($kindOfCoins === 3) return 10; | |
if ($kindOfCoins === 4) return 25; | |
if ($kindOfCoins === 5) return 50; | |
} | |
var_dump(count(iterator_to_array(countChange(100)))); | |
foreach (countChange(100) as $cc) { | |
echo join(" ", $cc), "\n"; | |
} |
This file contains 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 | |
// Sieve of Eratosthenes | |
function ints($n) { | |
for ($i = $n;; $i++) yield $i; | |
} | |
function headTail($gen) { | |
$head = $gen->current(); | |
$gen->next(); | |
return [$head, $gen]; | |
} | |
function primes($nums) { | |
list($head, $tail) = headTail($nums); | |
yield $head; | |
foreach (primes($tail) as $p) { | |
if ($p % $head != 0) | |
yield $p; | |
} | |
} | |
$ps = primes(ints(2)); | |
foreach ($ps as $p) { | |
if ($p > 1000) break; | |
echo $p." "; | |
} |
This file contains 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 | |
interface Tree { | |
function preOrder(); | |
function inOrder(); | |
function postOrder(); | |
} | |
class Fork implements Tree { | |
private $left, $el, $right; | |
function __construct(Tree $left, $el, Tree $right) { | |
list($this->left, $this->el, $this->right) = func_get_args(); | |
} | |
function preOrder() { | |
yield $this->el; | |
foreach ($this->left->preOrder() as $x) yield $x; | |
foreach ($this->right->preOrder() as $x) yield $x; | |
} | |
function inOrder() { | |
foreach ($this->left->inOrder() as $x) yield $x; | |
yield $this->el; | |
foreach ($this->right->inOrder() as $x) yield $x; | |
} | |
function postOrder() { | |
foreach ($this->left->postOrder() as $x) yield $x; | |
foreach ($this->right->postOrder() as $x) yield $x; | |
yield $this->el; | |
} | |
} | |
class Leaf implements Tree { | |
function preOrder() { return; yield; } | |
function inOrder() { return; yield; } | |
function postOrder() { return; yield; } | |
} | |
// *** | |
$leaf = new Leaf(); | |
$t = new Fork(new Fork($leaf, 1, $leaf), 3, new Fork($leaf, 2, $leaf)); | |
foreach ($t->inOrder() as $x) | |
echo $x, "\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment