Options
All
  • Public
  • Public/Protected
  • All
Menu

elbe

Index

Variables

Const InplaceStreamFactory

InplaceStreamFactory: IStreamFactory = createFactory(true)

This implements the factory methods as defined by IStreamFactory.

Creates instances of InplaceStream. An inplace stream does not create instances when chaining methods on the stream, but switches out the underlying iterable is is thus not immutable. This requires casting and it makes it unsafe with typescript. When a stream object is accessed again after chaining, no error can be thrown. The InplaceStream is slightly faster as it does not need to create new instances.

const stream1 = require("elbe").InplaceStreamFactory.stream("foo");
// Returns an instance of IStream<string>

const stream2 = stream1.map(x => x.charCodeAt(0));
// Returns an instance of IStream<number>

// Now stream1 and stream2 refer to the same object, but are deemed
// to be of differing types by the typescript compiler.
stream1 === stream2 // => true

stream2.toArray();
// => [102, 11, 111]

// This should throw an error because stream1 was consumed
// when it got mapped, but this is not possible with InplaceStream.
stream1.toArray();
see

IStreamFactory

Const TypesafeStreamFactory

TypesafeStreamFactory: IStreamFactory = createFactory(false)

This implements the factory methods as defined by IStreamFactory.

Creates instances of TypesafeStream. A typesafe stream creates new instances when chaining methods on the stream. This makes it safe to use with typescript. When an old stream instance is accessed again after chaining, an error can be thrown to indicate this fact.

const stream1 = require("elbe").TypesafeStreamFactory.stream("foo");
// Returns an instance of IStream<string>

const stream2 = stream1.map(x => x.charCodeAt(0));
// Returns an instance of IStream<number>

// Now stream1 and stream2 are of different type and do not
// refer to the same object. The call to #map created a new
// stream instance.
stream1 === stream2 // => false

// Throws an error because stream1 because the mapping operation
// already consumed the stream.
stream1.toArray();
see

IStreamFactory

Const factory

factory: IStreamFactory = InplaceStreamFactory

A shortcut for InplaceStreamFactory.

const { factory } = require("elbe")

factory.stream([1,2,3]) // => Stream[1, 2, 3]

factory.times(5) // => Stream[0, 1, 2, 3, 4]

Const hasOwnProperty

hasOwnProperty: hasOwnProperty = Object.prototype.hasOwnProperty

Const stream

stream: stream = InplaceStreamFactory.stream

A shortcut for InplaceStreamFactory#stream. Use this to create streams quickly.

const stream = require("elbe").stream("foobar")
stream.size() // => 6

Functions

appendCause

  • appendCause(error: Error, cause: Error): void
  • Parameters

    • error: Error
    • cause: Error

    Returns void

chunk

  • chunk<T>(iterable: Iterable<T>, chunkSize: 0): Iterable<never>
  • chunk<T>(iterable: Iterable<T>, chunkSize: 1): Iterable<Single<T>>
  • chunk<T>(iterable: Iterable<T>, chunkSize: 2): Iterable<Pair<T> | Single<T>>
  • chunk<T>(iterable: Iterable<T>, chunkSize: 3): Iterable<Triple<T> | Pair<T> | Single<T>>
  • chunk<T>(iterable: Iterable<T>, chunkSize: 4): Iterable<Quadruple<T> | Triple<T> | Pair<T> | Single<T>>
  • chunk<T>(iterable: Iterable<T>, chunkSize: 5): Iterable<Quintuple<T> | Quadruple<T> | Triple<T> | Pair<T> | Single<T>>
  • chunk<T>(iterable: Iterable<T>, chunkSize: 6): Iterable<Sextuple<T> | Quintuple<T> | Quadruple<T> | Triple<T> | Pair<T> | Single<T>>
  • chunk<T>(iterable: Iterable<T>, chunkSize: 7): Iterable<Septuple<T> | Sextuple<T> | Quintuple<T> | Quadruple<T> | Triple<T> | Pair<T> | Single<T>>
  • chunk<T>(iterable: Iterable<T>, chunkSize: 8): Iterable<Octuple<T> | Septuple<T> | Sextuple<T> | Quintuple<T> | Quadruple<T> | Triple<T> | Pair<T> | Single<T>>
  • chunk<T>(iterable: Iterable<T>, chunkSize: 9): Iterable<Nonuple<T> | Octuple<T> | Septuple<T> | Sextuple<T> | Quintuple<T> | Quadruple<T> | Triple<T> | Pair<T> | Single<T>>
  • chunk<T>(iterable: Iterable<T>, chunkSize: 10): Iterable<Decuple<T> | Nonuple<T> | Octuple<T> | Septuple<T> | Sextuple<T> | Quintuple<T> | Quadruple<T> | Triple<T> | Pair<T> | Single<T>>
  • chunk<T>(iterable: Iterable<T>, chunkSize: number): Iterable<T[]>
  • Overload with tuple types for chunk(chunkSize: number): Iterable<T[]>

    Type parameters

    • T

    Parameters

    • iterable: Iterable<T>
    • chunkSize: 0

    Returns Iterable<never>

  • Overload with tuple types for chunk(chunkSize: number): Iterable<T[]>

    Type parameters

    • T

    Parameters

    • iterable: Iterable<T>
    • chunkSize: 1

    Returns Iterable<Single<T>>

  • Overload with tuple types for chunk(chunkSize: number): Iterable<T[]>

    Type parameters

    • T

    Parameters

    • iterable: Iterable<T>
    • chunkSize: 2

    Returns Iterable<Pair<T> | Single<T>>

  • Overload with tuple types for chunk(chunkSize: number): Iterable<T[]>

    Type parameters

    • T

    Parameters

    • iterable: Iterable<T>
    • chunkSize: 3

    Returns Iterable<Triple<T> | Pair<T> | Single<T>>

  • Overload with tuple types for chunk(chunkSize: number): Iterable<T[]>

    Type parameters

    • T

    Parameters

    • iterable: Iterable<T>
    • chunkSize: 4

    Returns Iterable<Quadruple<T> | Triple<T> | Pair<T> | Single<T>>

  • Overload with tuple types for chunk(chunkSize: number): Iterable<T[]>

    Type parameters

    • T

    Parameters

    • iterable: Iterable<T>
    • chunkSize: 5

    Returns Iterable<Quintuple<T> | Quadruple<T> | Triple<T> | Pair<T> | Single<T>>

  • Overload with tuple types for chunk(chunkSize: number): Iterable<T[]>

    Type parameters

    • T

    Parameters

    • iterable: Iterable<T>
    • chunkSize: 6

    Returns Iterable<Sextuple<T> | Quintuple<T> | Quadruple<T> | Triple<T> | Pair<T> | Single<T>>

  • Overload with tuple types for chunk(chunkSize: number): Iterable<T[]>

    Type parameters

    • T

    Parameters

    • iterable: Iterable<T>
    • chunkSize: 7

    Returns Iterable<Septuple<T> | Sextuple<T> | Quintuple<T> | Quadruple<T> | Triple<T> | Pair<T> | Single<T>>

  • Overload with tuple types for chunk(chunkSize: number): Iterable<T[]>

    Type parameters

    • T

    Parameters

    • iterable: Iterable<T>
    • chunkSize: 8

    Returns Iterable<Octuple<T> | Septuple<T> | Sextuple<T> | Quintuple<T> | Quadruple<T> | Triple<T> | Pair<T> | Single<T>>

  • Overload with tuple types for chunk(chunkSize: number): Iterable<T[]>

    Type parameters

    • T

    Parameters

    • iterable: Iterable<T>
    • chunkSize: 9

    Returns Iterable<Nonuple<T> | Octuple<T> | Septuple<T> | Sextuple<T> | Quintuple<T> | Quadruple<T> | Triple<T> | Pair<T> | Single<T>>

  • Overload with tuple types for chunk(chunkSize: number): Iterable<T[]>

    Type parameters

    • T

    Parameters

    • iterable: Iterable<T>
    • chunkSize: 10

    Returns Iterable<Decuple<T> | Nonuple<T> | Octuple<T> | Septuple<T> | Sextuple<T> | Quintuple<T> | Quadruple<T> | Triple<T> | Pair<T> | Single<T>>

  • Chunks the items into chunks of chunkSize. This is equivalent to chunkBy(iterable, (_, i) => Math.floor(i/chunkSize)).

    chunk([1,2,3,4,5], 2) // => Iterable[ [1,2], [3,4], [5] ]
    chunk([1,2,3,4,5], 1) // => Iterable[ [1], [2], [3], [4], [5] ]
    chunk([1,2,3,4,5], 0) // => Iterable[]
    chunk([1,2,3,4,5], -1) // => Iterable[]
    chunk([1,2,3,4,5], NaN) // => Iterable[]
    chunk([1,2,3,4,5], Infinity) // => Iterable[[1,2,3,4,5]]
    

    Type parameters

    • T

      Type of the elements in the given iterable.

    Parameters

    • iterable: Iterable<T>

      The iterable to be chunked.

    • chunkSize: number

      Size of the produced chunks.

    Returns Iterable<T[]>

    An iterable over the chunked items.

