Thanks to visit codestin.com
Credit goes to github.com

Skip to content

brettz9/itertools.js

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

50 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

npm Build Status Coverage Status

A JavaScript port of Python's awesome itertools standard library.

Usage example:

import { izip, cycle } from 'itertools';

const xs = [1, 2, 3, 4];
const ys = ['hello', 'there'];
for (const [x, y] of izip(xs, cycle(ys))) {
    console.log(x, y);
}

// 1 'hello'
// 2 'there'
// 3 'hello'
// 4 'there'

About argument order

In Python, many of the itertools take a function as an argument. In the JS port of these we initially kept these orderings the same to stick closely to the Python functions, but in practice, it turns out to be more pragmatic to flip them, so the function gets to be the second param. Example:

In Python:

map(fn, items)

But in JavaScript:

map(items, fn)

The rationale for this flipping of argument order is because in practice, the function bodies can span multiple lines, in which case the following block will remaing aesthetically pleasing:

import { map } from 'itertools';

const numbers = [1, 2, 3];
const squares = map(numbers, n => {

    //
    // Do something wild with these numbers here
    //
    // ...
    return n * n;

});

API

The itertools package consists of a few building blocks:

Builtins

all

Returns true when all of the items in iterable are truthy. An optional key function can be used to define what truthiness means for this specific collection.

Examples:

all([])                           // => true
all([0])                          // => false
all([0, 1, 2])                    // => false
all([1, 2, 3])                    // => true

Examples with using a key function:

all([2, 4, 6], n => n % 2 === 0)  // => true
all([2, 4, 5], n => n % 2 === 0)  // => false

Parameters

  • iterable Iterable<T>
  • keyFn Predicate<T> (optional, default identityPredicate)

Returns boolean

any

Returns true when any of the items in iterable are truthy. An optional key function can be used to define what truthiness means for this specific collection.

Examples:

any([])                           // => false
any([0])                          // => false
any([0, 1, null, undefined])      // => true

Examples with using a key function:

any([1, 4, 5], n => n % 2 === 0)  // => true
any([{name: 'Bob'}, {name: 'Alice'}], person => person.name.startsWith('C'))  // => false

Parameters

  • iterable Iterable<T>
  • keyFn Predicate<T> (optional, default identityPredicate)

Returns boolean

contains

Returns true when any of the items in the iterable are equal to the target object.

Examples:

contains([], 'whatever')         // => false
contains([3], 42)                // => false
contains([3], 3)                 // => true
contains([0, 1, 2], 2)           // => true

Parameters

  • haystack Iterable<T>
  • needle T

Returns boolean

enumerate

Returns an iterable of enumeration pairs. Iterable must be a sequence, an iterator, or some other object which supports iteration. The elements produced by returns a tuple containing a counter value (starting from 0 by default) and the values obtained from iterating over given iterable.

Example:

import { enumerate } from 'itertools';

console.log([...enumerate(['hello', 'world'])]);
// [0, 'hello'], [1, 'world']]

Parameters

  • iterable Iterable<T>
  • start number (optional, default 0)

Returns Iterable<[number, T]>

filter

Non-lazy version of ifilter().

Parameters

  • iterable Iterable<T>
  • predicate Predicate<T>

Returns Array<T>

iter

Returns an iterator object for the given iterable. This can be used to manually get an iterator for any iterable datastructure. The purpose and main use case of this function is to get a single iterator (a thing with state, think of it as a "cursor") which can only be consumed once.

Parameters

  • iterable Iterable<T>

Returns Iterator<T>

map

Non-lazy version of imap().

Parameters

  • iterable Iterable<T>
  • mapper function (T): V

Returns Array<V>

max

Return the largest item in an iterable. Only works for numbers, as ordering is pretty poorly defined on any other data type in JS. The optional keyFn argument specifies a one-argument ordering function like that used for sorted().

If the iterable is empty, undefined is returned.

If multiple items are maximal, the function returns either one of them, but which one is not defined.

Parameters

  • iterable Iterable<T>
  • keyFn function (T): number (optional, default numberIdentity)

Returns Maybe<T>

min

Return the smallest item in an iterable. Only works for numbers, as ordering is pretty poorly defined on any other data type in JS. The optional keyFn argument specifies a one-argument ordering function like that used for sorted().

If the iterable is empty, undefined is returned.

If multiple items are minimal, the function returns either one of them, but which one is not defined.

Parameters

  • iterable Iterable<T>
  • keyFn function (T): number (optional, default numberIdentity)

Returns Maybe<T>

range

TODO

Parameters

Returns Iterable<number>

reduce

TODO: Reducer function with a start value.

Parameters

  • iterable Iterable<T>
  • reducer function (O, T, number): O
  • start O

Returns O

reduce_

TODO: Document Reducer function without a start value. The start value will be the first item in the input iterable. For this reason, the function may not necessarily return anything (i.e. if the input is empty), and the inputs and the outputs must be of homogeneous types.

Parameters

  • iterable Iterable<T>
  • reducer function (T, T, number): T

Returns Maybe<T>

sorted

TODO

Parameters

  • iterable Iterable<T>
  • keyFn function (T): Primitive (optional, default primitiveIdentity)
  • reverse boolean (optional, default false)

Returns Array<T>

sum

TODO

Parameters

Returns number

zip

TODO

Parameters

  • xs Iterable<T1>
  • ys Iterable<T2>

Returns Array<[T1, T2]>

zip3

TODO

Parameters

  • xs Iterable<T1>
  • ys Iterable<T2>
  • zs Iterable<T3>

Returns Array<[T1, T2, T3]>

chain

Returns an iterator that returns elements from the first iterable until it is exhausted, then proceeds to the next iterable, until all of the iterables are exhausted. Used for treating consecutive sequences as a single sequence.

Parameters

  • iterables ...Array<Iterable<T>>

Returns Iterable<T>

count

Returns an iterator that counts up values starting with number start (default 0), incrementing by step. To decrement, use a negative step number.

Parameters

  • start number (optional, default 0)
  • step number (optional, default 1)

Returns Iterable<number>

compress

Non-lazy version of icompress().

Parameters

  • data Iterable<T>
  • selectors Iterable<boolean>

Returns Array<T>

cycle

Returns an iterator producing elements from the iterable and saving a copy of each. When the iterable is exhausted, return elements from the saved copy. Repeats indefinitely.

Parameters

  • iterable Iterable<T>

Returns Iterable<T>

dropwhile

Returns an iterator that drops elements from the iterable as long as the predicate is true; afterwards, returns every remaining element. Note, the iterator does not produce any output until the predicate first becomes false.

Parameters

  • iterable Iterable<T>
  • predicate Predicate<T>

Returns Iterable<T>

icompress

Returns an iterator that filters elements from data returning only those that have a corresponding element in selectors that evaluates to true. Stops when either the data or selectors iterables has been exhausted.

Parameters

  • data Iterable<T>
  • selectors Iterable<boolean>

Returns Iterable<T>

ifilter

Returns an iterator that filters elements from iterable returning only those for which the predicate is true.

Parameters

  • iterable Iterable<T>
  • predicate Predicate<T>

Returns Iterable<T>

imap

Returns an iterator that computes the given mapper function using arguments from each of the iterables.

Parameters

  • iterable Iterable<T>
  • mapper function (T): V

Returns Iterable<V>

islice

Returns an iterator that returns selected elements from the iterable. If start is non-zero, then elements from the iterable are skipped until start is reached. Then, elements are returned by making steps of step (defaults to 1). If set to higher than 1, items will be skipped. If stop is provided, then iteration continues until the iterator reached that index, otherwise, the iterable will be fully exhausted. islice() does not support negative values for start, stop, or step.

Parameters

Returns Iterable<T>

izip2

Returns an iterator that aggregates elements from each of the iterables. Used for lock-step iteration over several iterables at a time. When iterating over two iterables, use izip2. When iterating over three iterables, use izip3, etc. izip is an alias for izip2.

Parameters

  • xs Iterable<T1>
  • ys Iterable<T2>

Returns Iterable<[T1, T2]>

izip3

Like izip2, but for three input iterables.

Parameters

  • xs Iterable<T1>
  • ys Iterable<T2>
  • zs Iterable<T3>

Returns Iterable<[T1, T2, T3]>

izipAll

Like the other izip's, but generalized. Due to type system limitations, you can only "generially" zip iterables with homogeneous types, so you cannot mix types like <A, B> like you can with izip2()

Parameters

  • iters ...Array<Iterable<T>>

Returns Iterable<Array<T>>

permutations

Return successive r-length permutations of elements in the iterable.

If r is not specified, then r defaults to the length of the iterable and all possible full-length permutations are generated.

Permutations are emitted in lexicographic sort order. So, if the input iterable is sorted, the permutation tuples will be produced in sorted order.

