Created
October 1, 2014 07:47
-
-
Save ngyuki/5d10b128bfa8491e6ffb to your computer and use it in GitHub Desktop.
function compose example ... see http://qiita.com/yuya_takeyama/items/b65afa141327e7ccd1ac
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 compose(callable $initial, callable ...$functions) | |
{ | |
if (count($functions) < 1) { | |
throw new \InvalidArgumentException('at least two functions are required'); | |
} | |
return array_reduce($functions, function ($f, $g) { | |
return function (...$args) use ($f, $g) { | |
return $f($g(...$args)); | |
}; | |
}, $initial); | |
} | |
$splitAsWords = function ($str) { | |
return \preg_split('/\s+/u', $str); | |
}; | |
$camelizeWords = function ($words) { | |
return \array_map('ucfirst', $words); | |
}; | |
$join = function ($words) { | |
return \join('', $words); | |
}; | |
$lowerCamelize = compose('lcfirst', $join, $camelizeWords, $splitAsWords); | |
echo $lowerCamelize('foo bar baz'); // => "fooBarBaz" |
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 compose(callable ...$functions) | |
{ | |
if (count($functions) < 2) { | |
throw new \InvalidArgumentException('at least two functions are required'); | |
} | |
return array_reduce($functions, function ($f, $g) { | |
return $f ? function (...$args) use ($f, $g) { | |
return $f($g(...$args)); | |
}: $g; | |
}); | |
} | |
$splitAsWords = function ($str) { | |
return \preg_split('/\s+/u', $str); | |
}; | |
$camelizeWords = function ($words) { | |
return \array_map('ucfirst', $words); | |
}; | |
$join = function ($words) { | |
return \join('', $words); | |
}; | |
$lowerCamelize = compose('lcfirst', $join, $camelizeWords, $splitAsWords); | |
echo $lowerCamelize('foo bar baz'); // => "fooBarBaz" |
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 compose(callable ...$functions) | |
{ | |
if (count($functions) < 2) { | |
throw new \InvalidArgumentException('at least two functions are required'); | |
} | |
return array_reduce($functions, function ($f, $g) { | |
return function (...$args) use ($f, $g) { | |
return $f ? $f($g(...$args)) : $g(...$args); | |
}; | |
}); | |
} | |
$splitAsWords = function ($str) { | |
return \preg_split('/\s+/u', $str); | |
}; | |
$camelizeWords = function ($words) { | |
return \array_map('ucfirst', $words); | |
}; | |
$join = function ($words) { | |
return \join('', $words); | |
}; | |
$lowerCamelize = compose('lcfirst', $join, $camelizeWords, $splitAsWords); | |
echo $lowerCamelize('foo bar baz'); // => "fooBarBaz" |
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 compose(callable ...$functions) | |
{ | |
if (count($functions) < 2) { | |
throw new \InvalidArgumentException('at least two functions are required'); | |
} | |
return array_reduce($functions, function ($f, $g) { | |
return function (...$args) use ($f, $g) { | |
return $f($g(...$args)); | |
}; | |
}, function ($v) { return $v; }); | |
} | |
$splitAsWords = function ($str) { | |
return \preg_split('/\s+/u', $str); | |
}; | |
$camelizeWords = function ($words) { | |
return \array_map('ucfirst', $words); | |
}; | |
$join = function ($words) { | |
return \join('', $words); | |
}; | |
$lowerCamelize = compose('lcfirst', $join, $camelizeWords, $splitAsWords); | |
echo $lowerCamelize('foo bar baz'); // => "fooBarBaz" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment