Functional Programming utilities for PHP 5.4+
- Installation
- License
- Contributing
- Further reading & suggested viewing
- A note for PHP 5.6+ users
- Features
- Release History
Put the require statement for functional-programming-utils
in your composer.json
file and run composer install
or php composer.phar install
:
{
"require": {
"daveross/functional-programming-utils": "~4.0"
}
}
Include all the files in the src
directory, or individual files as needed:
<?php
include 'path/to/functional-programming-utils/src/compose.php';
include 'path/to/functional-programming-utils/src/curry.php';
include 'path/to/functional-programming-utils/src/math.php';
include 'path/to/functional-programming-utils/src/memoize.php';
include 'path/to/functional-programming-utils/src/prop.php';
include 'path/to/functional-programming-utils/src/Monads/Monad.php';
include 'path/to/functional-programming-utils/src/Monads/Just.php';
include 'path/to/functional-programming-utils/src/Monads/Maybe.php';
include 'path/to/functional-programming-utils/src/Monads/Either.php';
include 'path/to/functional-programming-utils/src/Monads/Left.php';
include 'path/to/functional-programming-utils/src/Monads/Right.php';
See why I contribute to open source software.
Pull requests are welcome. Unit tests are encouraged but not required.
- 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
Starting in PHP 5.6, you can use function
at the top of a file to reference that function without typing its whole name, including the namespace. I encourage you to try it.
use function DaveRoss\FunctionalProgrammingUtils\add as add;
$x = add( 5, 5 ); // 10
Adds two values
$x = DaveRoss\FunctionalProgrammingUtils\add( 5, 5 ); // 10
Subtracts two values
$x = DaveRoss\FunctionalProgrammingUtils\subtract( 10, 5 ); // 5
Multiplies two numbers
$x = DaveRoss\FunctionalProgrammingUtils\multiply( 5, 5 ); // 25
Divides two numbers
$x = DaveRoss\FunctionalProgrammingUtils\divide( 25, 5 ); // 5
Computes the remainder after division
$x = DaveRoss\FunctionalProgrammingUtils\modulus( 13, 5 ); // 3
Inverts a number
$x = DaveRoss\FunctionalProgrammingUtils\inverse( 5 ); // -5
Checks if a value evaluates to true
using standard PHP rules
$x = DaveRoss\FunctionalProgrammingUtils\truthy( 5 ); // true
$x = DaveRoss\FunctionalProgrammingUtils\truthy( 0 ); // false
Checks if a value is boolean true
$x = DaveRoss\FunctionalProgrammingUtils\true( true ); // true
$x = DaveRoss\FunctionalProgrammingUtils\true( 5 ); // false
Checks if a value evaluates to false
using standard PHP rules
$x = DaveRoss\FunctionalProgrammingUtils\falsy( 0 ); // true
$x = DaveRoss\FunctionalProgrammingUtils\falsy( 5 ); // false
Checks if a value is boolean false
$x = DaveRoss\FunctionalProgrammingUtils\false( false ); // true
$x = DaveRoss\FunctionalProgrammingUtils\false( 0 ); // false
Returns a value, or a default if the value is null
$x = DaveRoss\FunctionalProgrammingUtils\default_value( 5, 10); // 10
$x = DaveRoss\FunctionalProgrammingUtils\default_value( 5, null); // 5
Returns a value from an array given the corresponding key, or null if the key doesn't exist in the array
$a = array( 'hello' => 'world', 'a' => 'b' );
$x = DaveRoss\FunctionalProgrammingUtils\array_prop( $a, 'hello'); // 'world'
$x = DaveRoss\FunctionalProgrammingUtils\array_prop( $a, 'test'); // null
Returns a value from an object given the corresponding property name, or null if the property doesn't exist in the object
$o = new stdClass();
$o->hello = 'world';
$o->a = 'b';
$x = DaveRoss\FunctionalProgrammingUtils\object_prop( $o, 'hello'); // 'world'
$x = DaveRoss\FunctionalProgrammingUtils\object_prop( $o, 'test'); // null
Calls array_prop
or object_prop
as appropriate
$a = array( 'hello' => 'world', 'a' => 'b' );
$x = DaveRoss\FunctionalProgrammingUtils\prop( $a, 'hello'); // 'world'
$x = DaveRoss\FunctionalProgrammingUtils\prop( $a, 'test'); // null
$o = new stdClass();
$o->hello = 'world';
$o->a = 'b';
$x = DaveRoss\FunctionalProgrammingUtils\prop( $o, 'hello'); // 'world'
$x = DaveRoss\FunctionalProgrammingUtils\prop( $o, 'test'); // null
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
$f = DaveRoss\FunctionalProgrammingUtils\memoize(function($a) { return $a; });
$x = $f(5); // 5
$x = $f(5); // 5 again, but the function didn't need to be called a second time
See Curry or Partial Application? The Difference Between Partial Application and Curry for details on how these functions differ.
Partially applies a function. Given a function that takes more than one parameter, returns a function that already knows the first parameter.
$add_five = DaveRoss\FunctionalProgrammingUtils\partially_apply( 'DaveRoss\FunctionalProgrammingUtils\add', 5 );
$x = $add_five( 5 ); // 10
Partially applies a function. Given a function that takes more than one parameter, returns a function that already knows the last parameter.
$divide_by_five = DaveRoss\FunctionalProgrammingUtils\partially_apply_right( 'DaveRoss\FunctionalProgrammingUtils\divide', 5 );
$x = $divide_by_five( 25 ); // 5
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.
function add_three_integers($a, $b, $c) {
return intval( $a ) + intval( $b ) + intval( $c );
}
$fn = DaveRoss\FunctionalProgrammingUtils\curry( 'add_three_integers' , 1 );
$fn2 = $fn( 2 );
$x = $fn2( 3 ); // 6
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.
$backwards_and_uppercase = DaveRoss\FunctionalProgrammingUtils\compose( 'str_reverse', 'strtoupper' );
$x = $backwards_and_uppercase( 'dlrow olleh' ); // HELLO WORLD
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 Just
Monad.
The Just
Monad "just" wraps a value and maps functions to it.
$x = new Just( 5 );
$y = $x->map( function( $a ) { return $a * 5; } ); // Just(25)
The Maybe Monad recognizes when it's holding a null
value and returns Maybe( null )
when a function is mapped to it. Otherwise, it behaves like a Just Monad.
$x = new Maybe( 5 );
$y = $x->map( function( $a ) { return $a * 5; } ); // Maybe(25)
$a = new Maybe( null );
$b = $a->map( function( $a ) { return $a * 5; } ); // Maybe(null)
May be used to extract the value from a Maybe Monad.
$x = new Maybe( 5 );
$y = maybe(null, function( $a ) { return $a * 5; }, $x); // 25
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, Left
and Right
.
The Left Monad wraps an error value from an unsuccessful function call.
$f = function($a) {
return ( $a < 10 ) : Left::of( 'too low' ) : Right::of( $a + 1 );
}
$x = $f( 15 ); // Right( 16 )
$y = $f( 5 ); // Left( "too low" )
The Right Monad wraps the result of a successful function call.
$f = function($a) {
return ( $a < 10 ) : Left::of( 'too low' ) : Right::of( $a + 1 );
}
$x = $f( 15 ); // Right( 16 )
$y = $f( 5 ); // Left( "too low" )
May be used to extract the value from either a Left Monad or a Right Monad.
$x = Left::of( 5 );
$y = Right::of( 7 );
$left_handler = function( $a ) { return $a * 2; };
$right_handler = function( $a ) { return $a * 3; };
$a = either($left_handler, $right_handler, $x); // 10
$b = either($left_handler, $right_handler, $y); // 21