2017 © Pedro Peláez
 

library collection

A simple immutable collection library with functional capabilities

image

pitchart/collection

A simple immutable collection library with functional capabilities

  • Wednesday, March 1, 2017
  • by pitchart
  • Repository
  • 1 Watchers
  • 0 Stars
  • 8 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 9 Versions
  • 0 % Grown

The README.md

Collection

A simple immutable collection library with a fluent API., (*1)

License: MIT Latest Stable Version Build Status Scrutinizer Code Quality Code Coverage, (*2)

Install

composer require pitchart/collection

Usage

Use the Collection class to manipulate array datas :, (*3)

use Pitchart\Collection\Collection;

$numbers = Collection::from([1, 2, 3, 4]);
$plusOneNumbers = $numbers->map(function ($item) {
     return $item + 1;
});
$evenNumbers = $plusOneNumbers->filter(function ($item) {
    return $item % 2 == 0;
});
$total = $evenNumbers->reduce(function ($accumulator, $item) {
    return $accumulator + $item;
}, 0);

Collection pipelines

Collection pipelines are a programming pattern where you organize some computation as a sequence of operations which compose by taking a collection as output of one operation and feeding it into the next. Martin Fowler, (*4)

This library is designed for collection pipelines usage as it offers a large catalogue of common collection operations., (*5)

The previous example can be rewriten like :, (*6)

$total = Collection::from([1, 2, 3, 4])
    ->map(function ($item) {
        return $item + 1;
    })
    ->filter(function ($item) {
        return $item % 2 == 0;
    })
    ->reduce(function ($accumulator, $item) {
        return $accumulator + $item;
    }, 0);

Speed up operations thanks to generators

If you need to manipulate heavy number of datas or reduce the memory impact of heavy intermediate transformation, you can use the GeneratorCollection class :, (*7)

use Pitchart\Collection\GeneratorCollection;

$collection = Collection::from($heavyFolderList)
    ->map(function ($folder) {
        return loadContentFromFilesInFolder($folder);
    })
    ->filter(function ($content) {
        return lotsOfRegexpContentFiltering($content);
    })
    ->reduce(function ($accumulator, $content) {
        return $accumulator.retrievePartsToCollect($content);
    }, '');

As a generator can be traversed only once, the result can be persisted in a classic Collection to be used as data source for multiples transformations :, (*8)

/** @var Collection $reusableCollection */
$reusableCollection = $generatorCollection->persist();

Functional helpers

This package provides functional helpers to use collection pipelines focused on operations :, (*9)

use function Pitchart\Collection\Helpers\map;

map([1, 2, 3, 4], function ($item) {
    return $item + 1;
})
->filter(function ($item) {
    return $item % 2 == 0;
});

You can also use them to perform operations on any iterable datas, IE arrays or traversable objects, with a consistent API., (*10)

Test

make test

License

The MIT License (MIT). Please see License File for more information., (*11)

The Versions

01/03 2017

dev-master

9999999-dev

A simple immutable collection library with functional capabilities

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Julien VITTE

collection immutable functional

26/02 2017

v1.3.0

1.3.0.0

A simple immutable collection library with functional capabilities

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

by Julien VITTE

collection immutable functional

26/02 2017

dev-ft-generators

dev-ft-generators

A simple immutable collection library with functional capabilities

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

by Julien VITTE

collection immutable functional

26/01 2017

v1.2.0

1.2.0.0

A simple immutable collection library with functional capabilities

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Julien VITTE

collection immutable functional

26/01 2017

v1.1.0

1.1.0.0

A simple immutable collection library with functional capabilities

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Julien VITTE

collection immutable functional

16/01 2017

v1.0.2

1.0.2.0

A simple immutable collection library with functional capabilities

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Julien VITTE

collection immutable functional

09/01 2017

v1.0.1

1.0.1.0

A simple immutable collection library with functional capabilities

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Julien VITTE

collection immutable functional

09/01 2017

v1.0.0

1.0.0.0

Collection library

  Sources   Download

The Development Requires

by Julien VITTE

05/11 2016

1.0.0

1.0.0.0

Collection library

  Sources   Download

The Development Requires

by Julien VITTE