Git Product home page Git Product logo

phln's Introduction

Build Status Scrutinizer Code Quality


baethon/phln

Set of small utility functions.

Heavily inspired by Ramda.js, adapted for PHP needs.

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.

Currying

Phln methods are loosely curried. A N-ary method will return a function until all arguments are provided.

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

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

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

To pass another macro as a reference call it without any arguments.

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

Extending

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

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.

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

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.

For compatibility reason, all functions which return object will return array.

Testing

./vendor/bin/phpunit

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.