Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface IStreamFactory

Factory for creating streams. One of the most common methods is IStreamFactory#stream(iterable), which wraps an iterable such as array or set in an object-oriented stream.

Hierarchy

  • IStreamFactory

Index

Methods

empty

  • Creates an empty stream with no items.

    Type parameters

    • T

      Type of the items of the produced stream.

    Returns IStream<T>

    A stream with no items.

fromObject

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

    fromObject({foo:2, bar: 3})
    // => Stream[ {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 IStream<object>

    A stream with the object's key-value-pairs.

fromObjectKeys

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

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

    Parameters

    • object: object

      The object with the keys to be iterated.

      • [s: string]: any

    Returns IStream<string>

    A stream with the object's keys.

fromObjectValues

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

    fromObjectValues({foo:2, bar: 3})
    // => Stream[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 IStream<T>

    A stream with the object's values.

generate

  • generate<T>(generator: TypedFunction<number, T>, amount?: undefined | number): IStream<T>
  • Creates a stream with the items provided by the given generator.

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

    Type parameters

    • T

      Type of the items of the produced stream.

    Parameters

    • generator: TypedFunction<number, T>

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

    • Optional amount: undefined | number

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

    Returns IStream<T>

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

iterate

  • iterate<T>(seed: T, next: TypedFunction<T, T>, amount?: undefined | number): IStream<T>
  • Creates a stream 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) // => Stream[2,4,8]
    iterate(2, x => 2*x, 0) // => Stream[]
    iterate(2, x => 2*x, Infinity) // => Stream[2,4,8,16,...]
    iterate(2, x => 2*x, -Infinity) // => Stream[]
    iterate(2, x => 2*x, NaN) // => Stream[]
    

    Type parameters

    • T

      Type of the items of the produced stream.

    Parameters

    • seed: T

      Initial item.

    • next: TypedFunction<T, T>

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

    • Optional amount: undefined | number

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

    Returns IStream<T>

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

random

  • random(amount?: undefined | number): IStream<number>
  • Creates a stream 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 IStream#limit} to limit the stream.

    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).toArray() // => [3 random numbers]
    
    random(-1).toArray() // => []
    
    random(Infinity).limit(5) // => [5 random numbers]
    
    random(10).first() // => 1 random number
    
    

    Parameters

    • Optional amount: undefined | number

      How many random numbers to generate. Defaults to Infinity.

    Returns IStream<number>

    A stream with the specified amount of random numbers.

repeat

  • repeat<T>(item: T, amount?: undefined | number): IStream<T>
  • Creates an stream with the given item occuring the given number of times.

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

    Type parameters

    • T

      Type of the items of the produced stream.

    Parameters

    • item: T

      Item to repeat.

    • Optional amount: undefined | number

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

    Returns IStream<T>

    Stream contains the given item the given number of times.

step

  • step(amount: number, start?: undefined | number, step?: undefined | number): IStream<number>
  • Creates a stream with numbers starting at the given value and separated from each other by the given step.

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

    Parameters

    • amount: number

      Number of items to produce. Must not be negative.

    • Optional start: undefined | number

      Initial number, defaults to 0.

    • Optional step: undefined | number

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

    Returns IStream<number>

    Stream with the configured numbers.

stream

  • stream<T>(iterable: Iterable<T>): IStream<T>
  • Creates a stream from the given iterable. The iterable is consumed as part of stream operations. If it is not a repeatable iterable and multiple iterations over it are required, use {@link IStream#fork}. The iterable is queried only on-demand, thus allowing for generators that produce an unlimited number of items.

    const stream1 = factory.stream("foo");
    // => Stream["f", "o", "o"]
    
    const stream2 = factory.stream("[1,2,3]");
    // => Stream[1, 2, 3]
    
    const stream3 = factory.stream(new Set([1,2,3]).values());
    // => Stream[1, 2, 3]
    
    function * generator() {
      let i = 0;
      while (true) yield ++i;
    }
    
    const stream4 = factory.stream(generator());
    // => Stream[1, 2, 3, 4, 5, 6, ...]
    

    Type parameters

    • T

      Type of the items of the produced stream.

    Parameters

    • iterable: Iterable<T>

    Returns IStream<T>

    A stream over the iterables' values.

times

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

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

    Type of the items of the produced stream.

    Parameters

    • amount: number

      Number of items to produce. Must not be negative.

    • Optional start: undefined | number

      Initial number, defaults to 0.

    • Optional end: undefined | number

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

    Returns IStream<number>

    Stream with the configured numbers.

Generated using TypeDoc