コード例 #1
0
ファイル: Collection.php プロジェクト: ihor/Nspl
 protected function stringifyArray(array $array)
 {
     if (a\isList($array)) {
         return 'array(' . implode(', ', array_map(function ($v) {
             return is_scalar($v) ? var_export($v, true) : \nspl\getType($v);
         }, $array)) . ')';
     }
     return f\I(var_export($this->array, true), f\partial('str_replace', "\n", ''), f\partial('str_replace', 'array (  ', 'array('), f\partial('str_replace', '  ', ' '), f\partial('str_replace', ',)', ')'));
 }
コード例 #2
0
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));
コード例 #3
0
ファイル: args.php プロジェクト: ihor/Nspl
 /**
  * @param string|object|array $type
  * @param bool $onlyOr
  * @return string
  */
 public static function getFor($type, $onlyOr = false)
 {
     if (is_array($type)) {
         $messagesFor = f\partial(a\map, ['\\nspl\\args\\_p\\ErrorMessage', 'getFor']);
         if ($onlyOr) {
             return implode(' or ', $messagesFor($type));
         } else {
             $isOr = function ($t) {
                 return isset(Checker::$isOr[$t]) || class_exists($t) || interface_exists($t);
             };
             list($orTypes, $andTypes) = a\partition($isOr, $type);
             return implode(' and ', array_filter([implode(' or ', $messagesFor($orTypes)), implode(' and ', $messagesFor($andTypes))]));
         }
     }
     if ($type instanceof Constraint) {
         return $type->__toString();
     }
     $default = class_exists($type) || interface_exists($type) ? $type : implode(' ', array_map('strtolower', preg_split('/(?=[A-Z])/', a\last(explode('\\', $type)))));
     return a\value(self::$messages, $type, 'be ' . $default);
 }
コード例 #4
0
ファイル: args.php プロジェクト: rakesh-mohanta/Nspl
 /**
  * @param string|object|array $type
  * @param bool $onlyOr
  * @return string
  */
 public static function getFor($type, $onlyOr = false)
 {
     if (is_array($type)) {
         $messagesFor = f\partial(f\map, ['\\nspl\\args\\_p\\ErrorMessage', 'getFor']);
         if ($onlyOr) {
             return implode(' or ', $messagesFor($type));
         } else {
             $isOr = function ($t) {
                 return isset(Checker::$isOr[$t]) || class_exists($t);
             };
             list($orTypes, $andTypes) = f\partition($isOr, $type);
             return implode(' and ', array_filter([implode(' or ', $messagesFor($orTypes)), implode(' and ', $messagesFor($andTypes))]));
         }
     }
     if ($type instanceof Constraint) {
         return $type->__toString();
     }
     return a\getByKey(self::$messages, $type, 'be ' . (class_exists($type) ? $type : end(explode('\\', $type))));
 }
コード例 #5
0
ファイル: f.php プロジェクト: ihor/Nspl
use function nspl\f\rpartial;
use function nspl\f\flipped;
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) {
コード例 #6
0
ファイル: FTest.php プロジェクト: ihor/Nspl
 public function testI()
 {
     $this->assertEquals('underscoreToCamelcase', I('underscore_to_camelcase', rpartial('ucwords', '_'), partial('str_replace', '_', ''), 'lcfirst'));
 }
コード例 #7
0
ファイル: a.php プロジェクト: ihor/Nspl
use function nspl\a\keySorted;
use function nspl\a\indexed;
use function nspl\a\take;
use function nspl\a\reorder;
use const nspl\op\eq;
use const nspl\op\object;
use function nspl\op\itemGetter;
use function nspl\op\propertyGetter;
use function nspl\f\partial;
$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. Check all statuses are "ready"
$statuses = ['ready', 'ready', 'not-ready'];
$ready = all($statuses, partial(eq, 'ready'));
echo $ready ? "Everybody is ready\n" : "Someone is not ready\n";
// 2. Check at least someone is "ready"
$someoneIsReady = any($statuses, partial(eq, 'ready'));
echo $someoneIsReady ? "Someone is ready\n" : "Everybody is not ready\n";
// 3. Get user ids
$userIds = map(propertyGetter('id'), $users);
echo sprintf("User ids are: %s\n", implode(', ', $userIds));
// 4. Count users younger than 25
$youngerThan25Count = reduce(function ($count, $user) {
    return $count + (int) ($user->age < 25);
}, $users);
echo sprintf("%s users are younger than 25\n", $youngerThan25Count);
// 5. Get users younger than 25
$youngerThan25 = filter(function ($user) {
    return $user->age < 25;
}, $users);
echo "These users are younger than 25:\n";
foreach ($youngerThan25 as $user) {
コード例 #8
0
ファイル: a.php プロジェクト: rakesh-mohanta/Nspl
use function nspl\op\itemGetter;
use function nspl\op\propertyGetter;
use function nspl\f\map;
use function nspl\f\reduce;
use function nspl\f\flipped;
use function nspl\f\partial;
// 1. Check all statuses are "ready"
$statuses = ['ready', 'ready', 'not-ready'];
$ready = all($statuses, partial(eq, 'ready'));
echo $ready ? "Everybody is ready\n" : "Someone is not ready\n";
// 2. Check at least someone is "ready"
$someoneIsReady = any($statuses, partial(eq, 'ready'));
echo $someoneIsReady ? "Someone is ready\n" : "Everybody is not ready\n";
// 3. 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(getByKey, $data)), ['username', 'user_name', 'name'], '');
echo sprintf("User name is %s\n", $name);
// 4. Sort list of user objects by their name
$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)]);
$usersSortedByName = sorted($users, false, propertyGetter('name'));
echo "Users sorted by name:\n";
foreach ($usersSortedByName as $user) {
    echo sprintf("    %s\n", $user->name);
}
// 5. Index users by ids
$usersIndexedByIds = indexed($users, propertyGetter('id'));
// In case of array it would be indexed($users, 'id')
echo "Users indexed by id:\n";
foreach ($usersIndexedByIds as $id => $user) {
    echo sprintf("    %s. %s\n", $id, $user->name);
}