zonuexe/functools

Functional toolbox

0.2.0 2015-11-23 10:49 UTC

This package is not auto-updated.

Last update: 2024-11-09 17:19:35 UTC


README

Package version Build Status Packagist Coverage Status

Functional toolbox for PHP

Installation

Composer

composer require zonuexe/functools

Features

  • Functools\Operator
    • Operator Expression wrapper
      • Binary operator 1 + 2 $a === "abc" $date instanceof '\Datetime'
      • Conditional operator $cond ? $then : $else
      • Lazy Conditional operator $cond($v) ? $then($v) : $else($v) (thunk)
    • Referential transparent version sorting array functions.
      • You can array_map(f::op('ksort'), $arrays) !
    • Language-construct wrapper
      • echo, print, eval, require, require_once, include, include_once
      • limited support: array, isset, empty
  • Functools::partial(callable $callback, array $arguments, int $pos)
    • Partial application for callable (arary_map friendly)
  • Functools::arity(callable $callback)
    • Analyze arity(number of arguments) of $callback.
  • Functools::curry(callable $callback)
  • Functools::op(string $symbol, [array $arguments, int $pos])
    • Get callable object (and partial application.)
  • Functools::compose(callable $callback,...)
  • Functools::pipe(mixed $value, callable $callback...)
  • Functools::tuple(mixed $item,...)
  • Functools::fix(callable $callback)
  • Functools::memoize(callable $callback, [array $cache])
    • Function optimizer using Memoization.
    • This feature will help in optimization of transparent reference explicit definition equation.

Iteration

This library does not have iterator. You will be able to combine it by selecting a favorite of iterator library.

Usage

Japanese version: http://qiita.com/tadsan/items/abc52f0739ab5b4e781b

Short syntax

use Teto\Functools as f;

f::partial()

$comma = f::partial("implode", [", "]);
$comma(range(1, 10));
// "1, 2, 3, 4, 5, 6, 7, 8, 9, 10"

$join_10 = f::partial("implode", [1 => range(1, 10)], 0);
$join_10("@");
// "1@2@3@4@5@6@7@8@9@10"
$join_10("\\");
=> "1\\2\\3\\4\\5\\6\\7\\8\\9\\10"

$sleep3 = f::partial("sleep", [3]);
$sleep3();
$sleep3("foo"); // Error!

$sleep3 = f::partial("sleep", [3], -1);
$sleep3("foo"); // OK!

f::op()

$add = f::op("+");
$add(2, 3); // (2 + 3) === 5

$add_1 = f::op("+", [1]);
$add_1(4);  // (1 + 4) === 5

$half = f::op("/", [1 => 2], 0);
$half(10);  // (10 / 2) === 5

f::tuple()

$teto  = f::tuple("Teto Kasane",  31, "2008-04-01", "Baguette");
$ritsu = f::tuple("Ritsu Namine", 6,  "2009-10-02", "Napa cabbage");

// index access
$teto[0]; // "Teto Kasane"
$teto[1]; // 31
$teto[2]; // "2008-04-01"
$teto[3]; // "Baguette"

// property access

$tetop  = f::tuple("name", "Teto Kasane",  "age", 31, "birthday", "2008-04-01", "item", "Baguette");
$ritsup = f::tuple("name", "Ritsu Namine", "age",  6, "birthday", "2009-10-02", "item", "Napa cabbage");
$tetop->pget("name");     // "Teto Kasane"
$tetop->pget("age");      // 31
$tetop->pget("birthday"); // "2008-04-01"
$tetop->pget("item");     // "Baguette"

f::fix()

$fib = f::fix(function ($fib) {
    return function ($x) use ($fib) {
        return ($x < 2) ? 1 : $fib($x - 1) + $fib($x -2);
    };
});

$fib(6); // 13

f::memoize()

// simple fibonacci function. But, very very slow.
$fib1 = function ($n) use (&$fib1) {
    return ($n < 2) ? $n : $fib1($n - 1) + $fib1($n - 2);
};

// simple fibonacci function too. very fast!
$fib2 = f::memoize(function ($n) use (&$fib2) {
    return ($n < 2) ? $n : $fib2($n - 1) + $fib2($n - 2);
}, [0, 1]);

Copyright

see ./LICENSE.

Functional toolbox
Copyright (c) 2015 USAMI Kenta <tadsan@zonu.me>

Teto Kasane

I love Teto Kasane. (ja: Teto Kasane official site)

       r /
  __ , --ヽ!-- .、_
 !  `/::::;::::ヽ l
 !二二!::/}::::丿ハニ|
 !ニニ.|:/ ノ/ }::::}コ
 L二lイ  0´ 0 ,':ノコ
 lヽノ/ヘ、 '' ▽_ノイ ソ
  ソ´ /}`ス / ̄ ̄ ̄ ̄/
   .(_:;つ/  0401 / カタカタ
  ̄ ̄ ̄ ̄ ̄\/____/