Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/pitchart/transformer

A PHP transducers implementation in a fluent way
https://github.com/pitchart/transformer

collection composition functional-programming iterables php transducer

Last synced: 3 months ago
JSON representation

A PHP transducers implementation in a fluent way

Awesome Lists containing this project

README

        

# Transformer

A transducers implementation in PHP, with OOP powers

This package has been inspired by libraries provided by other languages, like clojure's transducers or JAVA Stream API.

## Why ?

> Transducers are composable algorithmic transformations. They are independent from the context of their input and output sources and specify only the essence of the transformation in terms of an individual element. Because transducers are decoupled from input or output sources, they can be used in many different processes - collections, streams, channels, observables, etc. Transducers compose directly, without awareness of input or creation of intermediate aggregates.

[More information on the clojure reference](https://clojure.org/reference/transducers)

With classical pipeline pattern, the whole collection is iterated for each step of the transformation and creates an intermediate collection which is a massive waste in memory usage.

Transducers use functional composition to reduce the number of iterations made while applying transformation.

## Installation

```bash
composer require pitchart/transformer
```

## Usage

A transformation consists in a pipeline of transformation functions, called Reducers, ended by a reducing function, aka Termination.

```php
use function Pitchart\transform;

transform([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
->filter(function (int $integer) { return $integer % 2 === 0;}) // [0, 2, 4, 6, 8, 8, 6, 4, 2, 0]
->map(function (int $integer) { return $integer + 1; }) // [1, 3, 5, 7, 9, 9, 7, 5, 3, 1]
->dedupe() // [1, 3, 5, 7, 9, 9, 7, 5, 3, 1]
->distinct() // [1, 3, 5, 7, 9]
->take(3) // [1, 3, 5]
->sum() // 9
;

```

## API documentation

"Functional patterns" used by the library are discribed in the [Reducers documentation](docs/Reducers.md)

## Credits

- [Julien VITTE](https://github.com/pitchart)
- [All contributors](https://github.com/pitchart/transformer/graphs/contributors)

## Licence

The MIT License (MIT). See [License file](LICENCE.md) for more information.