Ejemplo n.º 1
0
/**
 * Returns array which contains sorted items the passed sequence
 *
 * @param array|\Traversable $array
 * @param bool $reversed If true then return reversed sorted sequence. If not boolean and $key was not passed then acts as a $key parameter
 * @param callable $key Function of one argument that is used to extract a comparison key from each element
 * @param callable $cmp Function of two arguments which returns a negative number, zero or positive number depending on
 *                      whether the first argument is smaller than, equal to, or larger than the second argument
 * @return array
 */
function sorted($array, $reversed = false, callable $key = null, callable $cmp = null)
{
    args\expects(args\traversable, $array);
    args\expects([args\bool, args\callable_], $reversed);
    if (!$cmp) {
        $cmp = function ($a, $b) {
            return $a > $b ? 1 : -1;
        };
    }
    if (!is_bool($reversed) && !$key) {
        $key = $reversed;
    }
    if ($key) {
        $cmp = function ($a, $b) use($key, $cmp) {
            return $cmp($key($a), $key($b));
        };
    }
    if (is_bool($reversed) && $reversed) {
        $cmp = f\compose(op\neg, $cmp);
    }
    $array = ds\traversableToArray($array);
    $isList = ds\isList($array);
    uasort($array, $cmp);
    return $isList ? array_values($array) : $array;
}
Ejemplo n.º 2
0
Archivo: FTest.php Proyecto: ihor/Nspl
 public function testCompose()
 {
     $countFiltered = compose('count', filter);
     $this->assertEquals(3, $countFiltered('is_int', [1, 'a', 2, 'b', 3]));
     $underscoreToCamelcase = compose('lcfirst', partial('str_replace', '_', ''), rpartial('ucwords', '_'));
     $this->assertEquals('underscoreToCamelcase', $underscoreToCamelcase('underscore_to_camelcase'));
     $countFiltered = call_user_func(compose, 'count', filter);
     $this->assertEquals(3, $countFiltered('is_int', [1, 'a', 2, 'b', 3]));
     $this->assertEquals('\\nspl\\f\\compose', compose);
 }
Ejemplo n.º 3
0
Archivo: f.php Proyecto: ihor/Nspl
use function nspl\f\compose;
use function nspl\f\memoized;
use const nspl\op\object;
use const nspl\op\gt;
use const nspl\op\mul;
use function nspl\op\propertyGetter;
use const nspl\a\value;
use function nspl\a\map;
use function nspl\a\reduce;
use function nspl\a\filter;
$users = map(object, [array('id' => 1, 'name' => 'John', 'age' => 15), array('id' => 2, 'name' => 'Jack', 'age' => 35), array('id' => 3, 'name' => 'Sarah', 'age' => 25), array('id' => 4, 'name' => 'Norah', 'age' => 20), array('id' => 5, 'name' => 'Michael', 'age' => 30), array('id' => 6, 'name' => 'Bob', 'age' => 30)]);
// 1. Get user name from which can be stored as username, user_name or name in data array
$data = array('id' => 1337, 'name' => 'John', 'gender' => 'male');
$name = reduce(flipped(partial(value, $data)), ['username', 'user_name', 'name'], '');
echo sprintf("User name is %s\n", $name);
// 2. Get users older than 25
$isOlderThan25 = compose(rpartial(gt, 25), propertyGetter('age'));
$olderThan25 = filter($isOlderThan25, $users);
echo "These users are older than 25:\n";
foreach ($olderThan25 as $user) {
    echo sprintf("    %s - %s y.o.\n", $user->name, $user->age);
}
// 3. Memoizing heavy calculations
$factorial = function ($n) {
    echo "Calculating {$n}!\n";
    return reduce(mul, range(1, $n), 1);
};
$memoizedFactorial = memoized($factorial);
foreach ([3, 3, 5, 5, 5] as $n) {
    echo sprintf("%s! = %s\n", $n, $memoizedFactorial($n));
}
Ejemplo n.º 4
0
Archivo: a.php Proyecto: ihor/Nspl
/**
 * Returns array which contains sorted items from the passed sequence
 *
 * @param array|\Traversable $sequence
 * @param bool $reversed If true then return reversed sorted sequence. If not boolean and $key was not passed then acts as a $key parameter
 * @param callable $key Function of one argument that is used to extract a comparison key from each item
 * @param callable $cmp Function of two arguments which returns a negative number, zero or positive number depending on
 *                      whether the first argument is smaller than, equal to, or larger than the second argument
 * @return array
 */
function sorted($sequence, $reversed = false, callable $key = null, callable $cmp = null)
{
    args\expects(args\traversable, $sequence);
    args\expects([args\bool, args\callable_], $reversed);
    if (!$cmp) {
        $cmp = function ($a, $b) {
            return $a > $b ? 1 : -1;
        };
    }
    if (!is_bool($reversed) && !$key) {
        $key = $reversed;
    }
    if ($key) {
        $cmp = function ($a, $b) use($key, $cmp) {
            return $cmp($key($a), $key($b));
        };
    }
    if (is_bool($reversed) && $reversed) {
        $cmp = f\compose(op\neg, $cmp);
    }
    if ($sequence instanceof \Iterator) {
        $sequence = iterator_to_array($sequence);
    }
    $isList = isList($sequence);
    uasort($sequence, $cmp);
    return $isList ? array_values($sequence) : $sequence;
}