• Stars
    star
    1,108
  • Rank 40,231 (Top 0.9 %)
  • Language
    PHP
  • License
    Other
  • Created almost 11 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Iteration primitives using generators

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\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:

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 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.

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\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:

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

More Repositories

1

PHP-Parser

A PHP parser written in PHP
PHP
16,432
star
2

FastRoute

Fast request router for PHP
PHP
4,986
star
3

scalar_objects

Extension that adds support for method calls on primitive types in PHP
PHP
1,120
star
4

php-ast

Extension exposing PHP 7 abstract syntax tree
PHP
900
star
5

PHP-Fuzzer

Experimental fuzzer for PHP libraries
PHP
394
star
6

Phlexy

Lexing experiments in PHP
PHP
160
star
7

DB

Very simple and secure PDO wrapper class
PHP
92
star
8

php-crater

Like crater, but for PHP
PHP
68
star
9

include-interceptor

Library to intercept and dynamically transform PHP includes. Forked from icewind1991/interceptor.
PHP
68
star
10

sample_prof

Sampling profiler for PHP
C
58
star
11

comparable

PHP extension implementing a magic "Comparable" interface
C
49
star
12

TypeUtil

Utility for adding PHP 7 scalar types and return types
PHP
47
star
13

ditaio

Cooperative task scheduler using coroutines
PHP
42
star
14

buffer

PHP extension for buffer based typed arrays
C
39
star
15

popular-package-analysis

Utilities to analyze most popular composer packages
PHP
38
star
16

nikic.github.com

CSS
36
star
17

llvm-compile-time-tracker

LLVM compile-tracking tracking infrastructure
PHP
33
star
18

Phuzzy

Fuzzer for PHP internal functions
PHP
31
star
19

prephp

Preprocesses PHP code to allow custom syntax and syntax of newer PHP versions, especially PHP 5.3
PHP
29
star
20

PHP-Backporter

Converts PHP 5.3 code to PHP 5.2 code
PHP
22
star
21

llvm-compile-time-data

LLVM compile-time performance data over time (repo 0).
18
star
22

tokenstream

Framework around PHP's Tokenizer to allow easier working with it
PHP
8
star
23

SPL-Datastructures

C
8
star
24

pear

Repo for publishing my PEAR packages
CSS
3
star
25

llvm-ir-diffs

LLVM
1
star
26

test-repo

For testing things.
1
star