Family Day update
This commit is contained in:
parent
aac6731f2c
commit
9ad63ea473
2442 changed files with 39761 additions and 8255 deletions
7
Task/Currying/Ol/currying.ol
Normal file
7
Task/Currying/Ol/currying.ol
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
(define (addN n)
|
||||
(lambda (x) (+ x n)))
|
||||
|
||||
(let ((add10 (addN 10))
|
||||
(add20 (addN 20)))
|
||||
(print "(add10 4) ==> " (add10 4))
|
||||
(print "(add20 4) ==> " (add20 4)))
|
||||
95
Task/Currying/PHP/currying.php
Normal file
95
Task/Currying/PHP/currying.php
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
<?php
|
||||
|
||||
function curry($callable)
|
||||
{
|
||||
if (_number_of_required_params($callable) === 0) {
|
||||
return _make_function($callable);
|
||||
}
|
||||
if (_number_of_required_params($callable) === 1) {
|
||||
return _curry_array_args($callable, _rest(func_get_args()));
|
||||
}
|
||||
return _curry_array_args($callable, _rest(func_get_args()));
|
||||
}
|
||||
|
||||
function _curry_array_args($callable, $args, $left = true)
|
||||
{
|
||||
return function () use ($callable, $args, $left) {
|
||||
if (_is_fullfilled($callable, $args)) {
|
||||
return _execute($callable, $args, $left);
|
||||
}
|
||||
$newArgs = array_merge($args, func_get_args());
|
||||
if (_is_fullfilled($callable, $newArgs)) {
|
||||
return _execute($callable, $newArgs, $left);
|
||||
}
|
||||
return _curry_array_args($callable, $newArgs, $left);
|
||||
};
|
||||
}
|
||||
|
||||
function _number_of_required_params($callable)
|
||||
{
|
||||
if (is_array($callable)) {
|
||||
$refl = new \ReflectionClass($callable[0]);
|
||||
$method = $refl->getMethod($callable[1]);
|
||||
return $method->getNumberOfRequiredParameters();
|
||||
}
|
||||
$refl = new \ReflectionFunction($callable);
|
||||
return $refl->getNumberOfRequiredParameters();
|
||||
}
|
||||
|
||||
function _make_function($callable)
|
||||
{
|
||||
if (is_array($callable))
|
||||
return function() use($callable) {
|
||||
return call_user_func_array($callable, func_get_args());
|
||||
};
|
||||
return $callable;
|
||||
}
|
||||
|
||||
function _execute($callable, $args, $left)
|
||||
{
|
||||
if (! $left) {
|
||||
$args = array_reverse($args);
|
||||
}
|
||||
$placeholders = _placeholder_positions($args);
|
||||
if (0 < count($placeholders)) {
|
||||
$n = _number_of_required_params($callable);
|
||||
if ($n <= _last($placeholders[count($placeholders) - 1])) {
|
||||
throw new \Exception('Argument Placeholder found on unexpected position!');
|
||||
}
|
||||
foreach ($placeholders as $i) {
|
||||
$args[$i] = $args[$n];
|
||||
array_splice($args, $n, 1);
|
||||
}
|
||||
}
|
||||
return call_user_func_array($callable, $args);
|
||||
}
|
||||
|
||||
function _placeholder_positions($args)
|
||||
{
|
||||
return array_keys(array_filter($args, '_is_placeholder'));
|
||||
}
|
||||
|
||||
function _is_fullfilled($callable, $args)
|
||||
{
|
||||
$args = array_filter($args, function($arg) {
|
||||
return ! _is_placeholder($arg);
|
||||
});
|
||||
return count($args) >= _number_of_required_params($callable);
|
||||
}
|
||||
|
||||
function _is_placeholder($arg)
|
||||
{
|
||||
return $arg instanceof Placeholder;
|
||||
}
|
||||
|
||||
function _rest(array $args)
|
||||
{
|
||||
return array_slice($args, 1);
|
||||
}
|
||||
|
||||
function product($a, $b)
|
||||
{
|
||||
return $a * $b;
|
||||
}
|
||||
|
||||
echo json_encode(array_map(curry('product', 7), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]));
|
||||
Loading…
Add table
Add a link
Reference in a new issue