php-slang / php-slang
PHPSlang is a library that allow you to write a purely functional code in PHP
Installs: 27 198
Dependents: 3
Suggesters: 0
Security: 0
Stars: 105
Watchers: 15
Forks: 11
Open Issues: 23
Requires
- php: ~7.0
Requires (Dev)
- humbug/humbug: ~1.0@dev
- phpunit/phpunit: ~5.5
README
PhpSlang will help you to write purely functional code with PHP.
PhpSlang is a PHP library aiming to fill the gaps between PHP and classical functional languages. It provides constructs optimizing your work and letting you develop with a purely functional style.
More info
Features
- Option monad
- Either monad
- Copy trait
- Immutable List collection
- Trampolines
- Pattern matching
- Memoization
- Immutable HashMap collections
- Immutable Set collections
- Extractors
- Try monad
- Future monad
- Parallel immutable List collection
- Parallel immutable HashMap collection
- Parallel immutable Set collection
- Traversable collections
- Numeric type with infinite precision
- Chainable\pipe monad
- Convenient enumeration
Example code
With a PhpSlang your code is going to look like that:
public function nonTrivialExampleFn(ParallelListCollection $mysteriousInput): float { return $mysteriousInput ->filter(function ($elem) { return $elem > 10; }) ->partition( function ($elem) { return $elem <=> 20; }, new Set([-1, 0, 1]) ) ->map(function (ListCollection $bucket) { return $bucket->max()->getOrElse(0.0); }) ->avg() ->getOrElse(0.0); }
Or that:
public function actionUpdateBook(string $bookId, Request $request): Response { return $this ->bookUpdaterService ->updateBook($bookId, $this->bookRequestTransformer->toInput($request), $this->getUser()) ->left(function(BookUpdateError $error) { return Match::val($error)->of( new TypeOf(BookNotFound::class, new Response(null, Response::HTTP_NOT_FOUND)), new TypeOf(InvalidInput::class, new Response(null, Response::HTTP_BAD_REQUEST)), new TypeOf(NotAuthorized::class, new Response(null, Response::HTTP_UNAUTHORIZED)) ); }) ->right(function(BookInfo $bookInfo) { return new Response($this->bookInfoTransformer->toJson($bookInfo), Response::HTTP_OK); }) ->get(); }
Want to see more examples? See documentation.
Contribution
TL;DR;
Just feel free to post your pull requests on GitHub.
Contribution how to
-
Fork this repository
-
Clone your fork
git clone git@github.com:php-slang/php-slang.git
- Install dependencies
composer install --dev
-
Write some code
-
Verify if it's fine
./ci/verify.sh
- Push your pull request!
The MIT License (MIT)
Copyright (c) 2018 Witold Adamus
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.