| @@ 12-35 (lines=24) @@ | ||
| 9 | load_macro('type', 'is'); |
|
| 10 | load_macro('logic', 'cond'); |
|
| 11 | ||
| 12 | P::macro('both', call_user_func(function () { |
|
| 13 | $allPrimitives = P::unapply(P::all(P::is('bool'))); |
|
| 14 | $allCallables = P::unapply(P::all(P::is('callable'))); |
|
| 15 | ||
| 16 | $bothPredicate = function ($left, $right) { |
|
| 17 | return function (...$args) use ($left, $right) { |
|
| 18 | return $left(...$args) && $right(...$args); |
|
| 19 | }; |
|
| 20 | }; |
|
| 21 | ||
| 22 | $compareBooleans = function ($left, $right) { |
|
| 23 | return $left && $right; |
|
| 24 | }; |
|
| 25 | ||
| 26 | $both = P::cond([ |
|
| 27 | [$allPrimitives, $compareBooleans], |
|
| 28 | [$allCallables, $bothPredicate], |
|
| 29 | [P::otherwise(), P::throwException(\InvalidArgumentException::class, [])], |
|
| 30 | ]); |
|
| 31 | ||
| 32 | return function ($left, $right) use ($both) { |
|
| 33 | return $both($left, $right); |
|
| 34 | }; |
|
| 35 | })); |
|
| 36 | ||
| @@ 11-34 (lines=24) @@ | ||
| 8 | ||
| 9 | load_macro('type', 'is'); |
|
| 10 | ||
| 11 | P::macro('either', call_user_func(function () { |
|
| 12 | $allPrimitives = P::unapply(P::all(P::is('bool'))); |
|
| 13 | $allCallables = P::unapply(P::all(P::is('callable'))); |
|
| 14 | ||
| 15 | $compareBooleans = function ($left, $right) { |
|
| 16 | return $left || $right; |
|
| 17 | }; |
|
| 18 | ||
| 19 | $eitherPredicate = function ($left, $right) { |
|
| 20 | return function (...$args) use ($left, $right) { |
|
| 21 | return $left(...$args) || $right(...$args); |
|
| 22 | }; |
|
| 23 | }; |
|
| 24 | ||
| 25 | $either = P::cond([ |
|
| 26 | [$allPrimitives, $compareBooleans], |
|
| 27 | [$allCallables, $eitherPredicate], |
|
| 28 | [P::otherwise(), P::throwException(\InvalidArgumentException::class, [])], |
|
| 29 | ]); |
|
| 30 | ||
| 31 | return function ($left, $right) use ($either) { |
|
| 32 | return $either($left, $right); |
|
| 33 | }; |
|
| 34 | })); |
|
| 35 | ||