GithubHelp home page GithubHelp logo

loophp / fpt Goto Github PK

View Code? Open in Web Editor NEW
25.0 4.0 0.0 109 KB

Functional programming toolbox for PHP.

Home Page: https://loophp-fpt.rtfd.io/

License: MIT License

PHP 98.99% Dockerfile 1.01%
functional-programming toolbox

fpt's Introduction

Latest Stable Version GitHub stars Total Downloads GitHub Workflow Status Scrutinizer code quality Type Coverage Code Coverage License Read the Docs Donate! Donate!

Functional Programming Toolbox

Description

Functional Programming Toolbox (FPT for the friends) is a set of stateless and immutable helper classes to facilitate the use of functional programming(FP) concepts.

This projects doesn't aim to transform PHP into a full featured FP language, but it will helps users willing to use and understand a subset of FP concepts in their own code.

Requirements

  • PHP 8

Installation

composer require loophp/fpt

Usage

Curry and Partial

<?php

declare(strict_types=1);

namespace Example;

include __DIR__ . '/vendor/autoload.php';

use loophp\fpt\FPT;

// The "curry" method combine at the same time the
// Curry and Partial, see example below.
$explode = FPT::curry()('explode');

// Thanks to the new PHP 8 feature "named arguments",
// you can pass arguments in any order.

var_dump($explode(',', 'a,b,c'));
var_dump($explode(separator: ',', string: 'a,b,c'));
var_dump($explode(separator: ',')(string: 'a,b,c'));
var_dump($explode(string: 'a,b,c', separator: ','));
var_dump($explode(string: 'a,b,c')(separator: ','));

// All of these will return:
/**
 * array(3) {
 *   [0] => string(1) "a"
 *   [1] => string(1) "b"
 *   [2] => string(1) "c"
 * }
*/

// You can pass an optional "arity" argument

$explode = FPT::curry()('explode', 3);

var_dump($explode(',', 'a,b,c', 2));
var_dump($explode(separator: ',', string: 'a,b,c', limit: 2));
var_dump($explode(separator: ',')(string: 'a,b,c')(limit: 2));
var_dump($explode(limit: 2, string: 'a,b,c', separator: ','));
var_dump($explode(limit: 2)(string: 'a,b,c')(separator: ','));

// All of these will return:
/**
 * array(2) {
 *   [0] => string(1) "a"
 *   [1] => string(3) "b,c"
 * }
*/

Composition

<?php

declare(strict_types=1);

namespace Example;

include __DIR__ . '/vendor/autoload.php';

use loophp\fpt\FPT;

$closure = static fn (string $first, string $second): string => sprintf("My cats names are %s and %s.", $first, $second);

$composedClosure = FPT::compose()('strtoupper', $closure);

$composedClosure('Izumi', 'Nakano'); // "MY CATS NAMES ARE IZUMI AND NAKANO."

Documentation

The API will give you a pretty good idea of the existing methods and what you can do with it.

I'm doing my best to keep the documentation up to date, if you found something odd, please let me know in the issue queue.

Code quality, tests and benchmarks

Every time changes are introduced into the library, Github run the tests.

The library has tests written with PHPSpec. Feel free to check them out in the spec directory. Run composer phpspec to trigger the tests.

Before each commit some inspections are executed with GrumPHP, run composer grumphp to check manually.

The quality of the tests is tested with Infection a PHP Mutation testing framework, run composer infection to try it.

Static analysers are also controlling the code. PHPStan and PSalm are enabled to their maximum level.

Contributing

Feel free to contribute by sending Github pull requests. I'm quite reactive :-)

If you can't contribute to the code, you can also sponsor me on Github or Paypal.

Changelog

See CHANGELOG.md for a changelog based on git commits.

For more detailed changelogs, please check the release changelogs.

Thanks

The API documentation has been inspired by the Ramda project.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.