php-slang/php-slang

PHPSlang is a library that allow you to write a purely functional code in PHP

v0.1.4 2017-02-07 09:50 UTC

This package is auto-updated.

Last update: 2024-03-18 22:53:16 UTC


README

Packagist PHP from Packagist Build Status Scrutinizer Code Quality Code Coverage Build Status Packagist

PhpSlang

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

Official page

Documentation

Twitter

Roadmap

Features

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

  1. Fork this repository

  2. Clone your fork

git clone git@github.com:php-slang/php-slang.git
  1. Install dependencies
composer install --dev
  1. Write some code

  2. Verify if it's fine

./ci/verify.sh
  1. 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.