PHPackages                             ibsciss/php-functional - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. ibsciss/php-functional

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

ibsciss/php-functional
======================

a reducer implementation in php

1552[13 issues](https://github.com/Ibsciss/php-functional/issues)PHP

Since Mar 17Pushed 10y ago4 watchersCompare

[ Source](https://github.com/Ibsciss/php-functional)[ Packagist](https://packagist.org/packages/ibsciss/php-functional)[ RSS](/packages/ibsciss-php-functional/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependenciesVersions (1)Used By (0)

Php Functional
==============

[](#php-functional)

[![Join the chat at https://gitter.im/Ibsciss/php-functional](https://camo.githubusercontent.com/abe08b740a4156153736f791393ec4da6619c4be73212e75769f52edacc0e2b5/68747470733a2f2f6261646765732e6769747465722e696d2f4a6f696e253230436861742e737667)](https://gitter.im/Ibsciss/php-functional?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Travis CI](https://camo.githubusercontent.com/b6e949cd611199a2a305da564d17145814a35b07addad544cbe532d6161f8efa/68747470733a2f2f7472617669732d63692e6f72672f496273636973732f7068702d66756e6374696f6e616c2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/Ibsciss/php-functional)[![Code Coverage](https://camo.githubusercontent.com/fe9e930ebd0bf9fb37850f453e2edc7c0a9d4cd26494c19d81a1fe2c131c23c5/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f496273636973732f7068702d66756e6374696f6e616c2f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/Ibsciss/php-functional/?branch=master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/7291c85716ac6a66d7e98525f0906d742d0e4e9bb9cc55302dad1b76943aa1f5/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f496273636973732f7068702d66756e6374696f6e616c2f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/Ibsciss/php-functional/?branch=master)[![Build Status](https://camo.githubusercontent.com/8bc38a00b7f08c18c6bd8ae3ba48dfb289592d0b848ac937b237dfc315fa4462/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f496273636973732f7068702d66756e6374696f6e616c2f6261646765732f6275696c642e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/Ibsciss/php-functional/build-status/master)

A collection of functions and classes to provide some nice functional tools for your projects, with a simple, **consistent** and well tested api.

Especially useful to build data processing algorithms in a breeze.

Install
-------

[](#install)

Via Composer

```
$ composer require ibsciss/php-functionnal
```

Usage
-----

[](#usage)

### Simple example

[](#simple-example)

Imagine you want to compute the total VAT amount for october:

Instead of doing things like this:

```
function compute_october_vat() {
    $total_vat_amount = 0;
    foreach ($invoices as $invoice) {
        if ($invoice->due_date->format('m') == '10') {
            $total_vat_amount += $invoice->amount * 0.2;
        }
    }
    return $total_vat_amount;
}
```

Or, if you want to try with map / reduce functions:

```
function compute_october_vat() {
    return array_reduce(
        array_map(
         function($invoice) { return $invoice->amount * 0.2; },
         array_filter( $invoices, function($invoice) { return $invoice->due_date->format('m') == '10'; } )
        ),
        function($x, $y) { return $x + $y; }, 0);
}
```

You can now use a more fluent api:

```
function compute_october_vat() {
    return Fp\collection($invoices)
        ->filter( function($invoice) { return $invoice->due_date->format('m') == '10'; }; )
        ->map( function($invoice) { return $invoice->amount * 0.2; }; )
        ->add();
}
```

Functional helper
-----------------

[](#functional-helper)

### Compose

[](#compose)

The compose function give you the ability to create a new functions from existing functions:

```
compose(f,g,h)(x) == f(g(h(x)))

```

A practical example:

```
$plus_one = function($x) { return $x + 1; };
$square = function($x) { return pow($x, 2); };

$plus_one_and_square = Fp\compose($plus_one, $square);
$plus_one_and_square(2) //return 9
```

*Of course you can compose as much functions as you want.*

### Pipelines functions

[](#pipelines-functions)

Pipelines functions are useful to apply transformations to collections, Martin Fowler wrote a [very good introduction (based on ruby)](http://martinfowler.com/articles/collection-pipeline/) about it. On the same blog, you'll find another resource to learn [how to refactor your too many loops using pipeline](http://martinfowler.com/articles/refactoring-pipelines.html).

The `map`, `filter` and `reduce` functions are wrapper around the native php function, to understand why we have made them please see the [FAQ](#faq).

#### Map

[](#map)

Apply a function to each item of a collection to create a new array.

```
//square each item of the collection
Fp\map(
  function($x) {
    return pow($x, 2); //square function
  }, [1,2,3]
); //return [1,4,9,16]
```

#### Filter

[](#filter)

Build an array composed with items that returns true when passed in the given callback.

```
//return even values from the collection
Fp\filter(
  function($x) {
    return ($x % 2 == 0);
  },
  [1,2,3,4]
); //return [2,4]
```

#### Reduce

[](#reduce)

It makes an accumulation by passing each item to the given callback. The callback returning value is returned for the next call (an init value is provided for the first call).

```
//sum values of the collection
Fp\reduce(
  function($carry, $item) {
    return $carray + $item
  },
  [1,2,3,4],
  0
); //return 10
```

#### Chaining

[](#chaining)

You can chain operations by using the `Fp\collection(collection)` function (don't forget to call `values()` to get the results):

```
//squared even values from the given collection
Fp\collection([1,2,3,4])
  ->filter(
    function($x) { return ($x % 2 == 0); }
  )
  ->map(
    function($x) { return pow($x, 2); }
  )
  ->values();
```

### Collection transducers

[](#collection-transducers)

With classical pipeline functions, you have to iterate the whole collection for each step of the transformation and create an intermediate collection which is a massive waste in memory usage.

Moreover you can't really extract a step to use it in other contexts which is bad for code reuse.

To tackle these downsides of classic pipeline function, the functional world come with a nice solution: `tranducers`.

#### Mapping

[](#mapping)

like map

#### Filtering

[](#filtering)

like filter

### scalar transducer

[](#scalar-transducer)

Use with `single_result` terminal reducer.

#### First

[](#first)

return the first element

#### Max

[](#max)

return the max

### Aggregate reducer

[](#aggregate-reducer)

#### Batching

[](#batching)

Batch result

#### Enumerating

[](#enumerating)

Create indexed tuples with results

### Terminal reducer

[](#terminal-reducer)

#### appending

[](#appending)

append to an array

#### conjoining

[](#conjoining)

immutable appending by merge

#### single\_result

[](#single_result)

to get a scalar result instead of a collection

FAQ
---

[](#faq)

### Why not using directly array\_\* (array\_filter, array\_map) functions ?

[](#why-not-using-directly-array_-array_filter-array_map-functions-)

- To improve api consistency
- To be able to produce transducers if the iterable is omitted
- To be able to consume `Collection` objects.

Change log
----------

[](#change-log)

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

Testing
-------

[](#testing)

```
$ composer test
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security
--------

[](#security)

If you discover any security related issues, please email :author\_email instead of using the issue tracker.

Credits
-------

[](#credits)

- [Arnaud LEMAIRE](https://github.com/lilobase)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

19

—

LowBetter than 9% of packages

Maintenance3

Infrequent updates — may be unmaintained

Popularity13

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 83.3% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/335501?v=4)[Arnaud LEMAIRE](/maintainers/lilobase)[@lilobase](https://github.com/lilobase)

---

Top Contributors

[![lilobase](https://avatars.githubusercontent.com/u/335501?v=4)](https://github.com/lilobase "lilobase (25 commits)")[![arnolanglade](https://avatars.githubusercontent.com/u/3585922?v=4)](https://github.com/arnolanglade "arnolanglade (4 commits)")[![gitter-badger](https://avatars.githubusercontent.com/u/8518239?v=4)](https://github.com/gitter-badger "gitter-badger (1 commits)")

### Embed Badge

![Health badge](/badges/ibsciss-php-functional/health.svg)

```
[![Health](https://phpackages.com/badges/ibsciss-php-functional/health.svg)](https://phpackages.com/packages/ibsciss-php-functional)
```

###  Alternatives

[sylius-labs/polyfill-symfony-event-dispatcher

Symfony EventDispatcher Polyfill

218.5M2](/packages/sylius-labs-polyfill-symfony-event-dispatcher)[yepsua/filament-rating-field

Ranting field for the Filament forms

4738.0k](/packages/yepsua-filament-rating-field)[aura/payload-interface

An interface package for Domain Payload implementations.

13404.3k4](/packages/aura-payload-interface)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
