-
JavaScript-inspired methods
-
Underscore.js-inspired methods
Method
_.deferis not relevant in in the context of PHP
Calls a function with a given this value, and arguments provided as an array
{mixed} $source- source mixed{object} $context- new $this{array} $args- array of arguments
apply(mixed $source, $context = null, array $args = [])<?php
$obj = Arrays::object(["foo" => "FOO"]);
$source = function( $input ){ return $input . "_" . $this->foo; };
$res = Functions::apply($source, $obj, ["BAR"]); // "BAR_FOO"Creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called
{mixed} $source- source mixed{object} $context- new $this
bind(mixed $source, $context = null)<?php
$obj = Arrays::object(["foo" => "FOO"]);
$source = function( $input ){ return $input . "_" . $this->foo; };
$func = Functions::bind($source, $obj);
echo $func("BAR"); // "BAR_FOO"Calls a function with a given $context value and arguments provided individually.
{mixed} $source- source mixed{object} $context- new $this{array} ...$args- arguments
call(mixed $source, $context = null, ...$args)<?php
$obj = Arrays::object(["foo" => "FOO"]);
$source = function( $input ){ return $input . "_" . $this->foo; };
$res = Functions::call($source, $obj, "BAR"); // "BAR_FOO"Returns a mixed representing the source code of the function.
{mixed} $source- source mixed
toString(mixed $source)<?php
echo Functions::toString("strlen");mixed(112) "Function [ <internal:Core> function strlen ] {
- Parameters [1] {
Parameter #0 [ <required> $str ]
}
}
Binds a number of methods on the object, specified by $methodNames, to be run in the context of that object whenever they are invoked. Very handy for binding functions that are going to be used as event handlers, which would otherwise be invoked with a fairly useless this. $methodNames are required
{object} $source- context object{array} $methodNames- method names to bind
bindAll($obj, ...$methodNames)<?php
$foo = (object)[
"value" => 1,
"increment" => function(){
$this->value++;
},
"reset" => function(){
$this->value = 0;
}
];
Functions::bindAll($foo, "increment", "reset");
($foo->increment)();
echo $foo->value; // 2
($foo->reset)();
echo $foo->value; // 0Partially apply a function by filling in any number of its arguments
{mixed} $source- source function{array} $boundArgs- arguments to bind
partial(mixed $source, ...$boundArgs)<?php
$subtract = function($a, $b) { return $b - $a; };
$sub5 = Functions::partial($subtract, 5);
$res = $sub5(20); // 15Memoizes a given function by caching the computed result. Useful for speeding up slow-running computations. If passed an optional hashFunction
{mixed} $source- source function{mixed} $hasher- (optional) hash generator
memoize($source, $hasher = null)<?php
$counter = Functions::memoize("fixtureCounter::increment");
$counter($foo); // 1
$counter($foo); // 1
$counter($bar); // 2
$counter($baz); // 3Much like setTimeout, invokes function after wait milliseconds. If you pass the optional arguments, they will be forwarded on to the function when it is invoked.
{mixed} $source- source function{int} $wait- wait time in ms{array} $args- (optional) arguments to pass into produced function
delay(mixed $source, int $wait, ...$args)<?php
$counter = Functions::memoize("fixtureCounter::increment");
$counter($foo); // 1
$counter($foo); // 1
$counter($bar); // 2
$counter($baz); // 3Creates and returns a new, throttled version of the passed function, that, when invoked repeatedly, will only actually call the original function at most once per every wait milliseconds. Useful for rate-limiting events that occur faster than you can keep up with.
{mixed} $source- source function{int} $wait- wait time in ms
throttle(mixed $source, int $wait)<?php
function increment()
{
static $count = 0;
return ++$count;
}
$func = Functions::throttle("increment", 20);
$func(); // 1
$func(); // false
usleep(20000);
$func(); // 2
$func(); // falseCreates and returns a new debounced version of the passed function which will postpone its execution until after wait milliseconds have elapsed since the last time it was invoked
{mixed} $source- source function{int} $wait- wait time in ms
debounce(mixed $source, int $wait)<?php
function increment()
{
static $count = 0;
return ++$count;
}
$func = Functions::debounce("increment", 20);
$func(); // false
$func(); // false
usleep(20000);
$func(); // 1
$func(); // falseCreates a version of the function that can only be called one time. Repeated calls to the modified function will have no effect, returning the value from the original call. Useful for initialization functions, instead of having to set a boolean flag and then check it later.
{mixed} $source- source function
once(mixed $source)<?php
function increment()
{
static $count = 0;
return ++$count;
}
$func = Functions::once("increment");
$func(); // 1
$func(); // 1
$func(); // 1Creates a version of the function that will only be run after being called count times. Useful for grouping asynchronous responses, where you want to be sure that all the async calls have finished, before proceeding.
{mixed} $source- source function{int} $count- count
after(mixed $source, int $count)<?php
function increment()
{
static $count = 0;
return ++$count;
}
$func = Functions::after("increment", 2);
$func(); // false
$func(); // false
$func(); // 1Creates a version of the function that can be called no more than count times. The result of the last function call is memoized and returned when count has been reached.
{mixed} $source- source function{int} $count- count
before(mixed $source, int $count)<?php
function increment()
{
static $count = 0;
return ++$count;
}
$func = Functions::before("increment", 2);
$func(); // 1
$func(); // 2
$func(); // 2Wraps the first function inside of the wrapper function, passing it as the first argument. This allows the transforming function to execute code before and after the function runs, adjust the arguments, and execute it conditionally.
{mixed} $source- source function{mixed} $transformer- transforming function
wrap(mixed $source, mixed $transformer)<?php
function increment()
{
static $count = 0;
return ++$count;
}
$func = Functions::wrap("increment", function($func){
return 10 + $func();
});
$func(); // 11Returns a new negated version of the predicate function.
{mixed} $source- source function
negate(mixed $source)<?php
$func = Functions::negate(function(){ return false; });
$func(): // trueReturns the composition of a list of functions, where each function consumes
the return value of the function that follows. In math terms,
composing the functions f(), g(), and h() produces f(g(h())).
{array} $functions- list of mixeds to compose
compose(...$functions)<?php
$greet = function(mixed $name){ return "hi: " . $name; };
$exclaim = function(mixed $statement){ return strtoupper($statement) . "!"; };
$welcome = Functions::compose($greet, $exclaim);
$welcome("moe"); // "hi: MOE!"Invokes the given iteratee function n times. Each invocation of iteratee is called with an index argument. Produces an array of the returned values.
{array} $source- source function{int} $n- (optional) invoke function N times{object} $context- (optional) context object to bind to
times(callable $source, int $n = 1, $context = null)<?php
$counter = 0;
Functions::times(function($value) use(&$counter){
$counter += $value;
}, 5); // 15Returns a wrapped object. Calling methods on this object will continue to return wrapped objects until value is called.
{mixed} $value- source mixed
chain(mixed $value): Functions<?php
$res = Strings::chain( " 12345 " )
->replace("/1/", "5")
->replace("/2/", "5")
->trim()
->substr(1, 3)
->value();
echo $res; // "534"