chunkBy

  • chunkBy<T, K>(iterable: Iterable<T>, classifier: TypedBiFunction<T, number, K>): Iterable<T[]>
  • Chunks together consecutive items for which the classifier returns the same value. Equality is checked with ===.

    chunkBy([1,2,3,4,5,6,1,2], i => Math.floor((i-1) / 3)) // => Stream[ [1,2,3], [4,5,6], [1,2] ]
    

    Type parameters

    • T

      Type of the elements in the given iterable.

    • K

      Type of the returned value of the chunker,

    Parameters

    • iterable: Iterable<T>

      The iterable to be chunked.

    • classifier: TypedBiFunction<T, number, K>

      It is passed the item as its first argument and the index as its second. Items are chunked together according to the returned value.

    Returns Iterable<T[]>

    An iterable over the chunked items.

collect

  • collect<T, S, R>(iterable: Iterable<T>, collector: Collector<T, S, R>): R
  • Creates and object an incorporates all items into that object.

    collect([1,2,3], Collectors.summarize) // => Statistic[min:1, max:3, count: 3, sum: 6, average: 2, variance: 0.67]
    

    Type parameters

    • T

      Type of the elements in the given iterable.

    • S

      Type of the intermediate value that incorporates each item in order.

    • R

      Type of the final collected value.

    Parameters

    • iterable: Iterable<T>

      The iterable to be collected.

    • collector: Collector<T, S, R>

      How to collect the items.

    Returns R

    The collected value.

collectWith

  • collectWith<T, S, R>(iterable: Iterable<T>, supplier: Supplier<S>, accumulator: BiConsumer<S, T>, finisher: TypedFunction<S, R>): R
  • Same as collect, but allows specifying the parts of the collector individually.

    collect([1,2,3], () => [], (array, x) => array.push(x), x => new Set(x))
    // => Set[1,2,3]
    

    Type parameters

    • T

      Type of the elements in the given iterable.

    • S

      Type of the intermediate value that incorporates each item in order.

    • R

      Type of the final collected value.

    Parameters

    • iterable: Iterable<T>

      The iterable to be collected.

    • supplier: Supplier<S>
    • accumulator: BiConsumer<S, T>

      Takes the intermediate objects as its first argument and the current items as its seconds, and incorporates the item into the intermediate value.

    • finisher: TypedFunction<S, R>

      Takes the intermediate object with all the items incoporated, and transforms it into the final value.

    Returns R

    The final collected value.

concat

  • concat<T>(...iterables: Iterable<T>[]): Iterable<T>
  • Concatenates all iterables into one iterable of all the items.

    concat("foo", "bar", "baz") // => Iterable["f", "o", "o", "b", "a", "r", "b", "a", "z"]
    

    Type parameters

    • T

      Type of the elements in the given iterable.

    Parameters

    • Rest ...iterables: Iterable<T>[]

    Returns Iterable<T>

    An iterable over all the items of the given iterables.

consume

  • consume<T>(iterable: Iterable<T>, sink: T[] | Consumer<T>, offset?: number, maxAmount?: number): Iterable<T>
  • Consumes the given amount of items from the iterable, adding it to the sink, and returns an iterable over the remaining items.

    const sink = [];
    consume("foobar", sink, 0, 3);
    // => sink is now ["f", "o", "o"]
    
    consume("foobar", console.log, 0, 3);
    // => logs "f", "o", "o"
    
    consume("foobar", console.log, 0, -3);
    // => logs nothing
    

    Type parameters

    • T

      Type of the elements in the given iterable.

    Parameters

    • iterable: Iterable<T>

      The iterable to be scanned.

    • sink: T[] | Consumer<T>

      If an array, consmed items are added to the array. Otherwise, the consumer is called with the consumed item.

    • Default value offset: number = 0

      Where to start removing items from the iterable. Defaults to 0.

    • Default value maxAmount: number = Infinity

      Maximum number of items to consume. Defaults to Infinity.

    Returns Iterable<T>

    An iterable over the remaining items.

consumeFirst

  • consumeFirst<T>(iterable: Iterable<T>, sink: T[] | Consumer<T>): Iterable<T>
  • Type parameters

    • T

    Parameters

    • iterable: Iterable<T>
    • sink: T[] | Consumer<T>

    Returns Iterable<T>

createFactory

cycle

  • cycle<T>(iterable: Iterable<T>, count?: number): Iterable<T>
  • Cycles over the elements of the iterable the given number of times.

    cycle([1,2,3], NaN) // => Iterable[]
    cycle([1,2,3], 3) // => Iterable[1,2,3,1,2,3,1,2,3]
    limit(cycle([1,2,3]), 5) // => Iterable[1,2,3,1,2]
    

    Type parameters

    • T

      Type of the elements in the given iterable.

    Parameters

    • iterable: Iterable<T>

      The iterable to be cycled.

    • Default value count: number = Infinity

      The number of cycle. If not given, cycles an unlimited amount of times.

    Returns Iterable<T>

    An iterable with the items of the given iterable repeating.

end

  • end<T>(iterable: Iterable<T>): void
  • Applies all pending operations, ending the given iterable.

    visit([1,2,3], console.log) // Iterable[1,2,3]; prints nothing
    end(visit([1,2,3], console.log)) // prints 1,2,3
    

    Type parameters

    • T

      Type of the elements in the given iterable.

    Parameters

    • iterable: Iterable<T>

      The iterable to be terminated.

    Returns void

every

  • every<T>(iterable: Iterable<T>, predicate: Predicate<T>): boolean
  • Determines whether every items matches the given predicate.

    every("fooz", x => x < "j") // => false
    

    Type parameters

    • T

      Type of the elements in the given iterable.

    Parameters

    • iterable: Iterable<T>

      The iterable to be scanned.

    • predicate: Predicate<T>

      Test to be performed on the items.

    Returns boolean

    Whether every items matches the given predicate.

filter

  • filter<T>(iterable: Iterable<T>, predicate: Predicate<T>): Iterable<T>
  • Removes all elements from the iterable for which the predicate does not return true.

    filter([4,-4,2,-9], x => x > 0) // => Iterable[4,2]
    

    Type parameters

    • T

      Type of the elements in the given iterable.

    Parameters

    • iterable: Iterable<T>

      The iterable to be filtered.

    • predicate: Predicate<T>

      Testing function returning true iff the item is to be kept, false otherwise.

    Returns Iterable<T>

    An iterable over all item for which the predicate returned true.

filterBy

  • filterBy<T, K>(iterable: Iterable<T>, target: K, keyExtractor: TypedFunction<T, K>, comparator?: Comparator<K>): Iterable<T>
  • Similar to {@link Methods}#filter, but filters out all items not equivalent to the given target. Items are compared to the target by first extracting a key with the given key extractor, and then comparing the keys with the given comparator.

    filterBy(["foo", "bar", "foobar"], x => x.length)
    // => Iterable["foo", "bar"]
    
    const user1 = {name: "Dave", birth: {day: 5, month: 4, year: 2005}}
    const user2 = {name: "Maria", birth: {day: 9, month: 11, year: 2005}}
    const user3 = {name: "Odo", birth: {day: 22, month: 7, year: 2004}}
    
    filterBy([user1, user2, user3], 2005, user => user.birth, (lhs,rhs) => lhs.year - rhs.year)
    // => Iterable[user1, user2]
    

    Type parameters

    • T

      Type of the elements in the given iterable.

    • K

    Parameters

    • iterable: Iterable<T>

      The iterable to be counted.

    • target: K

      Target for filterting. All items in the iterable not equivalent to the target are removed.

    • keyExtractor: TypedFunction<T, K>

      Extracts the key by which equality is determined. Default to identity x => x.

    • Optional comparator: Comparator<K>

      Comparator for comparing two keys. Defaults to the natural comparator using < and >.

    Returns Iterable<T>

    An iterable with the items not matching the target removed.

