예제 #1
0
파일: walk.php 프로젝트: rkgladson/PHPixme
<?php

namespace PHPixme;

/**
 * map
 * @param callable $hof
 * @param array|FunctorInterface|\Traversable $collection
 * @return \Closure|$collection
 */
function walk($hof = null, $collection = null)
{
    return call_user_func_array(__PRIVATE__::$instance[walk], func_get_args());
}
const walk = __NAMESPACE__ . '\\walk';
__PRIVATE__::$instance[walk] = __PRIVATE__::curryExactly2(function ($hof, $collection) {
    __CONTRACT__::argIsACallable($hof);
    __CONTRACT__::argIsATraversable($collection, 1);
    if ($collection instanceof FunctorInterface) {
        return $collection->walk($hof);
    }
    $array = __PRIVATE__::getArrayFrom($collection);
    if ($array !== null) {
        array_walk($array, $hof, $collection);
    } else {
        foreach (__PRIVATE__::copyTransversable($collection) as $k => $v) {
            call_user_func($hof, $v, $k, $collection);
        }
    }
    return $collection;
});
예제 #2
0
namespace PHPixme;

/**
 * @param callable $hof
 * @param \Traversable= $traversable
 * @return \Closure|mixed
 * @sig Callable (a, b -> a) -> \Traversable[a,b] -> a
 */
function reduceRight($hof = null, $traversable = null)
{
    return call_user_func_array(__PRIVATE__::$instance[reduceRight], func_get_args());
}
const reduceRight = __NAMESPACE__ . '\\reduceRight';
__PRIVATE__::$instance[reduceRight] = __PRIVATE__::curryExactly2(function ($hof, $arrayLike) {
    __CONTRACT__::argIsACallable($hof);
    __CONTRACT__::argIsATraversable($arrayLike, 1);
    if ($arrayLike instanceof ReducibleInterface) {
        return $arrayLike->reduceRight($hof);
    }
    // Equalize the usefulness
    $array = __CONTRACT__::isNonEmpty(__PRIVATE__::traversableToArray($arrayLike));
    $output = end($array);
    $value = prev($array);
    // Traverse using the internal pointer to avoid creating additional work
    while (($key = key($array)) !== null) {
        $output = call_user_func($hof, $output, $value, $key, $arrayLike);
        $value = prev($array);
    }
    return $output;
});
예제 #3
0
파일: map.php 프로젝트: rkgladson/PHPixme
<?php

namespace PHPixme;

/**
 * @param callable $hof
 * @param array|\Traversable|FunctorInterface $traversable
 * @return \Closure|mixed
 * @sig Callable (a -> b) -> \Traversable[a] -> \Traversable[b]
 *
 */
function map($hof = null, $traversable = null)
{
    return call_user_func_array(__PRIVATE__::$instance[map], func_get_args());
}
const map = __NAMESPACE__ . '\\map';
__PRIVATE__::$instance[map] = __PRIVATE__::curryExactly2(function ($hof, $traversable) {
    __CONTRACT__::argIsACallable($hof);
    __CONTRACT__::argIsATraversable($traversable, 1);
    if ($traversable instanceof FunctorInterface) {
        return $traversable->map($hof);
    }
    $output = [];
    foreach (__PRIVATE__::protectTraversable($traversable) as $key => $value) {
        $output[$key] = call_user_func($hof, $value, $key, $traversable);
    }
    return $output;
});