vector / core
A functional programming library for PHP
v7.0.1
2020-03-10 01:24 UTC
Requires
- php: ^7.4
Requires (Dev)
- phpunit/phpunit: ^8.4
- dev-master
- v7.0.1
- v7.0.0
- v6.0.0
- v5.1.0
- v5.0.0
- v4.0.0
- v3.0.10
- v3.0.9
- v3.0.8
- v3.0.7
- v3.0.6
- v3.0.5
- v3.0.4
- v3.0.3
- v3.0.2
- v3.0.1
- v3.0.0
- v2.0.27
- v2.0.26
- v2.0.25
- v2.0.24
- v2.0.23
- v2.0.22
- v2.0.21
- v2.0.20
- v2.0.19
- v2.0.18
- v2.0.17
- v2.0.16
- v2.0.15
- v2.0.14
- v2.0.13
- v2.0.12
- v2.0.11
- v2.0.10
- v2.0.9
- v2.0.8
- v2.0.7
- v2.0.6
- v2.0.5
- v2.0.4
- v2.0.3
- v2.0.2
- v2.0.1
- v2.0.0
- v1.2.0
- v1.1.3
- v1.1.2
- v1.1.1
- v1.1.0
- v1.0.0
- dev-5.0-dev
- dev-lh-patterns
- dev-Functors-Redux
This package is auto-updated.
Last update: 2024-10-28 05:29:35 UTC
README
The Elevator Pitch
Vector gives you php functional superpowers.
-
The evolution:
- Native PHP
array_sum( array_map( fn($a) => $a + 1, [1, 2, 3] ) ); // 9
- 👎 More than 1 or 2 function chains is unmaintainable
- Laravel Collections
collect([1, 2, 3]) ->map(fn($a) => $a + 1) ->sum(); // 9
- 👍 More than 1 or 2 function chains is unmaintainable
- 👎 Unfortunately you can't do this with every type in the same elegant way (only works with collections)
- Vector
vector([1, 2, 3]) ->pipe(Arrays::map(Math::add(1))) // or `fn($a) => $a + 1)` ->pipe(Math::sum())(); // [2, 3, 4]
- 👍 Works super similarly to collections, but just accepts & returns normal arrays (no ->toArray()-ing necessary)
- 👍 Works super similarly to collections for everything else too!
- 👎 Unfortunately it is an extra dependency (we don't have the native pipe operator yet https://wiki.php.net/rfc/pipe-operator-v2)
- Native PHP
-
You can add currying to any function, it isn't only limited to Vector built ins.
Module::curry('explode')(',')('a,b,c')(PHP_INT_MAX)
// ['a', 'b', 'c']
PHP Version Support
- 8.0+
Install
composer require vector/core
Show Me Some More Code
More automatic currying.
$addOne = Arrays::map(Math::add(1)); $addOne([1, 2, 3]); // [2, 3, 4]
First class composition (Functional Pipelines).
$addSix = Lambda::compose(Math::add(4), Math::add(2)); // (Or ::pipe for the opposite flow direction) $addSix(4); // 10;
Pattern Matching (Maybe & Result monads included).
Pattern::match([ fn(Just $value) => fn ($unwrapped) => $unwrapped, fn(Nothing $value) => 'nothing', ])(Maybe::just('just')); // 'just'
Granular control flow (without try/catch).
$errorHandler = function (Err $err) { return Pattern::match([ function (QueryException $exception) { Log::info($exception); return response(404); }, function (DBException $exception) { Log::error($exception); return response(500); }, ]); }; return Pattern::match([ fn(Ok $value) => fn (User $user) => $user, $errorHandler ])(Result::from(fn() => User::findOrFail(1)));
Make your own modules with auto-curried methods
use Vector\Core\Curry; use Vector\Core\Module; class MyModule { use Module; #[Curry] protected static function myCurriedFunction($a, $b) { return $a + $b; } }