Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/baethon/phln
Set of small utility functions. Inspired by Ramda 💜
https://github.com/baethon/phln
fp function-php functional-programming php
Last synced: 4 days ago
JSON representation
Set of small utility functions. Inspired by Ramda 💜
- Host: GitHub
- URL: https://github.com/baethon/phln
- Owner: baethon
- License: mit
- Created: 2017-04-27T16:02:55.000Z (over 7 years ago)
- Default Branch: 3.x
- Last Pushed: 2020-09-27T20:55:24.000Z (over 4 years ago)
- Last Synced: 2024-09-19T22:53:02.879Z (3 months ago)
- Topics: fp, function-php, functional-programming, php
- Language: PHP
- Homepage: https://baethon.github.io/phln/
- Size: 1.58 MB
- Stars: 18
- Watchers: 3
- Forks: 2
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
[![Build Status](https://travis-ci.org/baethon/phln.svg?branch=master)](https://travis-ci.org/baethon/phln) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/baethon/phln/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/baethon/phln/?branch=master)
---
# baethon/phln
Set of small utility functions.
Heavily inspired by [Ramda.js](http://ramdajs.com/), adapted for PHP needs.
## Installation
```bash
composer require baethon/phln
```## Example usage
```php
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.
```php
$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.
```php
$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.
```php
$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.
```php
$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:
```php
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
```bash
./vendor/bin/phpunit
```