Skip to content

Instantly share code, notes, and snippets.

@akanehara
Last active August 29, 2015 13:58
Show Gist options
  • Select an option

  • Save akanehara/10251951 to your computer and use it in GitHub Desktop.

Select an option

Save akanehara/10251951 to your computer and use it in GitHub Desktop.
sequence :: [m a] -> m [a] like function for PHP array
<?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)
@akanehara
Copy link
Author

でもこれパターンが多いと遅延じゃなきゃ実用になんないね。。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment