2017 © Pedro Peláez
 

library iter

Iteration primitives using generators

image

nikic/iter

Iteration primitives using generators

  • Friday, February 23, 2018
  • by nikic
  • Repository
  • 44 Watchers
  • 741 Stars
  • 617,648 Installations
  • PHP
  • 27 Dependents
  • 0 Suggesters
  • 41 Forks
  • 10 Open issues
  • 9 Versions
  • 7 % Grown

The README.md

Iteration primitives using generators

This library implements iteration primitives like map() and filter() using generators. To a large part this serves as a repository for small examples of generator usage, but of course the functions are also practically quite useful., (*1)

All functions in this library accept arbitrary iterables, i.e. arrays, traversables, iterators and aggregates, which makes it quite different from functions like array_map() (which only accept arrays) and the SPL iterators (which usually only accept iterators, not even aggregates). The operations are of course lazy., (*2)

Install

To install with composer:, (*3)

composer require nikic/iter

Functionality

A small usage example for the map() and range() functions:, (*4)

<?php

use iter\func;

require 'path/to/vendor/autoload.php';

$nums = iter\range(1, 10);
$numsTimesTen = iter\map(func\operator('*', 10), $nums);
// => iter(10, 20, 30, 40, 50, 60, 70, 80, 90, 100)

You can find documentation and usage examples for the individual functions in iter.php, here I only list the function signatures as an overview:, (*5)

Iterator map(callable $function, iterable $iterable)
Iterator mapWithKeys(callable $function, iterable $iterable)
Iterator mapKeys(callable $function, iterable $iterable)
Iterator flatMap(callable $function, iterable $iterable)
Iterator reindex(callable $function, iterable $iterable)
Iterator filter(callable $predicate, iterable $iterable)
Iterator enumerate(iterable $iterable)
Iterator toPairs(iterable $iterable)
Iterator fromPairs(iterable $iterable)
Iterator reductions(callable $function, iterable $iterable, mixed $startValue = null)
Iterator zip(iterable... $iterables)
Iterator zipKeyValue(iterable $keys, iterable $values)
Iterator chain(iterable... $iterables)
Iterator product(iterable... $iterables)
Iterator slice(iterable $iterable, int $start, int $length = INF)
Iterator take(int $num, iterable $iterable)
Iterator drop(int $num, iterable $iterable)
Iterator takeWhile(callable $predicate, iterable $iterable)
Iterator dropWhile(callable $predicate, iterable $iterable)
Iterator keys(iterable $iterable)
Iterator values(iterable $iterable)
Iterator flatten(iterable $iterable, int $levels = INF)
Iterator flip(iterable $iterable)
Iterator chunk(iterable $iterable, int $size, bool $preserveKeys = false)
Iterator chunkWithKeys(iterable $iterable, int $size)
Iterator tap(callable $function, iterable $iterable)
Iterator toIter(iterable $iterable)

Iterator range(number $start, number $end, number $step = null)
Iterator repeat(mixed $value, int $num = INF)
Iterator split(string $separator, string $data)

mixed    reduce(callable $function, iterable $iterable, mixed $startValue = null)
bool     any(callable $predicate, iterable $iterable)
bool     all(callable $predicate, iterable $iterable)
mixed    search(callable $predicate, iterable $iterable)
void     apply(callable $function, iterable $iterable)
string   join(string $separator, iterable $iterable)
int      count(iterable $iterable)
bool     isEmpty(iterable $iterable)
mixed    recurse(callable $function, $iterable)
array    toArray(iterable $iterable)
array    toArrayWithKeys(iterable $iterable)
bool     isIterable($value)

As the functionality is implemented using generators the resulting iterators are by default not rewindable. This library implements additional functionality to allow creating rewindable generators., (*6)

You can find documentation for this in iter.rewindable.php, here is just a small usage example of the two main functions:, (*7)

<?php

use iter\func;

require 'path/to/vendor/autoload.php';

/* Create a rewindable map function which can be used multiple times */
$rewindableMap = iter\makeRewindable('iter\\map');
$res = $rewindableMap(func\operator('*', 3), [1, 2, 3]);

/* Do a rewindable call to map, just once */
$res = iter\callRewindable('iter\\map', func\operator('*', 3), [1, 2, 3]);

The above functions are only useful for your own iterators though; for the iter iterators, rewindable variants are directly provided with an iter\rewindable prefix:, (*8)

$res = iter\rewindable\map(func\operator('*', 3), [1, 2, 3]);
// etc

The Versions

23/02 2018

dev-master

9999999-dev

Iteration primitives using generators

  Sources   Download

BSD-3-Clause

The Requires

  • php ^7.1

 

The Development Requires

by Nikita Popov

generator iterator functional

13/01 2018

1.x-dev

1.9999999.9999999.9999999-dev

Iteration primitives using generators

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.5.0

 

The Development Requires

by Nikita Popov

generator iterator functional

10/11 2017

v1.6.0

1.6.0.0

Iteration primitives using generators

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.5.0

 

The Development Requires

by Nikita Popov

generator iterator functional

26/04 2017

v1.5.0

1.5.0.0

Iteration primitives using generators

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.5.0

 

The Development Requires

by Nikita Popov

generator iterator functional

27/09 2016

v1.4.0

1.4.0.0

Iteration primitives using generators

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.5.0

 

by Nikita Popov

generator iterator functional

25/08 2015

v1.3.0

1.3.0.0

Iteration primitives using generators

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.5.0

 

by Nikita Popov

generator iterator functional

26/02 2015

v1.2.0

1.2.0.0

Iteration primitives using generators

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.5.0

 

by Nikita Popov

generator iterator functional

27/03 2014

v1.1.0

1.1.0.0

Iteration primitives using generators

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.5.0

 

by Nikita Popov

generator iterator functional

01/01 2014

v1.0.0

1.0.0.0

Iteration primitives using generators

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.5.0

 

by Nikita Popov

generator iterator functional