Module fusion.stdlib.iterative

Module for iterators and functions that use them

Functions

count (start, step) Infinitely count upwards.
cycle (pattern) Loop through an iterable object infinitely.
rep (element, n) Repeatedly yield a value, optionally up to n times.
range (start, stop, step) Return numbers from start to stop, incrementing by step.
accumulate (input, fn) Return accumulated sums or results of binary function.
chain (...) Iterate over every iterable object passed.
compress (input, selectors) Return values from input stream based on truthy values in selectors stream.
groupby (input) Squish repeated values based on equality of repeated values.
slice (input, start, stop, step) Return a subsection of an iterable object
zip (input0, input1, default) Zip two input streams together; table input streams will have their keys discarded.
head (n, input) Return a table based off a slice of the start of a stream.
tail (n, input) Return a table based off a slice of the end of a stream.
consume (iterator, n) Run an iterator a certain amount of times.
allequal (input) Return true if all values in input stream are true, otherwise false.
quantify (input, fn) Return a number incremented for every truthy value in an input stream after a function is applied to it.

Local Functions

iter () Iterate over a value


Functions

count (start, step)
Infinitely count upwards.

Parameters:

  • start number
  • step number

Usage:

    for (n in count()) {
    	print(n)
    	if (> n 100) break;
    }
cycle (pattern)
Loop through an iterable object infinitely.

Parameters:

Usage:

    for (char in cycle("hello")) print(char);
rep (element, n)
Repeatedly yield a value, optionally up to n times.

Parameters:

  • element
  • n number Amount of times to yield (default: infinite)

Usage:

    for (fn in rep(get_function())) print(fn);
range (start, stop, step)
Return numbers from start to stop, incrementing by step.

Parameters:

  • start number Default 1
  • stop number
  • step number Default 1

Usage:

    for (n in range(0, 100, 5)) print(n);
accumulate (input, fn)
Return accumulated sums or results of binary function.

Parameters:

  • input iter
  • fn function Binary function for reductive accumulation

Usage:

    print(x in accumulate(1::5)); -- 1 3 6 10 15
chain (...)
Iterate over every iterable object passed.

Parameters:

  • ... iter Variable argument of iterable objects

Usage:

    for (char in chain("ABCD", "EFGH")) print(char); -- A B C D E F G H
compress (input, selectors)
Return values from input stream based on truthy values in selectors stream.

Parameters:

Usage:

    for (val in compress({"hello"}, {1, 1, 1, false, 1})) print(val);
     -- 'helo'
groupby (input)
Squish repeated values based on equality of repeated values. Two values are returned; the value, and an iterator over all values matched.

Parameters:

Usage:

    print(group in groupby("ABCCAAAD")); -- A B C A D
slice (input, start, stop, step)
Return a subsection of an iterable object

Parameters:

  • input iter
  • start number
  • stop number
  • step number

Usage:

    print(slice("Hello World!", 6)); -- "World!"
zip (input0, input1, default)
Zip two input streams together; table input streams will have their keys discarded.

Parameters:

  • input0 iter
  • input1 iter
  • default Any value; may be null

Usage:

    print(key, value for key, value in zip("hi", {"hello", "world"}));
     -- ("h", "hello") ("i", "world")
head (n, input)
Return a table based off a slice of the start of a stream. Does not return n values, but instead will return values after n items are consumed from the stream.

Parameters:

  • n number Position to start getting results at
  • input iter

See also:

Usage:

    print(head("Hello World!"), 5); -- "Hello"
tail (n, input)
Return a table based off a slice of the end of a stream.

Parameters:

  • n number Maximum values to take
  • input iter

See also:

Usage:

    print(tail("Hello World!"), 6); -- "World!"
consume (iterator, n)
Run an iterator a certain amount of times.

Parameters:

  • iterator iter
  • n number

Returns:

    iter

Usage:

    print(n in consume(1::10, 3)); -- 4 5 6 7 8 9 10
allequal (input)
Return true if all values in input stream are true, otherwise false.

Parameters:

Returns:

    bool

Usage:

    print(allequal(rep(1, 10))); -- true
quantify (input, fn)
Return a number incremented for every truthy value in an input stream after a function is applied to it.

Parameters:

  • input iter
  • fn function

Returns:

    number

Usage:

    print(quantify(map((\-> math.random(0, 1)), 1::10), (n)-> n == 1));

Local Functions

iter ()
Iterate over a value

See also:

generated by LDoc 1.4.6 Last updated 2017-02-01 00:01:53