dev-master
9999999-devA minimal functional programming library for PHP
MIT
The Requires
- php >=5.3.3
- chh/itertools *@dev
A minimal functional programming library for PHP
Funk is a minimal functional library for PHP., (*1)
It's a collection of some things, I've felt the need for in many projects:, (*2)
Funk\Func
).Funk\Collection
).Funk\Type
).Install with Composer:, (*3)
php composer.phar require chh/funk:*@dev
Then require vendor/autoload.php
in your application., (*4)
The Funk\Collection
class wraps a PHP iterable value and provides
methods to manipulate it. The collection provides nearly all operations
using Iterators and exposes this iterator via getIterator
(also
implements IteratorAggregate
). That means that nearly all operations
are lazy evaluated., (*5)
You can see this in action, if you wrap a PDO statement in a collection
and then call map
. This wraps the PDO Statement in a special
"MappingIterator", which applies the callback function on each element
and yields the callback's return value. But this happens only when the
"MappingIterator" is iterated, until then nothing happens., (*6)
Example:, (*7)
<?php use Funk\Collection, Funk\Collection\Operator as op; echo (new Collection(range(0, 20))) ->keep(function($x) { return $x > 5; }) ->remove(op::gt(7)) ->map(function($val) { return strval($val); }) ->join(' ');
Everywhere where the argument is predicate
the operation takes several
different values with different characteristics:, (*8)
Operator
class.
Instances can be conveniently created by calling the operator as
static method on this class. Valid operators include: is
, eq
, gt
, gte
, lt
and lte
.===
.$value == true
.Example:, (*9)
<?php use Funk\Type; class Foo { function __toInt() { return 42; } } echo Type::intval(new Foo) + 1; # Output: # 43
A minimal functional programming library for PHP
MIT