Fork me on GitHub
fun-php  1.2
functional programming utilities for PHP
API Documentation On Github Functional programming with PHP
Static Public Member Functions | Public Attributes | List of all members
boehm_s\F Class Reference

Static Public Member Functions

static filter (... $args)
 
static partition (... $args)
 
static each (... $args)
 
static map (... $args)
 
static flatMap (... $args)
 
static find (... $args)
 
static findIndex (... $args)
 
static some (... $args)
 
static every (... $args)
 
static sort (... $args)
 
static reverse (... $args)
 
static reduce (... $args)
 
static includes (... $args)
 
static prop (... $args)
 
static props (... $args)
 
static propOr (... $args)
 
static pick (... $args)
 
static uniq (... $args)
 
static uniqBy (... $args)
 
static splitAt (... $args)
 
static merge ($obj,... $objs)
 
static propEq (... $args)
 
static propSatisfies (... $args)
 
static pipe (... $fns)
 
static compose (... $fns)
 
static partial (... $args)
 
static not (... $args)
 

Public Attributes

const _ = '@@fun-php/placeholder'
 F::_ is a special placeholder value used to specify "gaps" within curried functions, allowing partial application of any combination of arguments, regardless of their positions. More...
 

Detailed Description

This class contains all the methods of the fun-php library. These "methods" are all static, so these are just functions.

Member Function Documentation

◆ compose()

static boehm_s\F::compose (   $fns)
static

Performs right-to-left function composition. Like the unix pipe (|), but reversed ! All the function must be unary.

((y → z), (x → y), ... ,(a → b)) → (a → z)
$sayHello = function($name) { return "Hello $name"; };
$yellHello = F::compose('strtoupper', $sayHello);
$yellHello('Punky'); //=> HELLO PUNKY
Parameters
callable...$fns
Returns
callable

◆ each()

static boehm_s\F::each (   $args)
static

Iterate over an iterable, calling a provided function $fn for each element. Returns the original array.

(a → *) → [a] → [a]
$isEven = function($n) { return $n % 2 == 0; };
F::each($isEven, [1, 2, 3, 4, 5]); //=> [1, 2, 3, 4, 5]
// has no effect, returns the original input, good for logging / debugging
Parameters
callable$fn
iterable$arr
Returns
iterable

◆ every()

static boehm_s\F::every (   $args)
static

Takes a predicate and a iterable and returns true if all of the iterable members satisfies the predicate.

((a, i, [a]) → Bool) → [a] → Bool
$equals2 = function($n) { return $n === 2; };
$moreThan0 = function($n) { return $n > 0; };
F::every($equals2, [1, 2, 3, 4, 5]); //=> false
F::every($moreThan0, [1, 2, 3, 4, 5]); //=> true
Parameters
callable$predicate
iterable$arr
Returns
bool

◆ filter()

static boehm_s\F::filter (   $args)
static

Takes a predicate and a iterable and returns an array containing the members of the given iterable which satisfy the given predicate.

((a, i, [a]) → Bool) → [a] → [a]
$isEven = function($n) { return $n % 2 == 0; };
F::filter($isEven, [1, 2, 3, 4, 5]); //=> [2, 4]
Parameters
callable$predicate
iterable$arr
Returns
array

◆ find()

static boehm_s\F::find (   $args)
static

Returns the first element of the list which matches the predicate, or null if no element matches.

((a, i, [a]) → Bool) → [a] → a
$moreThan5 = function($n) { return $n > 5; };
$fifth = function($_, $i) { return $i === 4; };
F::find($moreThan5, [1, 2, 3, 4, 5]); //=> null
F::find($fifth, [1, 2, 3, 4, 5]); //=> 5

T

Parameters
callable$predicate
iterable<T>$arr
Returns
T

◆ findIndex()

static boehm_s\F::findIndex (   $args)
static

Returns the index of the first element of the list which matches the predicate, or null if no element matches.

((a, i, [a]) → Bool) → [a] → i
$moreThan2 = function($n) { return $n > 2; };
F::findIndex($moreThan2, [1, 2, 3, 4, 5]); //=> 2
Parameters
callable$predicate
iterable$arr
Returns
int | string

◆ flatMap()

static boehm_s\F::flatMap (   $args)
static

Takes a function and a iterable, apply the function to each of the iterable value and then flatten the result.

((a, i, [a]) → [b]) → [a] → [b]
$pairWithIdx = function($n, $i) { return [$i, $n]; };
F::flatMap($pairWithIdx, [1, 2, 3, 4, 5]); //=> [0, 1, 1, 2, 2, 3, 3, 4, 4, 5]
Parameters
callable$fn
iterable$arr
Returns
array

◆ includes()

static boehm_s\F::includes (   $args)
static

Takes a value and an array. Returns true if the value is in the array and false otherwise.

a → [a] → Bool
F::includes(4, [1, 2, 3, 4, 5]); //=> true
F::includes(8, [1, 2, 3, 4, 5]); //=> false
Parameters
mixed$needle
array$haystack
Returns
bool

◆ map()

static boehm_s\F::map (   $args)
static

Takes a function and a iterable and returns an array containing the results of function applied to each iterable values.

((a, i, [a]) → b) → [a] → [b]
$square = function($n) { return $n * $n; };
F::map($isEven, [1, 2, 3, 4, 5]); //=> [1, 4, 9, 16, 25]
Parameters
callable$fn
iterable$arr
Returns
array

◆ merge()

static boehm_s\F::merge (   $obj,
  $objs 
)
static

Takes an (associative) array and at least one other (variadic on the second argument) and returns all these arrays merged together.

{k: v} → ({k: v}, ..., {k: v}) → {k: v}
F::merge(['x' => 42, 'y' => 21], ['z' => 2]); //=> ['x' => 42, 'y' => 21, 'z' => 2]
F::merge(['x' => 42], ['y' => 21], ['z' => 2]); //=> ['x' => 42, 'y' => 21, 'z' => 2]
F::merge(['x' => 42, 'y' => 21], ['x' => 'HAHA', 'z' => 2]); //=> ['x' => 'HAHA', 'y' => 21, 'z' => 2]
Parameters
array$array1
array...$array2
Returns
array

◆ not()

static boehm_s\F::not (   $args)
static

Takes a value and returns the it's ! (NOT Logical operator). Returns true when passed a falsy value, and false when passed a truthy one.

* → Bool
F::not(true); //=> false
F::not(false); //=> true
F::not('Punky'); //=> false
Parameters
mixed$value
Returns
bool

◆ partial()

static boehm_s\F::partial (   $args)
static

Takes a function and an array of arguments. Applies the arguments to the function and returns a new function awaiting the rest of the arguments.

((a, b, ..., n) → x) → [a, b, ...] → ((d, e, ..., n) → x)
$greet = function($salutation, $title, $name) { return "$salutation $title $name !"; };
$greetDoctor = F::partial($greet, ['Hello', 'Dr.']);
$greetDoctor('Jekyll'); //=> Hello Dr. Jekyll !
Parameters
callable$fn
array$args
Returns
callable

◆ partition()

static boehm_s\F::partition (   $args)
static

Takes a predicate and a list and returns the pair of elements which do and do not satisfy the predicate, respectively.

((a, i, [a]) → Bool) → [a] → [[a], [a]]
$isEven = function($n) { return $n % 2 == 0; };
F::partition($isEven, [1, 2, 3, 4, 5]); //=> [[2, 4], [1, 3, 5]]
Parameters
callable$predicate
iterable$arr
Returns
array

◆ pick()

static boehm_s\F::pick (   $args)
static

Takes a list of properties and an (associative) array. Returns a partial copy of the (associative) array containing only the keys specified.

[k] → {k: v} → {k: v} | null
F::pick(['x', 'z'], ['x' => 42, 'y' => 21, 'z' => 2]); //=> ['x' => 42, 'z' => 2]
F::pick(['x', 'a', 'b'], ['x' => 42, 'y' => 21, 'z' => 2]); //=> ['x' => 42]
Parameters
array$props
array$array
Returns
array

