Module fusion.stdlib.functional
Module for "functional" iterators and functions.
Functions
map (fn, input) | Apply a function to one or more input streams, and return the values. |
filter (fn, input) | Return values in an input stream if an applied function returns true. |
reduce (fn, input, init) | Return a value from a function applied to all values of an input stream. |
fold (start, stop, step, fn, input, init) | Does the same thing as reduce, but operates on ordered sequences. |
foldl (fn, input, init) | Fold starting from the first value of input to the last value of input. |
foldr (fn, input, init) | Fold starting from the last value of input to the first value of input. |
any (fn, input) | Return true if any returned value from an input stream are truthy, otherwise false. |
all (fn, input) | Return true if all returned values from an input stream are truthy. |
sum (input, negative) | Return a sum of all values in a stream. |
Local Functions
_pairs (input) | Iterate over a table's keys and values |
iter (input, iterator) | Return an iterator over a value if possible or the value passed. |
mk_gen (fn) | Make an iterator (or 'generator') from a function. |
truthy (val) | Return the boolean form of a value. |
Functions
- map (fn, input)
-
Apply a function to one or more input streams, and return the values.
Parameters:
- fn function
- input iter
Returns:
-
iter
Initialized iterator
Usage:
print(x in map((\v -> (^ v 2)), 1::10)); -- squares
- filter (fn, input)
-
Return values in an input stream if an applied function returns true.
Parameters:
- fn function
- input iter Iterable object
Returns:
-
iter
Initialized iterator
Usage:
print(x in filter((\v -> (== (% v 2) 1)), 1::10)); -- odds
- reduce (fn, input, init)
-
Return a value from a function applied to all values of an input stream.
Parameters:
- fn function
- input iter Iterable object
- init Initial value (will use first value of stream if not supplied)
Usage:
print(reduce((\x, y -> (?: (> x y) x y)), {5, 2, 7, 9, 1})); -- math.max()
- fold (start, stop, step, fn, input, init)
-
Does the same thing as reduce, but operates on ordered sequences.
This function should only be used on numeric tables or indexable streams.
Use
foldl()
orfoldr()
for convenience instead of this function unless you need control over the exact sequence.Parameters:
- start number
- stop number
- step number
- fn function
- input Numerically indexable object
- init Initial value (will use first value in input if not supplied)
See also:
- foldl (fn, input, init)
-
Fold starting from the first value of input to the last value of input.
Parameters:
- fn function
- input Numerically indexable object
- init Initial value (will use first value in input if not supplied)
See also:
- foldr (fn, input, init)
-
Fold starting from the last value of input to the first value of input.
Parameters:
- fn function
- input Numerically indexable object
- init Initial value (will use last value in input if not supplied)
See also:
- any (fn, input)
-
Return true if any returned value from an input stream are truthy,
otherwise false.
Parameters:
- fn function
- input iter
Returns:
-
boolean
Usage:
print(any({nil, true, false})); -- true
- all (fn, input)
-
Return true if all returned values from an input stream are truthy.
Parameters:
- fn function
- input iter
Returns:
-
boolean
Usage:
print(all(chain(1::50, {false}))); -- false
- sum (input, negative)
-
Return a sum of all values in a stream.
Parameters:
- input iter
- negative boolean optional; returns a difference instead if true
Returns:
-
number
Usage:
print(sum(1::50));
Local Functions
- _pairs (input)
-
Iterate over a table's keys and values
Parameters:
- input table
Returns:
-
iter
Initialized iterator
- iter (input, iterator)
-
Return an iterator over a value if possible or the value passed.
Possible value types can be strings and any object with __pairs or __ipairs
metadata.
Parameters:
- input table Table to iterate over (can also be iterator function)
- iterator
function
Table iterator to use (
pairs()
by default)
Returns:
-
function
- mk_gen (fn)
-
Make an iterator (or 'generator') from a function.
Parameters:
- fn function
Returns:
-
function
Wrapped coroutine
- truthy (val)
-
Return the boolean form of a value. False and nil return false, otherwise
true is returned.
Parameters:
- val Value to convert to boolean value (optionally nil)
Returns:
-
boolean