find

  • find<T>(iterable: Iterable<T>, predicate: BiPredicate<T, number>): Maybe<T>
  • Searches for the first occurence of an item matching the predicate.

    find(["foo1", "bar,"foo2"], x => x.startsWith("foo")) // => "foo1"
    

    Type parameters

    • T

      Type of the elements in the given iterable.

    Parameters

    • iterable: Iterable<T>

      The iterable to be scanned.

    • predicate: BiPredicate<T, number>

      Test to be performed on the items. It is passed the current item and the current index.

    Returns Maybe<T>

    The item iff found, undefined otherwise.

findIndex

  • findIndex<T>(iterable: Iterable<T>, predicate: BiPredicate<T, number>): number
  • Searches for the first occurence of an item matching the predicate, and returns the index; or -1 otherwise.

    findIndex(["foo1", "bar, "foo2"], x => x.startsWith("foo")) // => 0
    

    Type parameters

    • T

      Type of the elements in the given iterable.

    Parameters

    • iterable: Iterable<T>

      The iterable to be scanned.

    • predicate: BiPredicate<T, number>

      Test to be performed on the items. It is passed the current item and the current index.

    Returns number

    The index iff the item was found, -1 otherwise.

first

  • first<T>(iterable: Iterable<T>): Maybe<T>
  • Returns the first item.

    first("foo") // => "f"
    first("") // => undefined
    

    Type parameters

    • T

      Type of the elements in the given iterable.

    Parameters

    • iterable: Iterable<T>

      The iterable to be scanned.

    Returns Maybe<T>

    The item at the first position, or undefined if empty.

flatMap

  • flatMap<T, S>(iterable: Iterable<T>, mapper: TypedFunction<T, Iterable<S>>): Iterable<S>
  • Applies the mapping function to each item and return an iterable over all the mapped iterables. This is equivalent to concat(map(mapper)).

    flatMap(["foo","bar"], x => fromString(x)) // => Iterable["f", "o", "o", "b", "a", "r"]
    flatMap(doTry(["[1]","[2,3]","[4,]"], JSON.parse), x=>x.iterate()) // => Iterable[ [1], [2, 3] ]
    

    Type parameters

    • T

      Type of the elements in the given iterable.

    • S

      Type of the elements in the produced iterable.

    Parameters

    • iterable: Iterable<T>

      The iterable to be flat-mapped.

    • mapper: TypedFunction<T, Iterable<S>>

      Mapping function taking each item and producing a new iterable.

    Returns Iterable<S>

    An iterable over all the items of the iterables produced by the mapper.

fork

  • fork<T>(iterable: Iterable<T>): Iterable<T>
  • This returns an iterable that can be iterated over any number of times. If the iterable is an array, set, map or string etc, it simpy return the iterable, otherwise it stores the items temporarily.

    function * foo() {
      return 1;
      return 2;
      return 3;
    }
    
    const transientIterable = foo();
    Array.from(transientIterable) // => [1,2,3]
    Array.from(transientIterable) // => []
    
    const persistentIterable = fork(foo());
    Array.from(persistentIterable) // => [1,2,3]
    Array.from(persistentIterable) // => [1,2,3]
    

    Note that buffering takes place on-demand, so the following will not enter an infinite loop:

    // Create an iterable with an unlimited amount of items.
    const iterable = generate(Math.random, Infinity);
    
    // Fork the iterable first, then limit to a finite number of items.
    // Items already produced are not recomputed.
    Array.from(limit(fork(iterable), 3)) // => [0.28, 0.14, 0.97]
    Array.from(limit(fork(iterable), 2)) // => [0.28, 0.14]
    Array.from(limit(fork(iterable), 4)) // => [0.28, 0.14, 0.97, 0.31]
    

    Type parameters

    • T

      Type of the elements in the given iterable.

    Parameters

    • iterable: Iterable<T>

      The iterable to to be persisted.

    Returns Iterable<T>

    A persistent iterable.

fromIter

  • fromIter<T>(iterableOrIterator: Iterable<T> | Iterator<T>): Iterable<T>
  • Creates an iterable from the given iterable or iterator. If it is an iterable, returns that iterable, otherwise create an iterabl reading from the iterator.

    fromIter([1,2,3]) // => Iterable[1,2,3]
    
    const set = new Set([1,2,3])
    fromIter(set.values().next()) // => Iterable[2,3]
    

    Type parameters

    • T

      Type of the generated items.

    Parameters

    • iterableOrIterator: Iterable<T> | Iterator<T>

      Iterable or iterator from which to read the items.

    Returns Iterable<T>

    An iterable over the iterable's or the iterator's items.

fromObject

  • fromObject<T>(object: object): Iterable<object>
  • Creates an iterable for iterating over an object's key-value-pairs. Only the object's own property are included.

    fromObject({foo:2, bar: 3})
    // => Iterable[ {key: "foo", value: 2}, {key: "bar", value: 3} ]
    

    Type parameters

    • T

      Type of the object's values.

    Parameters

    • object: object

      The object with the key-value-pairs to be iterated.

      • [s: string]: T

    Returns Iterable<object>

    An iterable with the object's key-value-pairs.

fromObjectKeys

  • fromObjectKeys(object: object): Iterable<string>
  • Creates an iterable for iterating over an object's keys. Only the object's own property are included.

    fromObjectKeys({foo:2, bar: 3, 42: 9})
    // => Iterable["foo", "bar", "42"]
    

    Parameters

    • object: object

      The object with the keys to be iterated.

      • [s: string]: any

    Returns Iterable<string>

    An iterable with the object's keys.

fromObjectValues

  • fromObjectValues<T>(object: object): Iterable<T>
  • Creates an iterable for iterating over an object's values. Only the object's own property are included.

    fromObjectValues({foo:2, bar: 3})
    // => Iterable[2,3]
    

    Type parameters

    • T

      Type of the object's values.

    Parameters

    • object: object

      The object with the values to be iterated.

      • [s: string]: T

    Returns Iterable<T>

    An iterable with the object's values.

generate

  • generate<T>(generator: TypedFunction<number, T>, amount?: number): Iterable<T>
  • Creates an iterable with the items provided by the given generator.

    generate(index => index, 2) // => Iterable[0,1,2]
    generate(index => index, 0) // => Iterable[]
    generate(index => index) // => Iterable[0,1,2,3,4,...]
    generate(index => index, Infinity) // => Iterable[0,1,2,3,4,...]
    generate(index => index, -Infinity) // => Iterable[]
    generate(index => index, NaN) // => Iterable[]
    

    Type parameters

    • T

      Type of the generated items.

    Parameters

    • generator: TypedFunction<number, T>

      Generator for generating the items of the iterable. It is passed the current index as its argument.

    • Default value amount: number = Infinity

      How many items to generate, Infinity for an unlimited amount.

    Returns Iterable<T>

    Iterable for iterating the given amount of times over the items supplied by the supplier.

group

  • group<T, K>(iterable: Iterable<T>, classifier: TypedFunction<T, K>): Map<K, T[]>
  • Splits the items into several groups, according to the given classifier.

    group(["a", "foo", "is", "some", "buzz"], x => x.length)
    // => Map [ 1 => ["a"], 2 => ["is"], 3 => "is", 4 => ["some", "buzz"] ]
    
    typearam

    K Type of the group key.

    Type parameters

    • T

      Type of the elements in the given iterable.

    • K

    Parameters

    • iterable: Iterable<T>

      The iterable to be grouped.

    • classifier: TypedFunction<T, K>

      Returns the group for each item.

    Returns Map<K, T[]>

    A map with the groups as keys and arrays as values, containg all the items for that group.

has

  • has<T>(iterable: Iterable<T>, object: T): boolean
  • Determines whether the iterable contains the given item. Equivalence is checked with ===. This is equivalent to some(iterable, x => x === object).

    has("foobar", "o") // => true
    

    Type parameters

    • T

      Type of the elements in the given iterable.

    Parameters

    • iterable: Iterable<T>

      The iterable to be scanned.

    • object: T

    Returns boolean

    Whether the given object is contained in the iterable.

identity

  • identity<T>(x: T): any
  • Type parameters

    • T

    Parameters

    • x: T

    Returns any

index

  • index<T>(iterable: Iterable<T>): Iterable<object>
  • Adds the index to each element of the given iterable. The index starts at 0

    index(["foo", "bar"]) // => Iterable<[ {index: 0, value: "foo"}, {index: 1, value: "bar"} ]>
    

    Type parameters

    • T

      Type of the element in the iterable.

    Parameters

    • iterable: Iterable<T>

      The iterable to be indexed.

    Returns Iterable<object>

    An iterable with each item being an object containing the item's index and the item itself. The index starts at 0.

isEmpty

  • Determines whether the iterable contains no items. This consumes at most one item from the iterable and works with infinite iterables.

    isEmpty([]).result      // => true
    isEmpty([1,2,3]).result // => false
    
    see

    isSizeBetween

    infinite_iterable

    Does not hang when used on infinite (unlimited) iterables.

    Type parameters

    • T

      Type of the elements in the given iterable.

    Parameters

    • iterable: Iterable<T>

    Returns IForkedResult<T, boolean>

    True iff the iterable contains no items or false otherwise; and an iterable over the original items.

isSizeBetween

  • isSizeBetween<T>(iterable: Iterable<T>, lower?: number, upper?: number): IForkedResult<T, boolean>
  • Determines whether the number of items in the iterable is at least lower and at most upper. This method only consumes as many elements from the iterable as necessary and may be used with infinite iterables. Using size(iterable) === 0 would force an endless iteration of all its item.

    isSizeBetween[1,2,3]).result        // => true
    isSizeBetween[1,2,3], 1, 3).result  // => true
    isSizeBetween[1,2,3], 3, 3).result  // => true
    isSizeBetween[1,2,3], 3, 4).result  // => true
    isSizeBetween[1,2,3], -4, 2).result // => false
    isSizeBetween[1,2,3], 4, 9).result  // => false
    isSizeBetween[1,2,3], 1, 2).result  // => false
    isSizeBetween[1,2,3], 2, Infinity).result // => true
    
    isSizeBetween[1,2,3], 0).result // => true
    isSizeBetween[1,2,3], 1).result // => true
    isSizeBetween[1,2,3], 3).result // => true
    isSizeBetween[1,2,3], 4).result // => false
    
    isSizeBetween[], 0, 0).result // => true
    isSizeBetween[], 1, 3).result // => false
    
    isSizeBetween[]).result      // => true
    isSizeBetween[1,2,3]).result // => true
    
    isSizeBetween[], NaN).result           // => false
    isSizeBetween[1,2,3], NaN).result      // => false
    isSizeBetween[], NaN, NaN).result      // => false
    isSizeBetween[1,2,3], NaN, NaN).result // => false
    
    see

    isEmpty

    infinite_iterable

    Does not hang when used on infinite (unlimited) iterables; unless argument lower is set to Infinity.

    Type parameters

    • T

      Type of the elements in the given iterable.

    Parameters

    • iterable: Iterable<T>

      The iterable to be counted.

    • Default value lower: number = 0

      Minimum number of items allowed. Defaults to 0.

    • Default value upper: number = Infinity

      Maximum number of items allowed. Defaults to Infinity.

    Returns IForkedResult<T, boolean>

    True iff the iterable contains the specified number of items or false otherwise; and an iterable over the original items.

isTry

  • isTry<S>(result: S | ITry<S>): boolean

iterate

  • iterate<T>(seed: T, next: TypedFunction<T, T>, amount?: number): Iterable<T>
  • Creates an iterable starting with the initial seed.

    iterate(42, x => (0x19660D * x + 0x3C6EF35F) % 0x100000000)
    // Random number generator, linear congruential generator from "Numerical Recipes".
    
    iterate(2, x => 2*x, 3) // => Iterable[2,4,8]
    iterate(2, x => 2*x, 0) // => Iterable[]
    iterate(2, x => 2*x, Infinity) // => Iterable[2,4,8,16,...]
    iterate(2, x => 2*x, -Infinity) // => Iterable[]
    iterate(2, x => 2*x, NaN) // => Iterable[]
    

    Type parameters

    • T

      Type of the items of the produced iterable.

    Parameters

    • seed: T

      Initial item.

    • next: TypedFunction<T, T>

      Function that takes the current item and produces the next item in the sequence.

    • Default value amount: number = Infinity

      How many times to iterate, Infinity for an unlimited amount.

    Returns Iterable<T>

    Iterable for iterating over the provided items the given amount of times.

join

  • join<T>(iterable: Iterable<T>, delimiter?: undefined | string, prefix?: undefined | string, suffix?: undefined | string): string
  • Joins every time with the given delimiter, optionally prepending a prefix and appending a suffix to the output.

    join([1,2,3], ",", "[", "]") // => "[1,2,3]"
    

    Type parameters

    • T

      Type of the elements in the given iterable.

    Parameters

    • iterable: Iterable<T>

      The iterable to be joined.

    • Optional delimiter: undefined | string

      String inserted between the items.

    • Optional prefix: undefined | string

      String prepended to the joined string.

    • Optional suffix: undefined | string

      String appended to the joined string.

    Returns string

    A string consisting of the prefix, the items joined with the delimiter, and the suffix.

last

  • last<T>(iterable: Iterable<T>): Maybe<T>
  • Returns the last item.

    last("foo") // => "o"
    last("") // => undefined
    

    Type parameters

    • T

      Type of the elements in the given iterable.

    Parameters

    • iterable: Iterable<T>

      The iterable to be scanned.

    Returns Maybe<T>

    The item at the last position, or undefined if empty.

limit

  • limit<T>(iterable: Iterable<T>, limit?: number): Iterable<T>
  • Limits the iterable to at most the given number of elements.

     limit([1,2,3,4,5,6], NaN) // => Iterable[1,2,3,4,5,6]
     limit([1,2,3,4,5,6], 0) // => Iterable[]
     limit([1,2,3,4,5,6], 3) // => Iterable[1,2,3]
    

    Type parameters

    • T

      Type of the elements in the given iterable.

    Parameters

    • iterable: Iterable<T>

      The iterable to be limited.

    • Default value limit: number = Infinity

      The maximum number of items in the resulting iterable. Default to Infinity.

    Returns Iterable<T>

    An iterable with at most the given number of items.

make

  • make<T>(inplace: boolean, iterable: Iterable<T>): IStream<T>

map

  • map<T, S>(iterable: Iterable<T>, mapper: TypedFunction<T, S>): Iterable<S>
  • Transform each element to another element of a different type.

    map([0,1,2,3], x => 2 * x) // => Iterable[0,2,4,6]
    

    Type parameters

    • T

      Type of the elements in the given iterable.

    • S

      Type of the elements in the produced iterable.

    Parameters

    • iterable: Iterable<T>

      The iterable to be mapped.

    • mapper: TypedFunction<T, S>

      A function taking each item of the given iterable and transforms it into another item.

    Returns Iterable<S>

    An iterable over the mapped elements.

max

  • max<T>(iterable: Iterable<T>, comparator?: Comparator<T>): Maybe<T>
  • Computes the maxmimum of the items.

    max([3,2,9,4]) // => 9
    max(["foo", "bar", "I"], Comparators.byField("length")) // => "foo"
    max([]) // => undefined
    

    Type parameters

    • T

      Type of the elements in the given iterable.

    Parameters

    • iterable: Iterable<T>

      The iterable to be maximized.

    • Default value comparator: Comparator<T> = natural

      How two items are compared. Defaults to the natural order, ie. by using &lt; and &gt;.

    Returns Maybe<T>

    The largest item. If there are multiple largest items, returns the first. undefined iff the iterable is empty.

maxBy

  • maxBy<T, K>(iterable: Iterable<T>, sortKey: TypedFunction<T, K>): Maybe<T>
  • Computes the minimum of the items, as determined by the given sort key.

    maxBy(["foo","foobar", "gg"], x => x.length)
    // => "foobar"
    

    Type parameters

    • T

      Type of the elements in the given iterable.

    • K

    Parameters

    • iterable: Iterable<T>

      The iterable to be maximized.

    • sortKey: TypedFunction<T, K>

      Takes an item and produces the key by which the maximum is determined.

    Returns Maybe<T>

    The smallest item, or undefined iff there are no items.

min

  • min<T>(iterable: Iterable<T>, comparator?: Comparator<T>): Maybe<T>
  • Computes the minimum of the items.

    min([3,2,9,4]) // => 9
    min(["foo", "bar", "I"], Comparators.byField("length")) // => "I"
    min([]) // => undefined
    

    Type parameters

    • T

      Type of the elements in the given iterable.

    Parameters

    • iterable: Iterable<T>

      The iterable to be minimized.

    • Default value comparator: Comparator<T> = natural

      How two items are compared. Defaults to the natural order, ie. by using &lt; and &gt;.

    Returns Maybe<T>

    The smallest item. If there are multiple smallest items, returns the first. undefined iff the iterable is empty.

minBy

  • minBy<T, K>(iterable: Iterable<T>, sortKey: TypedFunction<T, K>): Maybe<T>
  • Computes the minimum of the items, as determined by the given sort key.

    minBy(["foo","foobar", "gg"], x => x.length)
    // => "gg"
    

    Type parameters

    • T

      Type of the elements in the given iterable.

    • K

    Parameters

    • iterable: Iterable<T>

      The iterable to be minimized.

    • sortKey: TypedFunction<T, K>

      Takes an item and produces the key by which the minimum is determined.

    Returns Maybe<T>

    The smallest item, or undefined iff there are no items.

monkeyPatch

  • monkeyPatch(inplace?: boolean, force?: boolean): void
  • Patches a few stream convenience methods to the prototype of objects:

    • String.stream() Stream<string> Creates a stream over all characters.
    • Array.stream() Stream<T> Creates a stream over all items.
    • Set.stream() Stream<T> Creates a stream over all items.
    • Map.stream() Stream<K,V> Creates a stream over all key-value pairs.
    • Object.stream() Stream<{key: string, value: T}> Creates a stream over all key-value pairs.
    • Object.keys() Stream<string> Creates a stream over all keys.
    • Object.values() Stream<T> Creates a stream over all values.
    require("elbe").monkeyPatch();
    
    "foo".stream() // => Stream["f", "o", "o"]
    
    [1,2,3].stream() // => Stream[1, 2, 3]
    
    {foo: 42, bar: 99}.keys() // => Stream["foo", "bar"]
    

    Parameters

    • Default value inplace: boolean = false

      Iff true, uses InplaceStreamFactory or TypesafeStreamFactory otherwise.

    • Default value force: boolean = false

      Iff true, always add the methods to the prototype. Otherwise, does nothing if the methods exist already.

    Returns void

none

  • none<T>(iterable: Iterable<T>, predicate: Predicate<T>): boolean
  • Determines whether no item matches the given predicate. This is equivalent to !some(iterable, predicate).

    none("fooz", "x => x < "j") // => false
    

    Type parameters

    • T

      Type of the elements in the given iterable.

    Parameters

    • iterable: Iterable<T>

      The iterable to be scanned.

    • predicate: Predicate<T>

      Test to be performed on each item.

    Returns boolean

    Whether no item matches the given predicate.

nth

  • nth<T>(iterable: Iterable<T>, n: number): Maybe<T>
  • Returns the item at the n-th position.

    nth("foo", -1) // => "f"
    
    nth("foo", 0) // => "f"
    
    nth("foo", 2) // => "o"
    
    nth("foo", 3) // => undefined
    

    Type parameters

    • T

      Type of the elements in the given iterable.

    Parameters

    • iterable: Iterable<T>

      The iterable to be scanned.

    • n: number

      The position of the item to get.

    Returns Maybe<T>

    The item at the given position, or undefined if not found.

partition

  • partition<T>(iterable: Iterable<T>, discriminator: Predicate<T>): object
  • Splits the items into two groups according to the given discriminator.

    partition([-3,2,9,-4], x => x > 0)
    // => { false: [-3,-4], true: [2,9]}
    
    partition([], x => x > 0)
    // => { false: [], true: []}
    

    Type parameters

    • T

      Type of the elements in the given iterable.

    Parameters

    • iterable: Iterable<T>

      The iterable to be partitioned.

    • discriminator: Predicate<T>

      Partitions each item into one of two groups by returning true of false.

    Returns object

    An object containing the partitioned items.

    • false: T[]
    • true: T[]

patch

  • patch<P, T, S>(force: boolean, type: object, getStream: function, wrapStream: function, name?: string): void
  • internal

    Type parameters

    • P

    • T

    • S

    Parameters

    • force: boolean
    • type: object
      • prototype: P
    • getStream: function
        • (object: T): Iterable<S>
        • Parameters

          • object: T

          Returns Iterable<S>

    • wrapStream: function
        • (iterable: Iterable<S>): IStream<S>
        • Parameters

          • iterable: Iterable<S>

          Returns IStream<S>

    • Default value name: string = "stream"

    Returns void

promise

  • promise<T, S>(iterable: Iterable<T>, promiseConverter: TypedFunction<T, Promise<S>>): Promise<Iterable<S>>
  • Converts each item to a promise and returns a promise that is resolved with an iterable of results when all of the created Promises resolve, or rejected when any Promise is rejected.

    promise([1,2,3], id => fetch(`/user/${id}`))
        .then(s => s.forEach(renderUser))
        .catch(console.error)
    // Calls the renderUser method with the Response for each user request.
    

    Type parameters

    • T

      Type of the elements in the given iterable.

    • S

      Type of the item of the promises.

    Parameters

    • iterable: Iterable<T>

      The iterable to be scanned.

    • promiseConverter: TypedFunction<T, Promise<S>>

      Takes each item and creates a promise.

    Returns Promise<Iterable<S>>

    A promise that resolves when all promises resolve; or is rejected when any promise is rejected.

random

  • random(amount?: undefined | number): Iterable<number>
  • Creates an iterable of random numbers. The generated numbers are in the interval [0,1]. When the amount of numbers to generate is not specified, an unlimited amount of numbers are generated. Use methods such as {@link Methods.limit} to limit the iterable or {@link Methods.consume} to extract only a few numbers.

    Please note that the generated numbers are not cryptographically secure random numbers and must not be used in any context dealing with security.

    random(3) // => Iterable[3 random numbers]
    
    random(-1) // => Iterable[]
    
    limit(random(Infinity), 5) // => Iterable[5 random numbers]
    
    first(random(10)) // => 1 random number
    

    Parameters

    • Optional amount: undefined | number

      How many random numbers to generate. Defaults to Infinity.

    Returns Iterable<number>

    A stream with the specified amount of random numbers.

reduce

  • reduce<T, S>(iterable: Iterable<T>, reducer: TypedBiFunction<S, T, S>, initialValue: S): S
  • Coalesces all items into one value.

    reduce([1,2,3], (sum,x) => sum + x, 10) // => 16
    

    Type parameters

    • T

      Type of the elements in the given iterable.

    • S

      Type of the reduced value.

    Parameters

    • iterable: Iterable<T>

      The iterable to be reduced.

    • reducer: TypedBiFunction<S, T, S>

      Takes the current reduced value as its first argument and the current item as its second, combines the item with the current reduced value, and returns that value.

    • initialValue: S

      The initial value of the reduction.

    Returns S

    The reduced value, or the initial value iff the iterable is empty.

reduceSame

  • reduceSame<T>(iterable: Iterable<T>, reducer: TypedBiFunction<T, T, T>): Maybe<T>
  • Similar to reduce, but reduces to items of the same type as the items and thus does not require an initial value.

    reduceSame([1,2,3], (sum,x) => sum + x) // => 6
    

    Type parameters

    • T

      Type of the elements in the given iterable.

    Parameters

    • iterable: Iterable<T>

      The iterable to be reduced.

    • reducer: TypedBiFunction<T, T, T>

      Takes the current reduced value as its first argument and the current item as its second, combines the item with the current reduced value, and returns that value.

    Returns Maybe<T>

    The reduced value, or undefined iff the iterable is empty.

repeat

  • repeat<T>(item: T, amount?: number): Iterable<T>
  • Creates an iterable with the given item occuring the given number of times.

    repeat(0, 9)
    // => Iterable[0,0,0,0,0,0,0,0,0]
    
    repeat(0, 0) // => Iterable[]
    repeat(0, -Infinity) // => Iterable[]
    repeat(0, Infinity) // => Iterable[0,0,0,...]
    repeat(0, NaN) // => Iterable[]
    

    Type parameters

    • T

      Type of the items of the produced iterable.

    Parameters

    • item: T

      Item to repeat.

    • Default value amount: number = Infinity

      How many times to repeat, Infinity for an unlimited amount.

    Returns Iterable<T>

    Iterable contains the given item the given number of times.

reverse

  • reverse<T>(iterable: Iterable<T>): Iterable<T>
  • Reverses the order of the items.

    Note that the items need to be saved temporarily, so that this does not work with unlimite streams, as the last item needs to be accesed first.

    This method might create a new array, but if it does, calling toArray with the returned iterable will return that array instead of allocating a new array.

    reverse([1,2,3]) // => Iterable[3,2,1]
    reverse([]) // => Iterable[]
    reverse(factory.step(Infinity)) // hangs
    

    Type parameters

    • T

      Type of the elements in the given iterable.

    Parameters

    • iterable: Iterable<T>

      The iterable to be reversed.

    Returns Iterable<T>

    An iterable with the items in reversed order.

size

  • size(iterable: Iterable<any>): number
  • Counts the items.

    count([1,2,3]) // => 3
    
    typeparam

    Type of the elements in the given iterable.

    Parameters

    • iterable: Iterable<any>

      The iterable to be counted.

    Returns number

    The number of items in the iterable.

skip

  • skip<T>(iterable: Iterable<T>, toSkip?: number): Iterable<T>
  • Discards the given number of items from the iterable.

    skip([1,2,3,4,5,6], Infinity) // => Iterable[]
    skip([1,2,3,4,5,6], NaN) // => Iterable[1,2,3,4,5,6]
    skip([1,2,3,4,5,6], 0) // => Iterable[1,2,3,4,5,6]
    skip([1,2,3,4,5,6], 3) // => Iterable[4,5,6]
    
    see

    limit

    Type parameters

    • T

      Type of the elements in the given iterable.

    Parameters

    • iterable: Iterable<T>

      The iterable to be skipped.

    • Default value toSkip: number = Infinity

      Number of items to skip. Default to Infinity.

    Returns Iterable<T>

    An iterable with the given number of items skipped.

slice

  • slice<T>(iterable: Iterable<T>, startOffset?: number, endOffset?: number): IForkedResult<T, T[]>
  • Returns the items from the startOffset up to, but not including, the endOffset. The returned iterable is left unchanged, all items are still available for further consumption.

    const iterable = slice("foobar", 0, 3);
    // => ["f", "o", "o"]
    join(iterable) // => "foobar"
    
    const iterabl2 = slice("foobar", 2, 5);
    // => ["o", "b", "a"]
    join(s) // => "forbar"
    
    // special cases
    slice("foo", 0, 0)        // => []
    slice("foo", 3, 2)        // => []
    slice("foo", 0, 2.5)      // => ["f", "o"]
    slice("foo", 0.5, 2)      // => ["f", "o"]
    slice("foo", -5, 2)       // => ["f", "o"]
    slice("foo", 0, Infinity) // => ["f", "o", "o"]
    slice("foo", Infinity, 9) // => []
    slice("foo", 0, NaN)      // => []
    slice("foo", NaN, 2)      // => []
    slice("foo", NaN, NaN)    // => []
    slice(generate(Math.random), Infinity, Infinity) // => []
    
    infinite_iterable

    Does not hang when used on infinite (unlimited) iterables; unless argument startOffset or endOffset is set to Infinity.

    Type parameters

    • T

      Type of the elements in the given iterable.

    Parameters

    • iterable: Iterable<T>

      The iterable to be counted.

    • Default value startOffset: number = 0

      Position (inclusive) at which to start removing items from the iterable. Default to 0.

    • Default value endOffset: number = Infinity

      Position (not inclusive) at which to stop removing items from the iterable. Defaults to Infinity.

    Returns IForkedResult<T, T[]>

    The items in the iterable from startOffset up to, but not including, endOffset.

some

  • some<T>(iterable: Iterable<T>, predicate: Predicate<T>): boolean
  • Determines whether at least one items matches the given predicate.

    some("fooz", x => x < "j") // => true
    

    Type parameters

    • T

      Type of the elements in the given iterable.

    Parameters

    • iterable: Iterable<T>

      The iterable to be scanned.

    • predicate: Predicate<T>

      Test to be performed on the items.

    Returns boolean

    Whether some (at least one) item matches the given predicate.

sort

  • sort<T>(iterable: Iterable<T>, comparator?: Comparator<T>): Iterable<T>
  • Sorts the items. Consider converting the iterable to an array and sorting this array if you do not need an iterable for further operations.

    This method might create a new array, but if it does, calling toArray with the returned iterable will return that array instead of allocating a new array.

    sort(["foobar", "a", "bar", Comparators.byField("length")] // => Iterable["a", "bar", "foobar"]
    

    Type parameters

    • T

      Type of the elements in the given iterable.

    Parameters

    • iterable: Iterable<T>

      The iterable with to be sorted.

    • Optional comparator: Comparator<T>

      How to sort the items. Default to the natural order.

    Returns Iterable<T>

    An iterable with the items in sorted order.

sortBy

  • sortBy<T, K>(iterable: Iterable<T>, keyExtractor: TypedFunction<T, K>, comparator?: Comparator<K>): Iterable<T>
  • Similar to {@link Methods}#sort, but sorts items by comparing them according to the given key extractor and comparator. For each item, a key is extracted, two items are then compared by comparing theirs keys with the given comparator.

    This method might create a new array, but if it does, calling toArray with the returned iterable will return that array instead of allocating a new array.

    sortBy(["foo", "foobar", "bar"], x => x.length)
    // => Iterable["foo", "bar", "foobar"]
    
    const user1 = {name: "Dave", birth: {day: 5, month: 4, year: 1990}}
    const user2 = {name: "Maria", birth: {day: 9, month: 11, year: 2005}}
    const user3 = {name: "Odo", birth: {day: 22, month: 7, year: 2004}}
    
    sortBy([user1, user2, user3], user => user.birth, (lhs,rhs) => lhs.year - rhs.year)
    // => Iterable[user1, user3, user1]
    

    Type parameters

    • T

      Type of the elements in the given iterable.

    • K

    Parameters

    • iterable: Iterable<T>

      The iterable to be counted.

    • keyExtractor: TypedFunction<T, K>

      Extracts the key by which the sort order is determined. Default to identity x => x.

    • Optional comparator: Comparator<K>

      Comparator for comparing two keys. Defaults to the natural comparator using < and >.

    Returns Iterable<T>

    An iterable with the items in sorted order.

splice

  • splice<T>(iterable: Iterable<T>, offset?: undefined | number, maxAmount?: undefined | number): IForkedResult<T, T[]>
  • Extracts and return at most maxAmount items from the iterable, starting at the given start position. All items up to the starting point and the remaining items are left in the returned iterable and can still be iterated over.

    const result = splice("foobar", 0, 3);
    // => {iterable: {}, result: ["f", "o", "o"]}
    join(result.iterable) // => "bar"
    
    const result2 = splice("foobar", 2,3);
    // => {iterable: {}, result: ["o", "b", "a"]}
    join(result2.iterable) // => "for"
    
    // special cases
    splice("foo", 0, 0).result        // => []
    splice("foo", 0, Infinity).result // => ["f", "o", "o"]
    splice("foo", Infinity, 9).result // => []
    splice("foo", 0, NaN).result      // => []
    splice("foo", NaN, 2).result      // => []
    splice("foo", NaN, NaN)           // => []
    splice(generate(Math.random), Infinity, 0) // => []
    
    infinite_iterable

    Does not hang when used on infinite (unlimited) iterables; unless argument offset or maxAmount is set to Infinity.

    Type parameters

    • T

      Type of the elements in the given iterable.

    Parameters

    • iterable: Iterable<T>

      The iterable to be counted.

    • Optional offset: undefined | number

      Position at which to start removing items from the stream. Default to 0.

    • Optional maxAmount: undefined | number

      Maximum number if items to read from this stream. Defaults to Infinity.

    Returns IForkedResult<T, T[]>

    At most maxAmount items from the given start position of this stream.

step

  • step(amount: number, start?: number, step?: number): Iterable<number>
  • Creates an iterable with numbers starting at the given value and separated from each other by the given step.

    step(3) // => Iterable(0,1,2)
    step(3, 4) // => Iterable(4, 5, 6)
    step(3, 4, 8) // => Iterable(4, 12, 20)
    step(-3) // => Iterable()
    step(3, -4) // => Iterable(-4, -3, -2)
    step(3, 4, -2) // => Iterable(4, 2, 0)
    step(3, -4, -2) // => Iterable(-4, -6, -8)
    step(Infinity) // => Iterable(0, 1, 2, 3, ...)
    step(-Infinity) // => Iterable()
    step(Infinity, 5) // => Iterable(5, 6, 7, 8, ...)
    step(Infinity, 5, 2) // => Iterable(5, 7, 9, 11, ...)
    step(3, Infinity) // => Iterable(Infinity, Infinity, Infinity)
    step(4, 0, Infinity) // => Iterable(0, Infinity, Infinity, Infinity)
    step(4, Infinity, Infinity) // => Iterable(Infinity, Infinity, Infinity, Infinity)
    step(4, -Infinity, Infinity) // => Iterable(-Infinity, NaN, NaN, NaN)
    step(Infinity, 2, Infinity) // => Iterable(2, Infinity, Infinity, Infinity, ...)
    step(Infinity, Infinity, 2) // => Iterable(Infinity, Infinity, Infinity, Infinity, ...)
    step(Infinity, -Infinity, 2) // => Iterable(-Infinity, -Infinity, -Infinity, -Infinity, ...)
    step(Infinity, Infinity, Infinity) // => Iterable(Infinity, Infinity, Infinity, Infinity, ...)
    step(NaN) // => Iterable()
    step(5, NaN) // => Iterable(NaN, NaN, NaN, NaN, NaN)
    step(5, 1, NaN) // => Iterable(1, NaN, NaN, NaN, NaN)
    step(5, Infinity, NaN) // => Iterable(Infinity, NaN, NaN, NaN, NaN)
    step(5, -Infinity, NaN) // => Iterable(-Infinity, NaN, NaN, NaN, NaN)
    step(5, NaN, 2) // => Iterable(NaN, NaN, NaN, NaN, NaN)
    step(5, NaN, NaN) // => Iterable(NaN, NaN, NaN, NaN, NaN)
    step(NaN, NaN, NaN) // => Iterable()
    
    typeparam

    Type of the items of the produced iterable.

    Parameters

    • amount: number

      Number of items to produce. Must not be negative.

    • Default value start: number = 0

      Initial number, defaults to 0.

    • Default value step: number = 1

      How far apart the individual items are, defaults to 1.

    Returns Iterable<number>

    Iterable with the configured numbers.

sum

  • sum<T>(iterable: Iterable<T>, converter?: TypedFunction<T, number>): number
  • Sums all items arithmetically.

    sum([1,2,3]) // => 6
    sum(["I", "of", "Borg"], x => x.length) // => 7
    

    Type parameters

    • T

      Type of the elements in the given iterable.

    Parameters

    • iterable: Iterable<T>

      The iterable to be summed.

    • Optional converter: TypedFunction<T, number>

    Returns number

    The sum of the items.

takeFirst

  • takeFirst<T>(arg1: T, arg2: T): T
  • Type parameters

    • T

    Parameters

    • arg1: T
    • arg2: T

    Returns T

times

  • times(amount: number, start?: number, end?: number): Iterable<number>
  • Creates an iterable with numbers starting at the given value and ending at the given value.

    times(3) // => Iterable(0,1,2)
    times(3, 4) // => Iterable(4,5,6)
    times(3, 4, 8) // => Iterable(4,6,8)
    times(1, 10, 12) // => Iterable(10)
    times(0) // => Iterable()
    times(3, 0, -2) // => Iterable(0, -1, -2)
    times(-3) // => Iterable()
    times(5, 0, Infinity) // => Iterable(0, NaN, NaN, NaN, Infinity)
    times(5, Infinity, 0) // => Iterable(Infinity, NaN, NaN, NaN, 0)
    times(Infinity) // => Iterable(0, 0, 0, 0, ...)
    times(Infinity, 2, 3) // => Iterable(2, 2, 2, 2, ...)
    times(NaN) // => Iterable()
    times(5, NaN) // => Iterable(NaN, NaN, NaN, NaN, NaN)
    times(5, 2, NaN) // => Iterable(2, NaN, NaN, NaN, NaN)
    times(5, NaN, 7) // => Iterable(NaN, NaN, NaN, NaN, 7)
    times(NaN, 0, 5) // => Iterable()
    times(NaN, NaN, NaN) // => Iterable()
    
    
    typeparam

    Type of the items of the produced iterable.

    Parameters

    • amount: number

      Number of items to produce. Must not be negative.

    • Default value start: number = 0

      Initial number, defaults to 0.

    • Default value end: number = start + amount - 1

      Last number, defaults to start+amount-1. May be smaller than end, in which case numbers of decreasing value are generated.

    Returns Iterable<number>

    Iterable with the configured numbers.

toArray

  • toArray<T>(iterable: Iterable<T>, fresh?: boolean): T[]
  • Creates an array with the items of the given iterable. If the underlying iterable is an array, returns that array instead of creating a new array.

    toArray("foobar")
    // => ["f", "o", "o", "b", "a", "r"]
    

    Type parameters

    • T

      Type of the elements in the given iterable.

    Parameters

    • iterable: Iterable<T>

      The iterable to to be converted.

    • Default value fresh: boolean = false

      Iff true, always creates a new array. Otherise, reuses existing array when possible.

    Returns T[]

    An array with the items.

toMap

  • toMap<T, K, V>(iterable: Iterable<T>, keyMapper: TypedFunction<any, K>, valueMapper: TypedFunction<any, V>, merger?: BinaryOperator<V>): Map<K, V>
  • Creates a map from the items of the given iterable. The item's key and value is extracted by the given key and value mapper.

    When two items have the same key, the optional merge function is called with the two items and the result of the merge function is used as the value for that key. If no merge function is given, the value of the item encountered first is taken.

    toMap(["foo", "bar"], x => x, x => x.length)
    // => Map[ "foo" => 3, "bar" => 3 ]
    

    Type parameters

    • T

      Type of the elements in the given iterable.

    • K

      Type of the map's keys.

    • V

      Type of the map's values.

    Parameters

    • iterable: Iterable<T>

      The iterable to to be converted.

    • keyMapper: TypedFunction<any, K>

      Transforms an item into the map key to be used.

    • valueMapper: TypedFunction<any, V>

      Transforms an item into the value used for the corresponding key.

    • Optional merger: BinaryOperator<V>

      A merge function called when two items map to the same key and returns the merged value. Called with two items having the same key, the first argument is the item encountered first in the stream.

    Returns Map<K, V>

    A map with all the mapped key-value-pairs of the items.

toNumber

  • toNumber<T>(x: T): number
  • Type parameters

    • T

    Parameters

    • x: T

    Returns number

toSet

  • toSet<T>(iterable: Iterable<T>, fresh?: boolean): Set<T>
  • Creates a set with the items of the given iterable. If the underlying iterable is a set, returns that set instead of creating a new set.

    toSet("foobar")
    // => Set["f", "o", "b", "a", "r"]
    

    Type parameters

    • T

      Type of the elements in the given iterable.

    Parameters

    • iterable: Iterable<T>

      The iterable to to be converted.

    • Default value fresh: boolean = false

      Iff true, always creates a new set. Otherise, reuses existing set when possible.

    Returns Set<T>

    A set with the items.

tryCompute

  • tryCompute<T, S>(iterable: Iterable<T>, operation: TypedFunction<Iterable<T>, S>): ITry<S>
  • Passes the given iterable to the the operation and returns its result, wrapped in a {@link Try} in case the operation throws an error. Usually used for performing a terminal operation with error handling on the iterable.

    tryCompute([1,2,3], it => reduce(it, item => {throw new Error()}, 0))
    // => Try[success=false]
    

    Type parameters

    • T

      Type of the elements in the given iterable.

    • S

      Type of the value produced by the operation.

    Parameters

    • iterable: Iterable<T>

      The iterable to to be processed.

    • operation: TypedFunction<Iterable<T>, S>

      Takes the given iterable and returns a value. If it throws an error, the resulting {@link Try} is not successful.

    Returns ITry<S>

    The result of the operation, wrapped in a {@link Try} for encapsulating thrown errors.

tryEnd

  • tryEnd<T>(iterable: Iterable<T>): ITry<void>
  • Same as {@link end}, but encapsulates any errors thrown. Applies all pending operation to the pipeline.

    tryEnd(visit([1,2,3], console.log)) // prints 1,2,3
    

    Type parameters

    • T

      Type of the elements in the given iterable.

    Parameters

    • iterable: Iterable<T>

      The iterable to to be processed.

    Returns ITry<void>

    The encapsulated error, if any was thrown.

tryMap

  • tryMap<T, S>(iterable: Iterable<T>, mapper: TypedFunction<T, S>): Iterable<ITry<S>>
  • Similar to map, but handles any error that may be thrown by the mapper by wrapping them in a {@link Try}. This is equivalen to map(x => Try.of(() => mapper(x)))

    doTry(["[1]","[2,3]","[4,]"], JSON.parse)
    // => Iterable[ Try[1], Try[2,3], Try[ParseError] ]
    

    Type parameters

    • T

      Type of the elements in the given iterable.

    • S

      Type of the elements wrapped in a {@link Try} in the produced iterable.

    Parameters

    • iterable: Iterable<T>

      The iterable to to be processed.

    • mapper: TypedFunction<T, S>

      Mapping function that takes an item of the given iterable and produces a mapped item. If it throws an error, the resulting {@link Try} is not successful.

    Returns Iterable<ITry<S>>

    An iterable over the mapped elements, wrapped in a {@link Try} for encapsulating thrown errors.

unique

  • unique<T>(iterable: Iterable<T>, comparator?: Comparator<T>): Iterable<T>
  • Filters all elements that are considered equal according to the given comparator. If two items are equal, the first one is included in the resulting iterable. Consider using uniqueBy if possible for better performance.

    unique([4,1,3,4,1,3,1,9])
    // => Iterable[4,1,3,9]
    
    unique({id:4},{id:2},{id:4]}, (x,y) => x.id - y.id)
    // => Iterable[ {id:4}, {id:2} ]
    

    Type parameters

    • T

      Type of the elements in the given iterable.

    Parameters

    • iterable: Iterable<T>

      The iterable to be made unique

    • Optional comparator: Comparator<T>

      Takes two items and returns 0 if they are equal. Defaults to taking determining equality by ===.

    Returns Iterable<T>

    An iterable with all duplicates removed.

uniqueBy

  • uniqueBy<T, K>(iterable: Iterable<T>, keyExtractor: TypedFunction<T, K>): Iterable<T>
  • Similar to unique, but allows for a custom key to be specified, according to which uniqueness is determined.

    distinct([4,1,3,4,1,3,1,9])
    // => Iterable[4,1,3,9]
    
    distinct({id:4},{id:2},{id:4]}, customer => customer.id)
    // => Iterable[ {id:4}, {id:2} ]
    

    Type parameters

    • T

      Type of the elements in the given iterable.

    • K

    Parameters

    • iterable: Iterable<T>

      The iterable to be made unique

    • keyExtractor: TypedFunction<T, K>

      Returns a key for each item. Items with duplicate keys are removed. Defaults to taking the item itself as the key.

    Returns Iterable<T>

    An iterable with all duplicates removed.

visit

  • visit<T>(iterable: Iterable<T>, consumer: Consumer<T>): IterableIterator<T>
  • Calls the given consumer once for each item. The consumer is not called before the stream is consumed by some terminal operation.

    visit([1,2,3], console.log)
    // => Iterable[1,2,3]
    // prints `1,2,3` once the stream is consumed.
    

    Type parameters

    • T

      Type of the elements in the given iterable.

    Parameters

    • iterable: Iterable<T>

      The iterable to be limited.

    • consumer: Consumer<T>

      Callback taking each item.

    Returns IterableIterator<T>

    An iterable with the same items as the given iterable.

wrapIterator

  • wrapIterator<T>(iterator: Iterator<T>): Iterable<T>
  • Type parameters

    • T

    Parameters

    • iterator: Iterator<T>

    Returns Iterable<T>

zip

  • zip<T, S>(iterable: Iterable<T>, other: Iterable<S>): Iterable<[Maybe<T>, Maybe<S>]>
  • Produces an iterable over pairs of consisting of an item of the given iterable and the corresponding item of the other iterable. If one of the given iterable is longer, the shorter one is appended with undefined.

    zip(["foo", "bar"], [3, 4])
    // => Iterable[ ["foo",3], ["bar", 4] ]
    
    zip(["foo", "bar"], [3])
    // => Iterable[ ["foo",3], ["bar", undefined] ]
    
    toMap(zip(["foo", "bar"], [3, 4]), x=>x[0], x=>x[1])
    // => Map[ "foo" => 3, "bar" => 4] ]
    

    Type parameters

    • T

      Type of the elements in the given iterable.

    • S

    Parameters

    • iterable: Iterable<T>

      The iterable to be zipped with the other.

    • other: Iterable<S>

      Other iterable to be zipped to the given iterable.

    Returns Iterable<[Maybe<T>, Maybe<S>]>

    An iterable over all the produced pairs.

zipSame

  • zipSame<T>(iterable: Iterable<T>, others: Iterable<T>[]): Iterable<Maybe<T>[]>
  • Produces an iterable over tuples of consisting of an item of the given iterable and the corresponding items of the other iterables. If any of the iterables has a different length than the others, the shorter ones appended with undefined to the length of the longest.

    zipSame("ab", ["cd", "ef"])
    // => Iterable[ ["a","c", "d"], ["b", "d", "f"] ]
    
    zipSame("abc", ["d", "ef"])
    // => Iterable[ ["a","d", "e"], ["b", undefined, "f"], ["c", undefined, undefined] ]
    
    toArray(zipSame("fb",["oa","or"])).map(x=>x.map(String).join("")).join("")
    // => "foobar"
    

    Type parameters

    • T

      Type of the elements in the given iterable.

    Parameters

    • iterable: Iterable<T>

      The iterables to be zipped with the others.

    • others: Iterable<T>[]

      Other iterable to be zipped to the given iterable.

    Returns Iterable<Maybe<T>[]>

    An iterable over al the produced tuples.

Object literals

Const Collectors

Collectors: object

This implements the factory methods as defined by the interface Collectors.

see

Collectors

average

  • average<T>(converter?: TypedFunction<T, number>): Collector<T, object, number>
  • Type parameters

    • T

    Parameters

    • Default value converter: TypedFunction<T, number> = toNumber

    Returns Collector<T, object, number>

