|
| 1 | +MonadPHP |
| 2 | +======== |
| 3 | + |
| 4 | +This is a basic Monad library for PHP. |
| 5 | + |
| 6 | +Usage |
| 7 | +===== |
| 8 | + |
| 9 | +Values are "wrapped" in the monad via either the constructor: `new MonadPHP\Identity($value)` or the `unit()` method on an existing instance: `$monad->unit($value);` |
| 10 | + |
| 11 | +Functions can be called on the wrapped value using `bind()`: |
| 12 | + |
| 13 | + use MonadPHP\Identity; |
| 14 | + $monad = new Identity(1); |
| 15 | + $monad->bind(function($value) { var_dump($value); }); |
| 16 | + // Prints int(1) |
| 17 | + |
| 18 | +All calls to bind return a new monad instance wrapping the return value of the function. |
| 19 | + |
| 20 | + use MonadPHP\Identity; |
| 21 | + $monad = new Identity(1); |
| 22 | + $monad->bind(function($value) { |
| 23 | + return 2 * $value; |
| 24 | + })->bind(function($value) { |
| 25 | + var_dump($value); |
| 26 | + }); |
| 27 | + // Prints int(2) |
| 28 | + |
| 29 | +Additionally, "extracting" the raw value is supported as well (since this is PHP and not a pure functional language)... |
| 30 | + |
| 31 | + use MonadPHP\Identity; |
| 32 | + $monad = new Identity(1); |
| 33 | + var_dump($monad->extract()); |
| 34 | + // Prints int(1) |
| 35 | + |
| 36 | +Maybe Monad |
| 37 | +=========== |
| 38 | + |
| 39 | +One of the first useful monads, is the Maybe monad. The value here is that it will only call the callback provided to `bind()` if the value it wraps is not `null`. |
| 40 | + |
| 41 | + use MonadPHP\Maybe; |
| 42 | + $monad = new Maybe(1); |
| 43 | + $monad->bind(function($value) { var_dump($value); }); |
| 44 | + // prints int(1) |
| 45 | + |
| 46 | + $monad = new Maybe(null); |
| 47 | + $monad->bind(function($value) { var_dump($value); }); |
| 48 | + // prints nothing (callback never called)... |
| 49 | + |
| 50 | +The included Chain monad does the same thing, but providing a short-cut implementation for objects: |
| 51 | + |
| 52 | + use MonadPHP\Chain; |
| 53 | + $monad = new Chain($someChainableObject); |
| 54 | + $obj = $monad->call1()->call2()->nonExistantMethod()->call4()->extract(); |
| 55 | + var_dump($obj); |
| 56 | + // null |
| 57 | + |
| 58 | +This can prevent errors when used with chaining... |
| 59 | + |
| 60 | +List Monad |
| 61 | +========== |
| 62 | + |
| 63 | +This abstracts away the concept of a list of items (an array): |
| 64 | + |
| 65 | + use MonadPHP\ListMonad; |
| 66 | + $monad = new ListMonad(array(1, 2, 3, 4)); |
| 67 | + $doubled = $monad->bind(function($value) { return 2 * $value; }); |
| 68 | + var_dump($doubled->extract()); |
| 69 | + // Prints array(2, 4, 6, 8) |
| 70 | + |
| 71 | +Note that the passed in function gets called once per value, so it only ever deals with a single element, never the entire array... |
| 72 | + |
| 73 | +It also works with any `Traversable` object (like iterators, etc). Just be aware that returning the new monad that's wrapped will alwyas become an array... |
| 74 | + |
| 75 | +Composition |
| 76 | +=========== |
| 77 | + |
| 78 | +These Monads can be composed together to do some really useful things: |
| 79 | + |
| 80 | + use MonadPHP\ListMonad; |
| 81 | + use MonadPHP\Maybe; |
| 82 | + |
| 83 | + $monad = new ListMonad(array(1, 2, 3, null, 4)); |
| 84 | + $newMonad = $monad->bind(function($value) { return new Maybe($value); }); |
| 85 | + $doubled = $newMonad->bind(function($value) { return 2 * $value; }); |
| 86 | + var_dump($doubled->extract()); |
| 87 | + // Prints array(2, 4, 6, null, 8) |
| 88 | + |
| 89 | +Or, what if you want to deal with multi-dimensional arrays? |
| 90 | + |
| 91 | + use MonadPHP\ListMonad; |
| 92 | + $monad = new ListMonad(array(array(1, 2), array(3, 4), array(5, 6))); |
| 93 | + $newMonad = $monad->bind(function($value) { return new ListMonad($value); }); |
| 94 | + $doubled = $newMonad->bind(function($value) { return 2 * $value; }); |
| 95 | + var_dump($doubled->extract()); |
| 96 | + // Prints array(array(2, 4), array(6, 8), array(10, 12)) |
| 97 | + |
| 98 | + |
0 commit comments