Elements are treated as unique based on their position, not on their value. So if the input elements are unique, there will be no repeat values in each permutation.

Parameters

  • iterable Iterable<T>
  • r Maybe<number>

Returns Iterable<Array<T>>

takewhile

Returns an iterator that produces elements from the iterable as long as the predicate is true.

Parameters

  • iterable Iterable<T>
  • predicate Predicate<T>

Returns Iterable<T>

itake

Returns an iterable containing only the first n elements of the given iterable.

Parameters

  • n number
  • iterable Iterable<T>

Returns Iterable<T>

take

Non-lazy version of itake().

Parameters

  • n number
  • iterable Iterable<T>

Returns Array<T>

itertools

chain

Returns an iterator that returns elements from the first iterable until it is exhausted, then proceeds to the next iterable, until all of the iterables are exhausted. Used for treating consecutive sequences as a single sequence.

Parameters

  • iterables ...Array<Iterable<T>>

Returns Iterable<T>

count

Returns an iterator that counts up values starting with number start (default 0), incrementing by step. To decrement, use a negative step number.

Parameters

  • start number (optional, default 0)
  • step number (optional, default 1)

Returns Iterable<number>

compress

Non-lazy version of icompress().

Parameters

  • data Iterable<T>
  • selectors Iterable<boolean>

Returns Array<T>

cycle

Returns an iterator producing elements from the iterable and saving a copy of each. When the iterable is exhausted, return elements from the saved copy. Repeats indefinitely.

Parameters

  • iterable Iterable<T>

Returns Iterable<T>

dropwhile

Returns an iterator that drops elements from the iterable as long as the predicate is true; afterwards, returns every remaining element. Note, the iterator does not produce any output until the predicate first becomes false.

Parameters

  • iterable Iterable<T>
  • predicate Predicate<T>

Returns Iterable<T>

icompress

Returns an iterator that filters elements from data returning only those that have a corresponding element in selectors that evaluates to true. Stops when either the data or selectors iterables has been exhausted.

Parameters

  • data Iterable<T>
  • selectors Iterable<boolean>

Returns Iterable<T>

ifilter

Returns an iterator that filters elements from iterable returning only those for which the predicate is true.

Parameters

  • iterable Iterable<T>
  • predicate Predicate<T>

Returns Iterable<T>

imap

Returns an iterator that computes the given mapper function using arguments from each of the iterables.

Parameters

  • iterable Iterable<T>
  • mapper function (T): V

Returns Iterable<V>

islice

Returns an iterator that returns selected elements from the iterable. If start is non-zero, then elements from the iterable are skipped until start is reached. Then, elements are returned by making steps of step (defaults to 1). If set to higher than 1, items will be skipped. If stop is provided, then iteration continues until the iterator reached that index, otherwise, the iterable will be fully exhausted. islice() does not support negative values for start, stop, or step.

Parameters

Returns Iterable<T>

izip2

Returns an iterator that aggregates elements from each of the iterables. Used for lock-step iteration over several iterables at a time. When iterating over two iterables, use izip2. When iterating over three iterables, use izip3, etc. izip is an alias for izip2.

Parameters

  • xs Iterable<T1>
  • ys Iterable<T2>

Returns Iterable<[T1, T2]>

izip3

Like izip2, but for three input iterables.

Parameters

  • xs Iterable<T1>
  • ys Iterable<T2>
  • zs Iterable<T3>

Returns Iterable<[T1, T2, T3]>

izipAll

Like the other izip's, but generalized. Due to type system limitations, you can only "generially" zip iterables with homogeneous types, so you cannot mix types like <A, B> like you can with izip2()

Parameters

  • iters ...Array<Iterable<T>>

Returns Iterable<Array<T>>

permutations

Return successive r-length permutations of elements in the iterable.

If r is not specified, then r defaults to the length of the iterable and all possible full-length permutations are generated.

Permutations are emitted in lexicographic sort order. So, if the input iterable is sorted, the permutation tuples will be produced in sorted order.

Elements are treated as unique based on their position, not on their value. So if the input elements are unique, there will be no repeat values in each permutation.

Parameters

  • iterable Iterable<T>
  • r Maybe<number>

Returns Iterable<Array<T>>

takewhile

Returns an iterator that produces elements from the iterable as long as the predicate is true.

Parameters

  • iterable Iterable<T>
  • predicate Predicate<T>

Returns Iterable<T>

all

Returns true when all of the items in iterable are truthy. An optional key function can be used to define what truthiness means for this specific collection.

Examples:

all([])                           // => true
all([0])                          // => false
all([0, 1, 2])                    // => false
all([1, 2, 3])                    // => true

Examples with using a key function:

all([2, 4, 6], n => n % 2 === 0)  // => true
all([2, 4, 5], n => n % 2 === 0)  // => false

Parameters

  • iterable Iterable<T>
  • keyFn Predicate<T> (optional, default identityPredicate)

Returns boolean

any

Returns true when any of the items in iterable are truthy. An optional key function can be used to define what truthiness means for this specific collection.

Examples:

any([])                           // => false
any([0])                          // => false
any([0, 1, null, undefined])      // => true

Examples with using a key function:

any([1, 4, 5], n => n % 2 === 0)  // => true
any([{name: 'Bob'}, {name: 'Alice'}], person => person.name.startsWith('C'))  // => false

Parameters

  • iterable Iterable<T>
  • keyFn Predicate<T> (optional, default identityPredicate)

Returns boolean

contains

Returns true when any of the items in the iterable are equal to the target object.

Examples:

contains([], 'whatever')         // => false
contains([3], 42)                // => false
contains([3], 3)                 // => true
contains([0, 1, 2], 2)           // => true

Parameters

  • haystack Iterable<T>
  • needle T

Returns boolean

enumerate

Returns an iterable of enumeration pairs. Iterable must be a sequence, an iterator, or some other object which supports iteration. The elements produced by returns a tuple containing a counter value (starting from 0 by default) and the values obtained from iterating over given iterable.

Example:

import { enumerate } from 'itertools';

console.log([...enumerate(['hello', 'world'])]);
// [0, 'hello'], [1, 'world']]

Parameters

  • iterable Iterable<T>
  • start number (optional, default 0)

Returns Iterable<[number, T]>

filter

Non-lazy version of ifilter().

Parameters

  • iterable Iterable<T>
  • predicate Predicate<T>

Returns Array<T>

iter

Returns an iterator object for the given iterable. This can be used to manually get an iterator for any iterable datastructure. The purpose and main use case of this function is to get a single iterator (a thing with state, think of it as a "cursor") which can only be consumed once.

Parameters

  • iterable Iterable<T>

Returns Iterator<T>

map

Non-lazy version of imap().

Parameters

  • iterable Iterable<T>
  • mapper function (T): V

Returns Array<V>

max

Return the largest item in an iterable. Only works for numbers, as ordering is pretty poorly defined on any other data type in JS. The optional keyFn argument specifies a one-argument ordering function like that used for sorted().

If the iterable is empty, undefined is returned.

If multiple items are maximal, the function returns either one of them, but which one is not defined.

Parameters

  • iterable Iterable<T>
  • keyFn function (T): number (optional, default numberIdentity)

Returns Maybe<T>

min

Return the smallest item in an iterable. Only works for numbers, as ordering is pretty poorly defined on any other data type in JS. The optional keyFn argument specifies a one-argument ordering function like that used for sorted().

If the iterable is empty, undefined is returned.

If multiple items are minimal, the function returns either one of them, but which one is not defined.

Parameters

  • iterable Iterable<T>
  • keyFn function (T): number (optional, default numberIdentity)

Returns Maybe<T>

range

TODO

Parameters

Returns Iterable<number>

reduce

TODO: Reducer function with a start value.

Parameters

  • iterable Iterable<T>
  • reducer function (O, T, number): O
  • start O

Returns O

reduce_

TODO: Document Reducer function without a start value. The start value will be the first item in the input iterable. For this reason, the function may not necessarily return anything (i.e. if the input is empty), and the inputs and the outputs must be of homogeneous types.

Parameters

  • iterable Iterable<T>
  • reducer function (T, T, number): T

Returns Maybe<T>

sorted

TODO

Parameters

  • iterable Iterable<T>
  • keyFn function (T): Primitive (optional, default primitiveIdentity)
  • reverse boolean (optional, default false)

Returns Array<T>

sum

TODO

Parameters

Returns number

zip

TODO

Parameters

  • xs Iterable<T1>
  • ys Iterable<T2>

Returns Array<[T1, T2]>

zip3

TODO

Parameters

  • xs Iterable<T1>
  • ys Iterable<T2>
  • zs Iterable<T3>

Returns Array<[T1, T2, T3]>

itake

Returns an iterable containing only the first n elements of the given iterable.

Parameters

  • n number
  • iterable Iterable<T>

Returns Iterable<T>

take

Non-lazy version of itake().

Parameters

  • n number
  • iterable Iterable<T>

Returns Array<T>

more-itertools

itake

Returns an iterable containing only the first n elements of the given iterable.

Parameters

  • n number
  • iterable Iterable<T>

Returns Iterable<T>

take

Non-lazy version of itake().

Parameters

  • n number
  • iterable Iterable<T>

Returns Array<T>

all

Returns true when all of the items in iterable are truthy. An optional key function can be used to define what truthiness means for this specific collection.

Examples:

all([])                           // => true
all([0])                          // => false
all([0, 1, 2])                    // => false
all([1, 2, 3])                    // => true

Examples with using a key function:

all([2, 4, 6], n => n % 2 === 0)  // => true
all([2, 4, 5], n => n % 2 === 0)  // => false

Parameters

  • iterable Iterable<T>
  • keyFn Predicate<T> (optional, default identityPredicate)

Returns boolean

any

Returns true when any of the items in iterable are truthy. An optional key function can be used to define what truthiness means for this specific collection.

Examples:

any([])                           // => false
any([0])                          // => false
any([0, 1, null, undefined])      // => true

Examples with using a key function:

any([1, 4, 5], n => n % 2 === 0)  // => true
any([{name: 'Bob'}, {name: 'Alice'}], person => person.name.startsWith('C'))  // => false

Parameters

  • iterable Iterable<T>
  • keyFn Predicate<T> (optional, default identityPredicate)

Returns boolean

contains

Returns true when any of the items in the iterable are equal to the target object.

Examples:

contains([], 'whatever')         // => false
contains([3], 42)                // => false
contains([3], 3)                 // => true
contains([0, 1, 2], 2)           // => true

Parameters

  • haystack Iterable<T>
  • needle T

Returns boolean

enumerate

Returns an iterable of enumeration pairs. Iterable must be a sequence, an iterator, or some other object which supports iteration. The elements produced by returns a tuple containing a counter value (starting from 0 by default) and the values obtained from iterating over given iterable.

Example:

import { enumerate } from 'itertools';

console.log([...enumerate(['hello', 'world'])]);
// [0, 'hello'], [1, 'world']]

Parameters

  • iterable Iterable<T>
  • start number (optional, default 0)

Returns Iterable<[number, T]>

filter

Non-lazy version of ifilter().

Parameters

  • iterable Iterable<T>
  • predicate Predicate<T>

Returns Array<T>

iter

Returns an iterator object for the given iterable. This can be used to manually get an iterator for any iterable datastructure. The purpose and main use case of this function is to get a single iterator (a thing with state, think of it as a "cursor") which can only be consumed once.

Parameters

  • iterable Iterable<T>

Returns Iterator<T>

map

Non-lazy version of imap().

Parameters

  • iterable Iterable<T>
  • mapper function (T): V

Returns Array<V>

max

Return the largest item in an iterable. Only works for numbers, as ordering is pretty poorly defined on any other data type in JS. The optional keyFn argument specifies a one-argument ordering function like that used for sorted().

If the iterable is empty, undefined is returned.

If multiple items are maximal, the function returns either one of them, but which one is not defined.

Parameters

  • iterable Iterable<T>
  • keyFn function (T): number (optional, default numberIdentity)

Returns Maybe<T>

min

Return the smallest item in an iterable. Only works for numbers, as ordering is pretty poorly defined on any other data type in JS. The optional keyFn argument specifies a one-argument ordering function like that used for sorted().

If the iterable is empty, undefined is returned.

If multiple items are minimal, the function returns either one of them, but which one is not defined.

Parameters

  • iterable Iterable<T>
  • keyFn function (T): number (optional, default numberIdentity)

Returns Maybe<T>

range

TODO

Parameters

Returns Iterable<number>

reduce

TODO: Reducer function with a start value.

Parameters

  • iterable Iterable<T>
  • reducer function (O, T, number): O
  • start O

Returns O

reduce_

TODO: Document Reducer function without a start value. The start value will be the first item in the input iterable. For this reason, the function may not necessarily return anything (i.e. if the input is empty), and the inputs and the outputs must be of homogeneous types.

Parameters

  • iterable Iterable<T>
  • reducer function (T, T, number): T

Returns Maybe<T>

sorted

TODO

Parameters

  • iterable Iterable<T>
  • keyFn function (T): Primitive (optional, default primitiveIdentity)
  • reverse boolean (optional, default false)

Returns Array<T>

sum

TODO

Parameters

Returns number

zip

TODO

Parameters

  • xs Iterable<T1>
  • ys Iterable<T2>

Returns Array<[T1, T2]>

zip3

TODO

Parameters

  • xs Iterable<T1>
  • ys Iterable<T2>
  • zs Iterable<T3>

Returns Array<[T1, T2, T3]>

chain

Returns an iterator that returns elements from the first iterable until it is exhausted, then proceeds to the next iterable, until all of the iterables are exhausted. Used for treating consecutive sequences as a single sequence.

Parameters

  • iterables ...Array<Iterable<T>>

Returns Iterable<T>

count

Returns an iterator that counts up values starting with number start (default 0), incrementing by step. To decrement, use a negative step number.

Parameters

  • start number (optional, default 0)
  • step number (optional, default 1)

Returns Iterable<number>

compress

Non-lazy version of icompress().

Parameters

  • data Iterable<T>
  • selectors Iterable<boolean>

Returns Array<T>

cycle

Returns an iterator producing elements from the iterable and saving a copy of each. When the iterable is exhausted, return elements from the saved copy. Repeats indefinitely.

Parameters

  • iterable Iterable<T>

Returns Iterable<T>

dropwhile

Returns an iterator that drops elements from the iterable as long as the predicate is true; afterwards, returns every remaining element. Note, the iterator does not produce any output until the predicate first becomes false.

Parameters

  • iterable Iterable<T>
  • predicate Predicate<T>

Returns Iterable<T>

icompress

Returns an iterator that filters elements from data returning only those that have a corresponding element in selectors that evaluates to true. Stops when either the data or selectors iterables has been exhausted.

Parameters

  • data Iterable<T>
  • selectors Iterable<boolean>

Returns Iterable<T>

ifilter

Returns an iterator that filters elements from iterable returning only those for which the predicate is true.

Parameters

  • iterable Iterable<T>
  • predicate Predicate<T>

Returns Iterable<T>

imap

Returns an iterator that computes the given mapper function using arguments from each of the iterables.

Parameters

  • iterable Iterable<T>
  • mapper function (T): V

Returns Iterable<V>

islice

Returns an iterator that returns selected elements from the iterable. If start is non-zero, then elements from the iterable are skipped until start is reached. Then, elements are returned by making steps of step (defaults to 1). If set to higher than 1, items will be skipped. If stop is provided, then iteration continues until the iterator reached that index, otherwise, the iterable will be fully exhausted. islice() does not support negative values for start, stop, or step.

Parameters

Returns Iterable<T>

izip2

Returns an iterator that aggregates elements from each of the iterables. Used for lock-step iteration over several iterables at a time. When iterating over two iterables, use izip2. When iterating over three iterables, use izip3, etc. izip is an alias for izip2.

Parameters

  • xs Iterable<T1>
  • ys Iterable<T2>

Returns Iterable<[T1, T2]>

izip3

Like izip2, but for three input iterables.

Parameters

  • xs Iterable<T1>
  • ys Iterable<T2>
  • zs Iterable<T3>

Returns Iterable<[T1, T2, T3]>

izipAll

Like the other izip's, but generalized. Due to type system limitations, you can only "generially" zip iterables with homogeneous types, so you cannot mix types like <A, B> like you can with izip2()

Parameters

  • iters ...Array<Iterable<T>>

Returns Iterable<Array<T>>

permutations

Return successive r-length permutations of elements in the iterable.

If r is not specified, then r defaults to the length of the iterable and all possible full-length permutations are generated.

Permutations are emitted in lexicographic sort order. So, if the input iterable is sorted, the permutation tuples will be produced in sorted order.

Elements are treated as unique based on their position, not on their value. So if the input elements are unique, there will be no repeat values in each permutation.

Parameters

  • iterable Iterable<T>
  • r Maybe<number>

Returns Iterable<Array<T>>

takewhile

Returns an iterator that produces elements from the iterable as long as the predicate is true.

Parameters

  • iterable Iterable<T>
  • predicate Predicate<T>

Returns Iterable<T>

Additions

compactObject

Removes all undefined values from the given object. Returns a new object.

Examples: compactObject({ a: 1, b: undefined, c: 0 }) // ==> { a: 1, c: 0 }

Parameters

  • obj O

Returns $ObjMap<O, function (Maybe<T>): T>

flatmap

Returns 0 or more values for every value in the given iterable

Examples:

 Get Ids for an Author and its aliases
 flatmap(author => [author.id, ...author.aliases.map(a => a.id)], authors)

Parameters

  • iterable Iterable<T>
  • mapper function (T): Iterable<S>

Returns Iterable<S>

chain

Returns an iterator that returns elements from the first iterable until it is exhausted, then proceeds to the next iterable, until all of the iterables are exhausted. Used for treating consecutive sequences as a single sequence.

Parameters

  • iterables ...Array<Iterable<T>>

Returns Iterable<T>

count

Returns an iterator that counts up values starting with number start (default 0), incrementing by step. To decrement, use a negative step number.

Parameters

  • start number (optional, default 0)
  • step number (optional, default 1)

Returns Iterable<number>

compress

Non-lazy version of icompress().

Parameters

  • data Iterable<T>
  • selectors Iterable<boolean>

Returns Array<T>

cycle

Returns an iterator producing elements from the iterable and saving a copy of each. When the iterable is exhausted, return elements from the saved copy. Repeats indefinitely.

Parameters

  • iterable Iterable<T>

Returns Iterable<T>

dropwhile

Returns an iterator that drops elements from the iterable as long as the predicate is true; afterwards, returns every remaining element. Note, the iterator does not produce any output until the predicate first becomes false.

Parameters

  • iterable Iterable<T>
  • predicate Predicate<T>

Returns Iterable<T>

icompress

Returns an iterator that filters elements from data returning only those that have a corresponding element in selectors that evaluates to true. Stops when either the data or selectors iterables has been exhausted.

Parameters

  • data Iterable<T>
  • selectors Iterable<boolean>

Returns Iterable<T>

ifilter

Returns an iterator that filters elements from iterable returning only those for which the predicate is true.

Parameters

  • iterable Iterable<T>
  • predicate Predicate<T>

Returns Iterable<T>

imap

Returns an iterator that computes the given mapper function using arguments from each of the iterables.

Parameters

  • iterable Iterable<T>
  • mapper function (T): V

Returns Iterable<V>

islice

Returns an iterator that returns selected elements from the iterable. If start is non-zero, then elements from the iterable are skipped until start is reached. Then, elements are returned by making steps of step (defaults to 1). If set to higher than 1, items will be skipped. If stop is provided, then iteration continues until the iterator reached that index, otherwise, the iterable will be fully exhausted. islice() does not support negative values for start, stop, or step.

Parameters

Returns Iterable<T>

izip2

Returns an iterator that aggregates elements from each of the iterables. Used for lock-step iteration over several iterables at a time. When iterating over two iterables, use izip2. When iterating over three iterables, use izip3, etc. izip is an alias for izip2.

Parameters

  • xs Iterable<T1>
  • ys Iterable<T2>

Returns Iterable<[T1, T2]>

izip3

Like izip2, but for three input iterables.

Parameters

  • xs Iterable<T1>
  • ys Iterable<T2>
  • zs Iterable<T3>

Returns Iterable<[T1, T2, T3]>

izipAll

Like the other izip's, but generalized. Due to type system limitations, you can only "generially" zip iterables with homogeneous types, so you cannot mix types like <A, B> like you can with izip2()

Parameters

  • iters ...Array<Iterable<T>>

Returns Iterable<Array<T>>

permutations

Return successive r-length permutations of elements in the iterable.

If r is not specified, then r defaults to the length of the iterable and all possible full-length permutations are generated.

Permutations are emitted in lexicographic sort order. So, if the input iterable is sorted, the permutation tuples will be produced in sorted order.

Elements are treated as unique based on their position, not on their value. So if the input elements are unique, there will be no repeat values in each permutation.

Parameters

  • iterable Iterable<T>
  • r Maybe<number>

Returns Iterable<Array<T>>

takewhile

Returns an iterator that produces elements from the iterable as long as the predicate is true.

Parameters

  • iterable Iterable<T>
  • predicate Predicate<T>

Returns Iterable<T>

itake

Returns an iterable containing only the first n elements of the given iterable.

Parameters

  • n number
  • iterable Iterable<T>

Returns Iterable<T>

take

Non-lazy version of itake().

Parameters

  • n number
  • iterable Iterable<T>

Returns Array<T>

all

Returns true when all of the items in iterable are truthy. An optional key function can be used to define what truthiness means for this specific collection.

Examples:

all([])                           // => true
all([0])                          // => false
all([0, 1, 2])                    // => false
all([1, 2, 3])                    // => true

Examples with using a key function:

all([2, 4, 6], n => n % 2 === 0)  // => true
all([2, 4, 5], n => n % 2 === 0)  // => false

Parameters

  • iterable Iterable<T>
  • keyFn Predicate<T> (optional, default identityPredicate)

Returns boolean

any

Returns true when any of the items in iterable are truthy. An optional key function can be used to define what truthiness means for this specific collection.

Examples:

any([])                           // => false
any([0])                          // => false
any([0, 1, null, undefined])      // => true

Examples with using a key function:

any([1, 4, 5], n => n % 2 === 0)  // => true
any([{name: 'Bob'}, {name: 'Alice'}], person => person.name.startsWith('C'))  // => false

Parameters

  • iterable Iterable<T>
  • keyFn Predicate<T> (optional, default identityPredicate)

Returns boolean

contains

Returns true when any of the items in the iterable are equal to the target object.

Examples:

contains([], 'whatever')         // => false
contains([3], 42)                // => false
contains([3], 3)                 // => true
contains([0, 1, 2], 2)           // => true

Parameters

  • haystack Iterable<T>
  • needle T

Returns boolean

enumerate

Returns an iterable of enumeration pairs. Iterable must be a sequence, an iterator, or some other object which supports iteration. The elements produced by returns a tuple containing a counter value (starting from 0 by default) and the values obtained from iterating over given iterable.

Example:

import { enumerate } from 'itertools';

console.log([...enumerate(['hello', 'world'])]);
// [0, 'hello'], [1, 'world']]

Parameters

  • iterable Iterable<T>
  • start number (optional, default 0)

Returns Iterable<[number, T]>

filter

Non-lazy version of ifilter().

Parameters

  • iterable Iterable<T>
  • predicate Predicate<T>

Returns Array<T>

iter

Returns an iterator object for the given iterable. This can be used to manually get an iterator for any iterable datastructure. The purpose and main use case of this function is to get a single iterator (a thing with state, think of it as a "cursor") which can only be consumed once.

Parameters

  • iterable Iterable<T>

Returns Iterator<T>

map

Non-lazy version of imap().

Parameters

  • iterable Iterable<T>
  • mapper function (T): V

Returns Array<V>

max

Return the largest item in an iterable. Only works for numbers, as ordering is pretty poorly defined on any other data type in JS. The optional keyFn argument specifies a one-argument ordering function like that used for sorted().

If the iterable is empty, undefined is returned.

If multiple items are maximal, the function returns either one of them, but which one is not defined.

Parameters

  • iterable Iterable<T>
  • keyFn function (T): number (optional, default numberIdentity)

Returns Maybe<T>

min

Return the smallest item in an iterable. Only works for numbers, as ordering is pretty poorly defined on any other data type in JS. The optional keyFn argument specifies a one-argument ordering function like that used for sorted().

If the iterable is empty, undefined is returned.

If multiple items are minimal, the function returns either one of them, but which one is not defined.

Parameters

  • iterable Iterable<T>
  • keyFn function (T): number (optional, default numberIdentity)

Returns Maybe<T>

range

TODO

Parameters

Returns Iterable<number>

reduce

TODO: Reducer function with a start value.

Parameters

  • iterable Iterable<T>
  • reducer function (O, T, number): O
  • start O

Returns O

reduce_

TODO: Document Reducer function without a start value. The start value will be the first item in the input iterable. For this reason, the function may not necessarily return anything (i.e. if the input is empty), and the inputs and the outputs must be of homogeneous types.

Parameters

  • iterable Iterable<T>
  • reducer function (T, T, number): T

Returns Maybe<T>

sorted

TODO

Parameters

  • iterable Iterable<T>
  • keyFn function (T): Primitive (optional, default primitiveIdentity)
  • reverse boolean (optional, default false)

Returns Array<T>

sum

TODO

Parameters

Returns number

zip

TODO

Parameters

  • xs Iterable<T1>
  • ys Iterable<T2>

Returns Array<[T1, T2]>

zip3

TODO

Parameters

  • xs Iterable<T1>
  • ys Iterable<T2>
  • zs Iterable<T3>

Returns Array<[T1, T2, T3]>

About

JavaScript port of Python's awesome itertools stdlib

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%