LibLinq-1.0

API / Enumerable-Generators

Enumerable.Choice(...)

Return an Enumerable that represents an infinite sequence of randomly chosen items in the arguments.

Parameters

...
Either a list-like table or multiple arguments to choose.

Return value

An infinite sequence of randomly chosen items.

Usage

Enumerable.Choice(1, 2, 3, 4):ToString() == "[4, 2, 4, 3, 1, 4, 2, 1, 3, 1, ...]"
Enumerable.Choice({ 1, 2, 3, 4 }):ToString() == "[3, 2, 4, 3, 4, 1, 3, 2, 3, 1, ...]"


Enumerable.Cycle(...)

Return an Enumerable that will cycle over the items in the arguments.

Parameters

...
Either a list-like table or multiple arguments to choose.

Return value

An infinite sequence of cycled items.

Usage

Enumerable.Cycle(1, 2, 3, 4):ToString() == "[1, 2, 3, 4, 1, 2, 3, 4, 1, 2, ...]"
Enumerable.Cycle({ 1, 2, 3, 4 }):ToString() == "[1, 2, 3, 4, 1, 2, 3, 4, 1, 2, ...]"


Enumerable.Empty()

Return an Enumerable that contains no items

Return value

an empty Enumerable

Usage

Enumerable.Empty():ToString() == "[]"


Enumerable.From(obj)

Return an Enumerable by trying to convert an object to one through some means.
Passing in nil will result in an empty Enumerable Passing in an Enumerable will return the same Enumerable Passing in a string will return an Enumerable that iterates over the characters in the string. Passing in a table will call List.WrapTable Passing in a number or boolean will return an Enumerable with the element you passed in. Any other object will result in an error.

Parameters

obj

Return value

an Enumerable

Usage

Enumerable.From(nil):ToString() == "[]"
Enumerable.From("hey"):ToString() == '["h", "e", "y"]'
Enumerable.From(Enumerable.Empty()):ToString() == "[]"
Enumerable.From({ 1, 2, 3 }):ToString() == "[1, 2, 3]"
Enumerable.From(5):ToString() == "[5]"
Enumerable.From(true):ToString() == "[true]"


Enumerable.FromIterator(func, t, start)

Return an Enumerable by converting a Lua-style iterator to an Enumerable.

Parameters

func
The iteration function
t
the state object of the iterator
start
the starting object of the iterator

Return value

an Enumerable

Usage

Enumerable.FromIterator(pairs({ hey = true, there = true})):ToString() == '["hey", "there"]'


Enumerable.Generate(generator, count)

Return an Enumerable which yields the results of repeatedly calling the provided generator

Parameters

generator
a function that will be called repeatedly
count
the amount of elements to contain. If nil, then it will be an infinite sequence.

Return value

an Enumerable

Usage

Enumerable.Generate(function() return math.random(5) end):ToString() == "[1, 5, 3, 5, 2, 4, 2, 2, 1, 2, ...]"
Enumerable.Generate(function() return math.random(5) end, 5):ToString() == "[4, 2, 4, 5, 3]"


Enumerable.Range(start, count, step)

Return an increasing range of numbers

Parameters

start
the starting number
count
the count of elements
step
the step size, defaults to 1 if nil. Must not be 0.

Return value

an Enumerable

Usage

Enumerable.Range(0, 5):ToString() == "[0, 1, 2, 3, 4]"
Enumerable.Range(5, 5):ToString() == "[5, 6, 7, 8, 9]"
Enumerable.Range(0, 5, 2):ToString() == "[0, 2, 4, 6, 8]"


Enumerable.RangeDown(start, count, step)

Return a decreasing range of numbers

Parameters

start
the starting number
count
the count of elements
step
the step size, defaults to 1 if nil. Must not be 0.

Return value

an Enumerable

Usage

Enumerable.RangeDown(0, 5):ToString() == "[0, -1, -2, -3, -4]"
Enumerable.RangeDown(5, 5):ToString() == "[5, 4, 3, 2, 1]"
Enumerable.RangeDown(10, 5, 2):ToString() == "[10, 8, 6, 4, 2]"


Enumerable.RangeTo(start, finish, step)

Return a range of numbers inclusively covers [start, finish] if possible

Parameters

start
the starting number
finish
the final number
step
the step size, defaults to 1 if nil. Must not be 0.

