2017 © Pedro PelĂĄez
 

library functional

Syntax enhancement operators of functional programming

image

martinezdelariva/functional

Syntax enhancement operators of functional programming

  • Sunday, June 18, 2017
  • by martinezdelariva
  • Repository
  • 3 Watchers
  • 7 Stars
  • 6 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 0 % Grown

The README.md

Functional

Build Status, (*1)

Syntax enhancement operators of functional programming:, (*2)

  • Curry
  • Pipe
  • Pattern Match
  • Fold

At the moment there are plans to include curry and pipe as PHP functions. Meanwhile you can use this library for fun!, (*3)

Notes: * curry doesn't know if a function with optional params won't be provided. So, curry only runs the function curried when all params are given. * currying vs partial function application Wikipedia, (*4)

Installation

Install it using Composer, (*5)

composer require martinezdelariva/functional

Usage

Importing as a single function:, (*6)

   namespace Foo;

   use function Martinezdelariva\Functional\pipe;

   class Bar
   {
        public function baz()
        {
            pipe(
                'trim',
                'ucwords'
            )(' string ');
        }
   }

Importing namespace:, (*7)

   namespace Foo;

   use Martinezdelariva\Functional as F;

   class Bar
   {
        public function baz()
        {
            F\pipe(
                'trim',
                'ucwords'
            )(' string ');
        }
   }

Functions

curry

function sum(int $one, int $two, int $three) : int {
    return $one + $two + $three;
};

// curry firsts params
$sum3 = curry('sum', 1, 2);
$sum3(3); // output: 6
$sum3(5); // output: 8

// curry whole function
$sum  = curry('sum');
$sum1 = $sum(1);
$sum1(2, 3); // output: 6

// curry with all params given
curry(sum::class, 1, 2, 3); // output: 6

curry_left

curry_left is an alias of curry., (*8)

curry_right

function minus(int $one, int $two) : int {
    return $one - $two;
};

$minus1 = curry_right('minus');
echo $minus1(1)(4); // output: 3

match

$patterns = [
    'a'              => "It's an 'a'",
    'foo'            => function ($input, $other) {
                            return "$input::$other";
                        },
    'is_int'         => function ($input) {
                            return $input + 1;
                        },
    \stdClass::class => "It's an 'stdClass'",
    _                => "Default"
];

// provinding params
match($patterns, 'a');          // output: "It's an 'a'"

// provinding one by one param due `match` is curried
$match = match($patterns);
$match(5);                      // output: 6
$match(new \stdClass());        // output: "It's an 'stdClass'"
$match('unknown');              // output: "Default"

// provinding param to callables with more params
$match('foo')('bar');           // output: foo::bar

Pipe

$format = pipe(
    'trim',
    'ucwords',
    function (string $names) : string {
        return implode(', ', explode(' ', $names));
    }
);

$format('  john mary andre'); // output: John, Mary, Andre

Fold

// example: using fold to sum integers
$sum = fold(function($item, $carry) {
   return $item + $carry;
}, 0);
$sum([1, 2, 3, 4]); // output 10

// example: dynamically changing the fold function
$match = match([
   'is_string'  =>  function($item, $carry) {
                        return $carry.$item;
                    },
   'is_int'     =>  function($item, $carry) {
                        return "$carry{X}";
                    },
]);

fold($match, '', ['a', 'b', 'c', 2, 'd', 'e']);  // output "abc{X}de"

The Versions

18/06 2017

dev-master

9999999-dev

Syntax enhancement operators of functional programming

  Sources   Download

MIT

The Requires

  • php >=7.1

 

The Development Requires

pipe functional curry pattern match

18/06 2017

v0.4-alpha

0.4.0.0-alpha

Syntax enhancement operators of functional programming

  Sources   Download

MIT

The Requires

  • php >=7.1

 

The Development Requires

pipe functional curry pattern match

11/06 2017

v0.3-alpha

0.3.0.0-alpha

Syntax enhancement operators of functional programming

  Sources   Download

MIT

The Requires

  • php >=7.1

 

The Development Requires

pipe functional curry pattern match

04/06 2017

v0.2-alpha

0.2.0.0-alpha

Syntax enhancement operators of functional programming

  Sources   Download

MIT

The Requires

  • php >=7.1

 

The Development Requires

pipe functional curry pattern match

23/04 2017

v0.1-alpha

0.1.0.0-alpha

Syntax enhancement operators of functional programming

  Sources   Download

MIT

The Requires

  • php >=7.1

 

The Development Requires

pipe functional curry pattern match