◆ pipe()

static boehm_s\F::pipe (   $fns)
static

Performs left-to-right function composition. Like the unix pipe (|). All the function must be unary.

((a → b), (b → c), ... , (y → z)) → (a → z)
$isEven = function($n) { return $n % 2 == 0; };
$square = function($n) { return $n * $n; };
$addReducer = function($acc, $val) { return $acc + $val; };
F::filter($isEven), //=> [2, 4]
F::map($square), //=> [4, 16]
F::reduce($addReducer, 0) //=> 20
)([1, 2, 3, 4, 5]); //=> 20
Parameters
callable...$fns
Returns
callable

◆ prop()

static boehm_s\F::prop (   $args)
static

Takes a property and an array and returns the array's property value.

k → {k: v} → v | null
F::prop('x', ['x' => 42]); //=> 42
F::prop('y', ['x' => 42]); //=> null
F::prop(1, [42, 21]); //=> 21
Parameters
string  |  int$prop
array$array
Returns
mixed

◆ propEq()

static boehm_s\F::propEq (   $args)
static

Takes a property, a value and an (associative) array. Returns true if the specified array property is equal to the supplied value and false otherwise.

k → v → {k: v} → Bool
F::propEq('x', 42, ['x' => 42]); //=> true
F::propEq('x', 666, ['x' => 42]); //=> false
$travels = [['type' => 'train', 'id' => 11], ['type' => 'flight', 'id' => 42], ['type' => 'hotel', 'id' => 45]];
F::find(F::propEq('type', 'flight'), $travels); //=> ['type' => 'flight', 'id' => 42]
Parameters
string$prop
mixed$value
array$array
Returns
bool

◆ propOr()

static boehm_s\F::propOr (   $args)
static

Takes a property, an array and a default value. Returns the array's property value if it exists and the default value otherwise.

k → d → {k: v} → v | d
F::propOr('x', 666, ['x' => 42]); //=> 42
F::propOr('y', 666, ['x' => 42]); //=> 666
F::propOr(42, ['foo' => 'bar'], [42, 21]); //=> ['foo' => 'bar']
Parameters
string  |  int$prop
mixed$default
array$array
Returns
mixed

◆ props()

static boehm_s\F::props (   $args)
static

Acts as multiple prop array of keys in, array of values out. Preserves order.

[k] → {k: v} → [v]
F::props(['x', 'y'], ['x' => 42, 'y' => 'foo']); //=> [42, 'foo']
F::props(['y', 'z'], ['x' => 42, 'y' => 'foo']); //=> ['foo', null]
F::props([1], [42, 21]); //=> [21]
Parameters
array$props
array$array
Returns
array

◆ propSatisfies()

static boehm_s\F::propSatisfies (   $args)
static

Takes a predicate, a property and an (associative) array. Returns true if the specified array property matches the predicate and false otherwise.

(v → Bool) → k → {k: v} → Bool
F::propSatisfies(function($x) { return $x > 1; }, 'p', ['p' => 42]); //=> true
$travels = [['type' => 'train', 'id' => 11], ['type' => 'flight', 'id' => 42], ['type' => 'hotel', 'id' => 45]];
$isFlightOrTrain = F::includes(F::_, ['train', 'flight']);
F::filter(F::propSatisfies($isFlightOrTrain, 'type'), $travels); //=> [['type' => 'train', 'id' => 11], ['type' => 'flight', 'id' => 42]]
Parameters
callable$predicate
mixed$prop
array$array
Returns
bool

◆ reduce()

static boehm_s\F::reduce (   $args)
static

Takes an iterable, a function and a starting (or default) value. Reduces the iterable to a single value by successively calling the function, passing it an accumulator value and the current value from the iterable, and then passing the result to the next call.