averageGeometrically

  • averageGeometrically<T>(converter?: TypedFunction<T, number>): Collector<T, object, number>
  • Type parameters

    • T

    Parameters

    • Default value converter: TypedFunction<T, number> = toNumber

    Returns Collector<T, object, number>

averageHarmonically

  • averageHarmonically<T>(converter?: TypedFunction<T, number>): Collector<T, object, number>
  • Type parameters

    • T

    Parameters

    • Default value converter: TypedFunction<T, number> = toNumber

    Returns Collector<T, object, number>

count

  • count(): Collector<any, object, number>

group

  • group<T, K>(classifier: TypedFunction<T, K>): Collector<T, Map<K, T[]>, Map<K, T[]>>
  • Type parameters

    • T

    • K

    Parameters

    • classifier: TypedFunction<T, K>

    Returns Collector<T, Map<K, T[]>, Map<K, T[]>>

groupDown

  • groupDown<T, K, A, D>(classifier: TypedFunction<T, K>, downstream: Collector<T, A, D>): Collector<T, Map<K, A>, Map<K, D>>
  • Type parameters

    • T

    • K

    • A

    • D

    Parameters

    • classifier: TypedFunction<T, K>
    • downstream: Collector<T, A, D>

    Returns Collector<T, Map<K, A>, Map<K, D>>

join

  • join<T>(delimiter?: string, prefix?: undefined | string, suffix?: undefined | string): Collector<T, string[], string>
  • Type parameters

    • T

    Parameters

    • Default value delimiter: string = ""
    • Optional prefix: undefined | string
    • Optional suffix: undefined | string

    Returns Collector<T, string[], string>

map

  • map<T, U, A, R>(mapper: TypedFunction<T, U>, downstream: Collector<U, A, R>): Collector<T, A, R>
  • Type parameters

    • T

    • U

    • A

    • R

    Parameters

    • mapper: TypedFunction<T, U>
    • downstream: Collector<U, A, R>

    Returns Collector<T, A, R>

multiply

  • multiply<T>(converter?: TypedFunction<T, number>): Collector<T, object, number>
  • Type parameters

    • T

    Parameters

    • Default value converter: TypedFunction<T, number> = toNumber

    Returns Collector<T, object, number>

partition

  • partition<T>(predicate: Predicate<T>): Collector<T, object, object>
  • Type parameters

    • T

    Parameters

    • predicate: Predicate<T>

    Returns Collector<T, object, object>

partitionDown

  • partitionDown<T, A, D>(predicate: Predicate<T>, downstream: Collector<T, A, D>): Collector<T, object, object>
  • Type parameters

    • T

    • A

    • D

    Parameters

    • predicate: Predicate<T>
    • downstream: Collector<T, A, D>

    Returns Collector<T, object, object>

sum

  • sum<T>(converter?: TypedFunction<T, number>): Collector<T, object, number>
  • Type parameters

    • T

    Parameters

    • Default value converter: TypedFunction<T, number> = toNumber

    Returns Collector<T, object, number>

summarize

  • summarize<T>(converter?: TypedFunction<T, number>): Collector<T, any, IStatistics>
  • Type parameters

    • T

    Parameters

    • Default value converter: TypedFunction<T, number> = toNumber

    Returns Collector<T, any, IStatistics>

toArray

  • toArray<T>(): Collector<T, T[], T[]>

toMap

  • toMap<T, K, V>(keyMapper: TypedFunction<T, K>, valueMapper: TypedFunction<T, V>, merger?: BinaryOperator<V>): Collector<T, Map<K, V>, Map<K, V>>
  • Type parameters

    • T

    • K

    • V

    Parameters

    • keyMapper: TypedFunction<T, K>
    • valueMapper: TypedFunction<T, V>
    • Default value merger: BinaryOperator<V> = takeFirst

    Returns Collector<T, Map<K, V>, Map<K, V>>

toSet

  • toSet<T>(): Collector<T, Set<T>, Set<T>>
  • Type parameters

    • T

    Returns Collector<T, Set<T>, Set<T>>

Const DONE_RESULT

DONE_RESULT: object

done

done: true = true

value

value: undefined = undefined

Const EMPTY_ITERABLE

EMPTY_ITERABLE: object

__@iterator

  • __@iterator(): Iterator<any>

Const EMPTY_ITERATOR

EMPTY_ITERATOR: object

next

  • next(value?: any): IteratorResult<any>
  • Parameters

    • Optional value: any

    Returns IteratorResult<any>

Const TryFactory

TryFactory: object

This implements the factory methods as specified by ITryFactory.

see

ITryFactory

error

  • error<T>(error: Error | string, cause?: Error): ITry<T>
  • Type parameters

    • T

    Parameters

    • error: Error | string
    • Optional cause: Error

    Returns ITry<T>

flatOf

  • flatOf<T>(action: Supplier<ITry<T>>, cause?: Error): ITry<T>

of

  • of<T>(action: Supplier<T>, cause?: Error): ITry<T>
  • Type parameters

    • T

    Parameters

    • action: Supplier<T>
    • Optional cause: Error

    Returns ITry<T>

success

  • success<T>(value: T): ITry<T>

Generated using TypeDoc