Download the PHP package apantle/fun-php without Composer
On this page you can find all versions of the php package apantle/fun-php. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download apantle/fun-php
More information about apantle/fun-php
Files in apantle/fun-php
Package fun-php
Short Description Functional Programming to enhance OOP PHP developer experience
License MIT
Informations about the package fun-php
fun-php
Functional Programming to enhance OOP PHP developer experience
Here you have the supported functions with a minimal example, there are more examples in the tests of the package:
compose
applies a par of functions, first the one to the right, then to the returned value applies the function to the left:
Example:
pipe
Returns a function that receives any number of callables, first one is passed an arbitrary number of arguments, then it passes the returned value to next one in the pipe, and so on. Next functions must be unary (only accept one argument).
Example with a simplified functional controller:
curryToUnary
Allow to partially apply any callable, returning an unary
function. Most useful in combination with pipe
to write
point-free style algorithms.
Passing in the list of arguments to bound, the constant
Apantle\FunPHP\_
the position of the argument expected
can be swapped. For example, you can build a Mappable collection
with array_map and the array to be mapped, and pass to this
collection a different mapper every time.
Examples:
constant
Returns a function that always return the value passed, useful for placeholders or composition with constant values but to avoid hardcoding values:
identity
useful for functor tests, always returns the passed value
head
gets the first item of an array
unfold
Returns a function that takes a single value, applies an array of transformations to it, and returns a an associative array with the same keys as the array of specs, with the value mapped by those functions.
Useful to take a single input and pass it to several services, then collecting the output in a single hashmap. A perfect dual to apantle/hashmapper.
Example:
If the function is unary, it passes only the input value.
In order to composer more complex algorithms easily, the mappers can receive a second argument, that apantle/hashmapper passes to every mapper automatically, being that the whole associative array being mapped.
Apart from the spec of transformations, it takes an optional argument that allows you to pass values through the mappers, it is recommended this argument to be an object to be passed by reference.
See the test sources for more examples.