Beispiel #1
0
<?php

require_once __DIR__ . '/../autoload.php';
use const nspl\op\object;
use const nspl\op\gt;
use function nspl\op\itemGetter;
use function nspl\op\propertyGetter;
use function nspl\f\map;
use function nspl\f\rpartial;
use function nspl\a\all;
use function nspl\a\any;
use function nspl\a\sorted;
$users = [array('name' => 'John', 'age' => 15), array('name' => 'Jack', 'age' => 35), array('name' => 'Sarah', 'age' => 25), array('name' => 'Norah', 'age' => 20), array('name' => 'Michael', 'age' => 30)];
// 1. Get user names from list of users presented with array data
$names = map(itemGetter('name'), $users);
echo sprintf("User names are: %s (users were presented with array data)\n", implode(', ', $names));
// 2. Convert list of user presented with array data to list of objects
$objects = map(object, $users);
echo sprintf("List of users converted to objects consists of types: %s\n", implode(', ', map('\\nspl\\ds\\getType', $objects)));
// 3. Sort users by age
$sorted = sorted($users, false, itemGetter('age'));
echo "Users sorted by age:\n";
foreach ($sorted as $user) {
    echo sprintf("    %s - %s y.o.\n", $user['name'], $user['age']);
}
// 4. Check if all numbers are positive
$allPositive = all([1, 2, 3, 4, 5], rpartial(gt, 0));
echo $allPositive ? "All numbers are positive\n" : "At least one number was not positive\n";
require 'nspl/autoload.php';
use nspl\f;
use nspl\op;
use nspl\a;
$users = [['first_name' => 'Max', 'last_name' => 'Gopey', 'company' => 'CGI'], ['first_name' => 'Bob', 'last_name' => 'Doe', 'company' => 'Google'], ['first_name' => 'Alice', 'last_name' => 'Doe', 'company' => 'Google']];
$startsWith = function ($string, $substing) {
    return stripos($string, $substing) === 0;
};
$contains = function ($string, $substing) {
    return stripos($string, $substing) !== false;
};
$getFullName = function ($firstName, $lastName) {
    return $lastName . ', ' . $firstName;
};
$startsWithD = f\rpartial($startsWith, 'd');
$isBob = f\rpartial($contains, 'bob');
$getFullNameFromUser = function ($user) use($getFullName) {
    return $getFullName($user['first_name'], $user['last_name']);
};
$getStackKey = function ($name) use($isBob) {
    return $isBob($name) ? 'bobs' : 'alices';
};
$putToCorrectStack = function ($stacks, $value) use($getStackKey) {
    $stacks[$getStackKey($value)][] = $value;
    return $stacks;
};
$getBobsAndAlicesWithD = function ($users) use($startsWithD, $getFullNameFromUser, $putToCorrectStack) {
    return f\pipe($users, f\partial(a\map, $getFullNameFromUser), f\partial(a\filter, $startsWithD), f\ppartial(a\reduce, [0 => $putToCorrectStack, 2 => ['bobs' => [], 'alices' => []]]));
};
print_r($getBobsAndAlicesWithD($users));
Beispiel #3
0
 public function testI()
 {
     $this->assertEquals('underscoreToCamelcase', I('underscore_to_camelcase', rpartial('ucwords', '_'), partial('str_replace', '_', ''), 'lcfirst'));
 }
Beispiel #4
0
Datei: f.php Projekt: 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));
}
Beispiel #5
0
 public function testDropWhile()
 {
     $this->assertEquals(['a', 'b', 'c', 4, 5, 6], dropWhile('is_numeric', [1, 2, 3, 'a', 'b', 'c', 4, 5, 6]));
     $this->assertEquals([4, 5, 6, 7, 8, 9], dropWhile(rpartial(lt, 4), [1, 2, 3, 4, 5, 6, 7, 8, 9]));
     $this->assertEquals([4, 5, 6, 7, 8, 9], dropWhile(rpartial(lt, 4), new \ArrayIterator([1, 2, 3, 4, 5, 6, 7, 8, 9])));
     $this->assertEquals([], dropWhile(rpartial(lt, 4), []));
     $this->assertEquals([4, 5, 6, 7, 8, 9], call_user_func(dropWhile, rpartial(lt, 4), [1, 2, 3, 4, 5, 6, 7, 8, 9]));
     $this->assertEquals('\\nspl\\a\\dropWhile', dropWhile);
 }