|
| 1 | +[](https://packagist.org/packages/loophp/combinator) |
| 2 | + [](https://packagist.org/packages/loophp/combinator) |
| 3 | + [](https://packagist.org/packages/loophp/combinator) |
| 4 | + [](https://github.com/loophp/combinator/actions) |
| 5 | + [](https://scrutinizer-ci.com/g/loophp/combinator/?branch=master) |
| 6 | + [](https://scrutinizer-ci.com/g/loophp/combinator/?branch=master) |
| 7 | + [](https://stryker-mutator.github.io) |
| 8 | + [](https://packagist.org/packages/loophp/combinator) |
| 9 | + [](https://paypal.me/drupol) |
| 10 | + |
| 11 | +# Combinator |
| 12 | + |
| 13 | +This package provides a list of well known Combinators. |
| 14 | + |
| 15 | +A combinator is [a higher-order function](https://en.wikipedia.org/wiki/Higher-order_function) that uses only function |
| 16 | +application and earlier defined combinators to define a result from its arguments. |
| 17 | +It was introduced in 1920 by [Moses Schönfinkel](https://en.wikipedia.org/wiki/Moses_Sch%C3%B6nfinkel) and |
| 18 | +[Haskell Curry](https://en.wikipedia.org/wiki/Haskell_Curry), and has more recently been used in computer |
| 19 | +science as a theoretical model of computation and also as a basis for the design of [functional programming languages](https://en.wikipedia.org/wiki/Functional_programming_languages). |
| 20 | +Combinators which were introduced by Schönfinkel in 1920 with the idea of providing an analogous way to build up |
| 21 | +functions - and to remove any mention of variables - particularly in predicate logic. |
| 22 | + |
| 23 | +## Requirements |
| 24 | + |
| 25 | +* PHP >= 7.1.3 |
| 26 | + |
| 27 | +## Installation |
| 28 | + |
| 29 | +```shell script |
| 30 | +composer require loophp/combinator |
| 31 | +``` |
| 32 | + |
| 33 | +## Available combinators |
| 34 | + |
| 35 | +| Combinator | Definition | # Arguments | |
| 36 | +|------------|------------------------------------------|-------------| |
| 37 | +| A | `a => b => a(b)` | 2 | |
| 38 | +| B | `a => b => c => a(b(c))` | 3 | |
| 39 | +| C | `a => b => c => a(c)(b)` | 3 | |
| 40 | +| D | `a => b => c => d => a(b)(c(d))` | 4 | |
| 41 | +| E | `a => b => c => d => e => a(b)(c(d)(e))` | 5 | |
| 42 | +| F | `a => b => c => c(b)(a)` | 3 | |
| 43 | +| G | `a => b => c => d => a(d)(b(c))` | 4 | |
| 44 | +| H | `a => b => c => a(b)(c)(b)` | 3 | |
| 45 | +| I | `a => a` | 1 | |
| 46 | +| J | `a => b => c => d => a(b)(a(d)(c))` | 4 | |
| 47 | +| K | `a => b => a` | 2 | |
| 48 | +| L | `a => b => a(b(b))` | 2 | |
| 49 | +| M | `a => a(a)` | 1 | |
| 50 | +| O | `a => b => b(a(b))` | 2 | |
| 51 | +| Psi | `a => b => c => d => a((b(c))(b(d)))` | 4 | |
| 52 | +| Q | `a => b => c => b(a(c))` | 3 | |
| 53 | +| R | `a => b => c => b(c)(a)` | 3 | |
| 54 | +| S | `a => b => c => a(c)(b(c))` | 3 | |
| 55 | +| T | `a => b => b(a)` | 2 | |
| 56 | +| U | `a => b => b(a(a)(b))` | 2 | |
| 57 | +| V | `a => b => c => c(a)(b)` | 3 | |
| 58 | +| W | `a => b => a(b)(b)` | 2 | |
| 59 | +| Y | `a => (b => b(b))(b => a(c => b(b)(c)))` | 1 | |
| 60 | + |
| 61 | +Example with the B combinator: |
| 62 | + |
| 63 | +The definition means that the combinator needs 3 arguments: `a`, `b` and `c`. |
| 64 | +The return of this combinator is the result of the function `a` applied to the result of the function `b` applied to `c`. |
| 65 | + |
| 66 | +PHP will first evaluate `b(c)` and will then pass the result to the function `a`, this is what the definition means: `a(b(c))` |
| 67 | + |
| 68 | +## Usage |
| 69 | + |
| 70 | +Example with [the Y combinator](https://en.wikipedia.org/wiki/Fixed-point_combinator). |
| 71 | + |
| 72 | +```php |
| 73 | +<?php |
| 74 | + |
| 75 | +declare(strict_types=1); |
| 76 | + |
| 77 | +include 'vendor/autoload.php'; |
| 78 | + |
| 79 | +use loophp\combinator\Y; |
| 80 | + |
| 81 | +$fibonacci = new Y( |
| 82 | + static function ($f) { |
| 83 | + return static function ($n) use ($f) { |
| 84 | + return (1 >= $n) ? $n : ($f($n - 1) + $f($n - 2)); |
| 85 | + }; |
| 86 | + } |
| 87 | +); |
| 88 | + |
| 89 | +$result = $fibonacci()(10); |
| 90 | +``` |
| 91 | + |
| 92 | +## Further reading |
| 93 | + |
| 94 | +- [To Mock a Mockingbird](https://en.wikipedia.org/wiki/To_Mock_a_Mockingbird) |
| 95 | +- [http://dkeenan.com/Lambda/](http://dkeenan.com/Lambda/) |
| 96 | +- https://gist.github.com/Avaq/1f0636ec5c8d6aed2e45 |
| 97 | +- https://en.wikipedia.org/wiki/Combinatory_logic |
| 98 | +- https://github.com/sanctuary-js/sanctuary |
| 99 | +- https://en.wikipedia.org/wiki/Lambda_calculus |
| 100 | + |
| 101 | +## Authors |
| 102 | + |
| 103 | +* Pol Dellaiera |
0 commit comments