Download the PHP package daveross/functional-programming-utils without Composer
On this page you can find all versions of the php package daveross/functional-programming-utils. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download daveross/functional-programming-utils
More information about daveross/functional-programming-utils
Files in daveross/functional-programming-utils
Package functional-programming-utils
Short Description Functional Programming utilities for PHP 5.4+
License MIT
Homepage https://github.com/daveross/functional-programming-utils
Informations about the package functional-programming-utils
PHP Functional Programming Utils
Functional Programming utilities for PHP 5.4+
Table of Contents
- Installation
- Using composer
- Manually
- License
- Contributing
- Further reading & suggested viewing
- Functional PHP talk at PHP Hampshire Feb 2014
- A note for PHP 5.6+ users
- Features
- Mathematical functions
- Property Access
- Memoization
- Currying
- Composition
- Monads
- Release History
Installation
Using composer
Put the require statement for functional-programming-utils
in your composer.json
file and run composer install
or php composer.phar install
:
Manually
Include all the files in the src
directory, or individual files as needed:
License
See why I contribute to open source software.
Contributing
Pull requests are welcome. Unit tests are encouraged but not required.
Further reading & suggested viewing
- Professor Frisby's Mostly Adequate Guide to Functional Programming
- Functional Programming in PHP
- Learn You a Haskell for Great Good to see how a language built for Functional Programming works
Simon Holywell's Functional PHP talk at PHP Hampshire Feb 2014
A note for PHP 5.6+ users
Starting in PHP 5.6, you can at the top of a file to reference that function without typing its whole name, including the namespace. I encourage you to try it.
Features
Mathematical functions
add
Adds two values
subtract
Subtracts two values
multiply
Multiplies two numbers
divide
Divides two numbers
modulus
Computes the remainder after division
inverse
Inverts a number
truthy
Checks if a value evaluates to using standard PHP rules
true
Checks if a value is boolean true
falsy
Checks if a value evaluates to using standard PHP rules
false
Checks if a value is boolean false
default_value
Returns a value, or a default if the value is null
Property Access
array_prop
Returns a value from an array given the corresponding key, or null if the key doesn't exist in the array
object_prop
Returns a value from an object given the corresponding property name, or null if the property doesn't exist in the object
prop
Calls or as appropriate
Memoization
memoize
Wraps a function in a layer that stores the function's return value for every set of parameters it's called with, so the function doesn't need to be called again the next time it's called with the same parameters
Currying
See Curry or Partial Application? The Difference Between Partial Application and Curry for details on how these functions differ.
partially_apply
Partially applies a function. Given a function that takes more than one parameter, returns a function that already knows the first parameter.
partially_apply_right
Partially applies a function. Given a function that takes more than one parameter, returns a function that already knows the last parameter.
curry
Curries a function. Given a function that takes more than one parameter, applies a single parameter to it and returns a function that takes the next parameter until all required parameters are provided.
Composition
compose
Creates a new function consisting of a series of functions that each take one parameter. When the new function is called, that series of functions is called from right to left, processing the result of the previous function.
Monads
Monad
Abstract parent class for Monads. A Monad is a class that wraps a single value and implements function map(callable $f)
.
Monad::map() returns another Monad wrapping the function's return value. See the Monad.
Just
The Monad "just" wraps a value and maps functions to it.
Maybe
The Maybe Monad recognizes when it's holding a value and returns when a function is mapped to it. Otherwise, it behaves like a Just Monad.
maybe function
May be used to extract the value from a Maybe Monad.
Either
To implement conditionals, a function can be defined as returning either one value or another. This is represented with the Either Monad and its children, and .
Left
The Left Monad wraps an error value from an unsuccessful function call.
Right
The Right Monad wraps the result of a successful function call.
either function
May be used to extract the value from either a Left Monad or a Right Monad.