2017 © Pedro Peláez
 

library phln

A practical functional library for PHP developers

image

baethon/phln

A practical functional library for PHP developers

  • Thursday, June 7, 2018
  • by radmen
  • Repository
  • 2 Watchers
  • 9 Stars
  • 17 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 1 Open issues
  • 7 Versions
  • 31 % Grown

The README.md

Build Status Scrutinizer Code Quality, (*1)


baethon/phln

Set of small utility functions., (*2)

Heavily inspired by Ramda.js, adapted for PHP needs., (*3)

Installation

composer require baethon/phln

Example usage

use Baethon\Phln\Phln as P;

$aboveMinPoints = P::compose(P::lte(50), P::prop('score'));
$onlyPhp = P::pathEq('language.name', 'PHP');

$topScores = collect($users)
    ->filter(P::both($aboveMinPoints, $onlyPhp));

Note: in the docs P will be used as an alias to Baethon\Phln\Phln., (*4)

Currying

Phln methods are loosely curried. A N-ary method will return a function until all arguments are provided., (*5)

$foo = P::curryN(2, function ($left, $right) {
    return $left + $right;
});

$foo(1); // returns instance of \Closure
$foo(1, 2); // 3
$foo(1)(2); // 3

Partial application

Partial application is possible with combination of P::partial() and P::__ const. Partial returns a function which accepts arguments which should "fill" gap of missing arguments for callable., (*6)

$foos = [1, 2, 3];
$mapFoos = P::partial('\\array_map', [P::__, $foos]);
$mapFoos(function ($f) {
    return $f + 100;
}); // [100, 200, 300]

Function composition

For function composition phln provides pipe() and compose() functions., (*7)

$allFoos = P::pipe(
    P::filter(P::lte(5)),
    P::map(P::always('foo'))
);

$firstFoo = P::compose(P::head(), $allFoos);

$allFoos([4, 5, 6]); // ['foo', 'foo']
$firstFoo([4, 5, 6]); // 'foo'

Using methods as references

Some of phln methods accept callable as an argument., (*8)

To pass another macro as a reference call it without any arguments., (*9)

$collection = [1, 2, 3, 4];
P::reduce(P::sum(), $collection); // 10

Also, you can use P::raw() method wich returns uncurried macro, or pointer to Phln method., (*10)

Extending

Baethon\Phln\Phln is macroable. This means that it can be extened using macro() method:, (*11)

P::macro('foo', function () {
    return 'foo';
});

P::foo(); // 'foo'

Note about objects

The library takes terminology from Ramda. In most cases, it's perfectly fine, until one gets to the concept of object., (*12)

Ramda treats objects as dictionaries. In JavaScript, there's only one type which can act as a dictionary. It's ... object., (*13)

In PHP things get complicated. It's possible to use arrays and objects as dictionaries. This way Phln has to treat both of those types as an object., (*14)

For compatibility reason, all functions which return object will return array., (*15)

Testing

./vendor/bin/phpunit

The Versions

07/06 2018
23/05 2018
22/05 2018
24/04 2018

dev-objects-support

dev-objects-support

A practical functional library for PHP developers

  Sources   Download

MIT

The Development Requires

by Radoslaw Mejer

10/07 2017

1.1.0

1.1.0.0

A practical functional library for PHP developers

  Sources   Download

MIT

The Development Requires

by Radoslaw Mejer

07/07 2017

1.0.0

1.0.0.0

A practical functional library for PHP developers

  Sources   Download

MIT

The Development Requires

by Radoslaw Mejer