Last active
August 29, 2015 13:58
-
-
Save akanehara/10251951 to your computer and use it in GitHub Desktop.
sequence :: [m a] -> m [a] like function for PHP array
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 | |
| function combinations() | |
| { | |
| $xss = func_get_args(); | |
| return array_reduce($xss, function($yss, $xs) { | |
| $zs = []; | |
| foreach ($yss as $ys) { | |
| foreach ($xs as $x) { | |
| $zs[] = array_merge($ys, array($x)); | |
| } | |
| } | |
| return $zs; | |
| }, [[]]); | |
| } | |
| $paramsSet = combinations( | |
| [1,2,3], | |
| ['http://www.example.com/', null], | |
| ['male', 'female', 'none'] | |
| ); | |
| foreach($paramsSet as $params) { | |
| list($a, $b, $c) = $params; | |
| echo "f($a, $b, $c)\n"; | |
| } | |
| f(1, http://www.example.com/, male) | |
| f(1, http://www.example.com/, female) | |
| f(1, http://www.example.com/, none) | |
| f(1, , male) | |
| f(1, , female) | |
| f(1, , none) | |
| f(2, http://www.example.com/, male) | |
| f(2, http://www.example.com/, female) | |
| f(2, http://www.example.com/, none) | |
| f(2, , male) | |
| f(2, , female) | |
| f(2, , none) | |
| f(3, http://www.example.com/, male) | |
| f(3, http://www.example.com/, female) | |
| f(3, http://www.example.com/, none) | |
| f(3, , male) | |
| f(3, , female) | |
| f(3, , none) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
でもこれパターンが多いと遅延じゃなきゃ実用になんないね。。