The place where PHP meets Functional Programming
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.
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.
Just feel free to post your pull requests on GitHub.
Fork this repository
Clone your fork
git clone [email protected]:php-slang/php-slang.git
composer install --dev
Write some code
Verify if it's fine
./ci/verify.sh
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.