Are you happy with your logging solution? Would you help us out by taking a 30-second survey? Click here

iter

Iteration primitives using generators

Subscribe to updates I use iter


Statistics on iter

Number of watchers on Github 644
Number of open issues 8
Average time to close an issue 6 days
Main language PHP
Average time to merge a PR 18 days
Open pull requests 9+
Closed pull requests 21+
Last commit over 1 year ago
Repo Created about 6 years ago
Repo Last Updated over 1 year ago
Size 118 KB
Organization / Authornikic
Latest Releasev1.6.0
Contributors9
Page Updated
Do you use iter? Leave a review!
View open issues (8)
View iter activity
View on github
Fresh, new opensource launches 🚀🚀🚀
Trendy new open source projects in your inbox! View examples

Subscribe to our mailing list

Evaluating iter for your project? Score Explanation
Commits Score (?)
Issues & PR Score (?)

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.

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.

Install

To install with composer:

composer require nikic/iter

Functionality

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

<?php

use iter\fn;

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

$nums = iter\range(1, 10);
$numsTimesTen = iter\map(fn\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:

Iterator map(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 = true)
Iterator toIter(iterable $iterable)

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

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.

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

<?php

use iter\fn;

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

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

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

The above functions are only useful for your own generators though, for the iter generators rewindable variants are directly provided with an iter\rewindable prefix:

$res = iter\rewindable\map(fn\operator('*', 3), [1, 2, 3]);
// etc
iter open issues Ask a question     (View All Issues)
  • over 3 years \iter\unique support
iter open pull requests (View All Pulls)
  • add permutations and combinations functions
  • Support for cycle
  • Recursive cousins for toArray and toArrayWithKeys
  • Micro-optimize if,if to if,elseif
  • Fix non-existent PHPDoc type
  • Drop isIterable in favor of PHP 7.1 is_iterable function
  • Add <> operator
  • Closes #31. Add \iter\unique which returns an iterator with unique va…
  • test enhancement
iter questions on Stackoverflow (View All Questions)
  • Using multiple iter vars in the for loop in python
  • Can't figure out return type of this Rust function that returns Iter?
  • Modifying `iter` chain to use `and_then`, etc
  • What is the difference between iter and into_iter?
  • Does it make sense or is it possible to write a `iter` function for lazy list?
  • How does zip(*[iter(s)]*n) work in Python?
  • Element Tree's iter() is skipping over random elements
  • How does the Python iter() function work?
  • Why is iter not a method of an instance and __iter__ is?
  • Python - NLP - convert iter(iter(tree)) to list(tree)
  • libsvm: optimization finished, #iter = 1 nu = nan
  • c++ boost::iterator_range<iter> string_view error
  • When looping, how does .iter() differ from a reference (&)?
  • What is "def __iter__(self): return iter(self.file)" for?
  • Difference between iter() and into_iter() on a shared, borrowed Vec?
  • What is the difference between iter(x) and x.__iter__()?
  • C++ understanding iter and erase
  • What should be the return type of iter().cloned().filter().map()
  • Clarification of iterating through maps using for(auto iter : value)
  • vector erase works without setting iter return value
  • How to implement iter method using static/dynamic dispatch?
  • Is there an iter which accepts a complex sentinel?
  • Iter through defaultdict of defaultdict on Python 2.6
  • Why calling cloned() on Iter with inferred Item type allows it to be passed into this function?
  • iter, values, item in dictionary does not work
  • TypeError: iter() takes no keyword arguments
  • Parsing text file lines into numbers using std::iter::Iterator map
  • Error when trying to update a JAGS model: "Error in model$iter() : JAGS model must be recompiled"
  • "yield from iterable" vs "return iter(iterable)"
  • What is the equivalent of Rust's Option::iter in Java?
iter list of languages used
iter latest release notes
v1.6.0 iter 1.6
  • Added iter\enumerate() function (#43).
  • Added iter\isEmpty() function (#46).
  • Added $preserveKeys parameter to iter\chunk() (#42).
v1.5.0 iter 1.5
  • Added $levels argument for flatten().
  • Added flatMap() function.
v1.4.0 iter 1.4
  • Fixed infinite loop when iter\zip() is called without arguments.
  • Added additional error checks for various length and count parameters (usually disallowing negative numbers).
  • The iter\flatten() function now preserves keys.
  • Added an iter\recurse() function. Example use is iter\recurse('iter\toArray', $data) which will recursively convert an iterator to array.
Other projects in PHP