((a, b) → a) → a → [b] → a
$addReducer = function($acc, $val) { return $acc + $val; };
$sum = F::reduce($addReducer, 0); // fun-php functions are automatically curried
$sum([1, 2, 3, 4, 5]); //=> 15 (1 + 2 + 3 + 4 + 5)
Parameters
callable$fn
mixed$arr
iterable$arr
Returns
mixed

◆ reverse()

static boehm_s\F::reverse (   $args)
static

Takes an array (NO OBJECTS) and returns a reversed copy of the array.

[a] → [a]
F::reverse([1, 2, 3, 4, 5]); //=> [5, 4, 3, 2, 1]
// Warning : Does not work with objects !
Parameters
array$arr
Returns
array

◆ some()

static boehm_s\F::some (   $args)
static

Takes a predicate and a iterable and returns true if one of the iterable members satisfies the predicate.

((a, i, [a]) → Bool) → [a] → Bool
$equals2 = function($n) { return $n === 2; };
$moreThan5 = function($n) { return $n > 5; };
F::some($equals2, [1, 2, 3, 4, 5]); //=> true
F::some($moreThan5, [1, 2, 3, 4, 5]); //=> false
Parameters
callable$predicate
iterable$arr
Returns
bool

◆ sort()

static boehm_s\F::sort (   $args)
static

Takes a comparison function and an array (NO OBJECTS) and return a copy of the array sorted according to the comparison function.

((a, a) → Bool) → [a] → [a]
$ascNum = function($n1, $n2) { return $n1 - $n2; };
F::sort($ascNum, [4, 5, 3, 1, 2]); //=> [1, 2, 3, 4, 5]
// Warning : When working with objects, they will be modified !
Parameters
callable$fn
array$arr
Returns
array

◆ splitAt()

static boehm_s\F::splitAt (   $args)
static

Splits a given list at a given index.

Number → [a] → [[a], [a]]
F::splitAt(2, [1, 2, 3, 4, 5]); //=> [[1, 2], [3, 4, 5]]
F::splitAt(0, [1, 2, 3]); //=> [[], [1, 2, 3]]
Parameters
int$fn
array$array
Returns
array

◆ uniq()

static boehm_s\F::uniq (   $args)
static

Takes an array and returns a new array containing only one copy of each element in the original one. Warning : re-indexes the array.

[a] → [a]
F::uniq([1, 3, 2, 3, 4, 1, 5, 3, 4, 2]); //=> [1, 3, 2, 4, 5]
$arr = ['a' => 1, 'b' => 3, 'c' => 2, 'd' => 3, 'e' => 4, 'f' => 1, 'g' => 5, 'h' => 3, 'i' => 4, 'j' => 2];
F::uniq($arr); //=> [1, 3, 2, 4, 5]
// WARNING : F::uniq re-indexes arrays
Parameters
array$array
Returns
array

◆ uniqBy()

static boehm_s\F::uniqBy (   $args)
static

Returns a new list containing only one copy of each element in the original list, based upon the value returned by applying the supplied function to each list element. Prefers the first item if the supplied function produces the same value on two items.

(a → b) → [a] → [a]
$arr = [
[ 'a' => 4, 'b' => 8],
[ 'a' => 15, 'b' => 16],
[ 'a' => 4, 'b' => 23],
];
F::uniqBy(F::prop('a'), $arr); //=> [[ 'a' => 4, 'b' => 8], [ 'a' => 15, 'b' => 16]]
Parameters
callable$fn
array$array
Returns
array

Member Data Documentation

◆ _

const boehm_s\F::_ = '@@fun-php/placeholder'

F::_ is a special placeholder value used to specify "gaps" within curried functions, allowing partial application of any combination of arguments, regardless of their positions.

If g is a curried ternary function and _ is F::_, the following are equivalent:

_
g(1, 2, 3);
g(_, 2, 3)(1)
g(_, _, 3)(1)(2);
g(_, 2, _)(1, 3);
g(_, 2)(1)(3);
g(_, 2)(1, 3);
g(_, 2)(_, 3)(1);

The documentation for this class was generated from the following file: