Created
May 10, 2017 19:56
-
-
Save pwm/3014800b91f5aa6bc23afdedcb36cefc to your computer and use it in GitHub Desktop.
Lifting out computation from iteration
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 | |
declare(strict_types=1); | |
function uppercaseWordsImperative(array $words): void { | |
foreach ($words as $word) { | |
$word = strtoupper($word); | |
echo $word.' '; | |
} | |
} | |
uppercaseWordsImperative(['foo', 'bar', 'baz']); | |
echo PHP_EOL; | |
// [String] -> [String] | |
function uppercaseWords(array $words): Generator { | |
foreach ($words as $word) { | |
yield strtoupper($word); | |
} | |
} | |
// [String] -> IO () | |
function uppercaseWordsImperativeShell(array $words): void { | |
foreach (uppercaseWords($words) as $word) { | |
echo $word.' '; | |
} | |
} | |
uppercaseWordsImperativeShell(['foo', 'bar', 'baz']); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment