PHP code example of daveross / functional-programming-utils

1. Go to this page and download the library: Download daveross/functional-programming-utils library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

daveross / functional-programming-utils example snippets



de 'path/to/functional-programming-utils/src/curry.php';
;
hp';
nclude 'path/to/functional-programming-utils/src/Monads/Right.php';

use function DaveRoss\FunctionalProgrammingUtils\add as add;
$x = add( 5, 5 ); // 10

$x = DaveRoss\FunctionalProgrammingUtils\add( 5, 5 ); // 10

$x = DaveRoss\FunctionalProgrammingUtils\subtract( 10, 5 ); // 5

$x = DaveRoss\FunctionalProgrammingUtils\multiply( 5, 5 ); // 25

$x = DaveRoss\FunctionalProgrammingUtils\divide( 25, 5 ); // 5

$x = DaveRoss\FunctionalProgrammingUtils\modulus( 13, 5 ); // 3

$x = DaveRoss\FunctionalProgrammingUtils\inverse( 5 ); // -5

$x = DaveRoss\FunctionalProgrammingUtils\truthy( 5 ); // true
$x = DaveRoss\FunctionalProgrammingUtils\truthy( 0 ); // false

$x = DaveRoss\FunctionalProgrammingUtils\true( true ); // true
$x = DaveRoss\FunctionalProgrammingUtils\true( 5 ); // false

$x = DaveRoss\FunctionalProgrammingUtils\falsy( 0 ); // true
$x = DaveRoss\FunctionalProgrammingUtils\falsy( 5 ); // false

$x = DaveRoss\FunctionalProgrammingUtils\false( false ); // true
$x = DaveRoss\FunctionalProgrammingUtils\false( 0 ); // false

$x = DaveRoss\FunctionalProgrammingUtils\default_value( 5, 10); // 10
$x = DaveRoss\FunctionalProgrammingUtils\default_value( 5, null); // 5

$a = array( 'hello' => 'world', 'a' => 'b' );
$x = DaveRoss\FunctionalProgrammingUtils\array_prop( $a, 'hello'); // 'world'
$x = DaveRoss\FunctionalProgrammingUtils\array_prop( $a, 'test'); // null

$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

$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

$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

$add_five = DaveRoss\FunctionalProgrammingUtils\partially_apply( 'DaveRoss\FunctionalProgrammingUtils\add', 5 );
$x = $add_five( 5 ); // 10

$divide_by_five = DaveRoss\FunctionalProgrammingUtils\partially_apply_right( 'DaveRoss\FunctionalProgrammingUtils\divide', 5 );
$x = $divide_by_five( 25 ); // 5

  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

$backwards_and_uppercase = DaveRoss\FunctionalProgrammingUtils\compose( 'str_reverse', 'strtoupper' );
$x = $backwards_and_uppercase( 'dlrow olleh' ); // HELLO WORLD

$x = new Just( 5 );
$y = $x->map( function( $a ) { return $a * 5; } ); // Just(25)

$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)


$x = new Maybe( 5 );
$y = maybe(null, function( $a ) { return $a * 5; }, $x); // 25

$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" )

$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" )

$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
array_prop