Return value

an Enumerable

Usage

Enumerable.RangeTo(0, 5) == "[0, 1, 2, 3, 4, 5]"
Enumerable.RangeTo(5, 0) == "[5, 4, 3, 2, 1, 0]"
Enumerable.RangeTo(0, 9, 2) == "[0, 2, 4, 6, 8]"
Enumerable.RangeTo(0, 10, 2) == "[0, 2, 4, 6, 8, 10]"


Enumerable.Repeat(obj, num)

Return an Enumerable that repeats either infinitely or a given number of times

Parameters

obj
the item to repeat
num
either nil or the amount of times to repeat

Return value

an Enumerable that repeats the object

Usage

Enumerable.Repeat(0):ToString() == "[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]"
Enumerable.Repeat(0, 5):ToString() == "[0, 0, 0, 0, 0]"


Enumerable.RepeatWithFinalize(initializer, finalizer, num)

Return an Enumerable that repeats

Parameters

initializer
a function which will return a value to be repeated
finalizer
a function that will be run on dispose, passing in the repeated value
num
the number of times to repeat, or nil to repeat infinitely

Return value

an Enumerable

Usage

Enumerable.RepeatWithFinalize(function() return 0 end):ToString() == "[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]"
Enumerable.RepeatWithFinalize(function() return 0 end, function(item) end):ToString() == "[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...]"
Enumerable.RepeatWithFinalize(function() return 0 end, nil, 5):ToString() == "[0, 0, 0, 0, 0]"


Enumerable.Return(element)

Return an Enumerable that contains a single provided element

Parameters

element
the element to contain

Return value

an Enumerable

Usage

Enumerable.Return(nil):ToString() == "[nil]"
Enumerable.Return(5):ToString() == "[5]"
Enumerable.Return("hey"):ToString() == '["hey"]'


Enumerable.SetTableWrapper(func)

Set the standard table wrapper function that Enumerable.From will use

Parameters

func
the function to call that will have a table passed in.

Usage

Enumerable.SetTableWrapper(List.WrapTable)


Enumerable.ToInfinity(start, step)

Return an Enumerable which represents infinite sequence which yields numbers starting at the given start and increasing by the step.
If a negative step is specified, it will merely be made positive.

Parameters

start
The starting number, defaulting to 0 if not specified.
step
The step size, defaulting to 1 if not specified. Must not be 0.

Return value

an Enumerable

Usage

Enumerable.ToInfinity():ToString() == "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ...]"
Enumerable.ToInfinity(1):ToString() == "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ...]"
Enumerable.ToInfinity(0, 2):ToString() == "[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, ...]"
Enumerable.ToInfinity(0, -1):ToString() == "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ...]"


Enumerable.ToNegativeInfinity(start, step)

Return an Enumerable which represents infinite sequence which yields numbers starting at the given start and decreasing by the step.
If a negative step is specified, it will merely be made positive.

Parameters

start
The starting number, defaulting to 0 if not specified.
step
The step size, defaulting to 1 if not specified. Must not be 0.

Return value

an Enumerable

Usage

Enumerable.ToNegativeInfinity():ToString() == "[0, -1, -2, -3, -4, -5, -6, -7, -8, -9, ...]"
Enumerable.ToNegativeInfinity(10):ToString() == "[10, 9, 8, 7, 6, 5, 4, 3, 2, 1, ...]"
Enumerable.ToNegativeInfinity(0, 2):ToString() == "[0, -2, -4, -6, -8, -10, -12, -14, -16, -18, ...]"
Enumerable.ToNegativeInfinity(0, -1):ToString() == "[0, -1, -2, -3, -4, -5, -6, -7, -8, -9, ...]"


Enumerable.Unfold(seed, func)

Return an Enumerable which repeatedly calls a function, passing around an aggregated value

Parameters

seed
The initial value to work on and yield
func
The function to repeatedly call, first passing in the yield, then the result after that.

Return value

an Enumerable

Usage

Enumerable.Unfold(1, function(x) return x*2 end):ToString() == "[1, 2, 4, 8, 16, 32, 64, 128, 256, 512, ...]"
Enumerable.Unfold(1, "x => x+1"):ToString() == "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ...]"


You must login to post a comment. Don't have an account? Register to get one!