API/Enumerable-Projection

Enumerable.prototype:CascadeBreadthFirst(func, result_selector)

Project each element of the sequence and flattens the resultant sequences using breadth-first search

Parameters

func
a function that takes an element and selects the child sequence.
result_selector
optional: a function which takes an element and the nest level and whose result will be yielded.

Return value

an Enumerable

Usage

Enumerable.From({ 1, 2, 3 }):CascadeBreadthFirst(function(x) return x < 4 and Enumerable.Return(x + 1) or Enumerable.Empty() end):ToString() == "[1, 2, 3, 2, 3, 4, 3, 4, 4]"


Enumerable.prototype:CascadeDepthFirst(func, result_selector)

Project each element of the sequence and flattens the resultant sequences using depth-first search

Parameters

func
a function that takes an element and selects the child sequence.
result_selector
optional: a function which takes an element and the nest level and whose result will be yielded.

Return value

an Enumerable

Usage

Enumerable.From({ 1, 2, 3 }):CascadeDepthFirst(function(x) return x < 4 and Enumerable.Return(x + 1) or Enumerable.Empty() end):ToString() == "[1, 2, 3, 2, 3, 4, 3, 4, 4]"


Enumerable.prototype:Flatten()

Flatten sequences into one sequence

Return value

an Enumerable

Usage

Enumerable.From({ 1, { 2, 3 }, { { 4, 5 }, { 6, 7 } } }):Flatten()


Enumerable.prototype:OfType(type_name)

Filter the enumerable based on the given type

Parameters

type_name
the name of the type.

Return value

an Enumerable

Usage

Enumerable.From({ 1, "hey", function() end, "there" }):OfType("string"):ToString() == '["hey", "there"]'


Enumerable.prototype:Pairwise(selector)

Projects current and next element of a sequence into a new form.

Parameters

selector
A function to call whose results will be yielded.

Return value

an Enumerable

Usage

Enumerable.From({ 1, 2, 3, 4 }):Pairwise(function(x, y) return x + y end):ToString() == "[3, 5, 7]"
Enumerable.From({ 1, 2, 3, 4 }):Pairwise("x, y => x + y"):ToString() == "[3, 5, 7]"


Enumerable.prototype:Scan(seed, func, result_selector)

Apply an accumulation function over a sequence

Parameters

seed
optional: The initial accumulation seed
func
The function to be invoked on each element
result_selector
optional: A function to transform each accumulator value

Return value

An Enumerable

Usage

Enumerable.From({ 1, 2, 3, 4 }):Scan(function(a, b) return a + b end):ToString() == "[1, 3, 6, 10]"
Enumerable.From({ 1, 2, 3, 4 }):Scan(0, function(a, b) return a + b end):ToString() == "[0, 1, 3, 6, 10]"
Enumerable.From({ 1, 2, 3, 4 }):Scan(0, function(a, b) return a + b end, function(v) return v * 2 end):ToString() == "[0, 2, 6, 12, 20]"
Enumerable.From({ 1, 2, 3, 4 }):Scan("a, b => a + b"):ToString() == "[1, 3, 6, 10]"


Enumerable.prototype:Select(selector)

Project each element of the sequence to a new element.

Parameters

selector
the transform function which takes the item and the 1-based index

Return value

an Enumerable

Usage

Enumerable.From({ 1, 2, 3 }):Select(function(x) return x*x end):ToString() == "[1, 4, 9]"
Enumerable.From({ 1, 2, 3 }):Select("x => x*x"):ToString() == "[1, 4, 9]"


Enumerable.prototype:SelectMany(collection_selector, result_selector)

Project each element of the sequence to a new sequence, flattening the resultant sequences.

Parameters

collection_selector
the transform function which takes the item and the 1-based index
result_selector
optional: a function that takes the input element and each resultant element.

Return value

an Enumerable

Usage

Enumerable.From({ 1, 2, 3 }):SelectMany(function(x) return Enumerable.Range(1, x) end):ToString() == "[1, 1, 2, 1, 2, 3]"
Enumerable.From({ 1, 2, 3 }):SelectMany(function(x) return Enumerable.Range(1, x) end, function(a, b) return a * b end):ToString() == "[1, 2, 4, 3, 6, 9]"


Enumerable.prototype:Where(predicate)

Filter the Enumerable based on the predicate provided

Parameters

predicate
a function to test each source element that takes the element and the 1-based index and returna a boolean.

Return value

An enumerable

Usage

Enumerable.From({ 1, 2, 3, 4 }):Where(function(x) return x % 2 == 0 end):ToString() == "[2, 4]"
Enumerable.From({ 1, 2, 3, 4 }):Where("x => x%2 == 0"):ToString() == "[2, 4]"


Enumerable.prototype:Zip(second, selector)

Merge two sequences using the specified selector.
The resultant Enumerable will only be as long as the shortest enumerable zipped upon.

Parameters

second
the sequence to merge onto
selector
a function which takes two elements and the 1-based index and returns an element

Return value

an Enumerable

Usage

Enumerable.From({ 1, 2, 3, 4 }):Zip({ 6, 7, 8 }, function(a, b, i) return a + b end):ToString() == "[7, 9, 11]"
Enumerable.From({ 1, 2, 3, 4 }):Zip({ 6, 7, 8 }, "a, b => a+b"):ToString() == "[7, 9, 11]"



Comments

Posts Quoted:
Reply
Clear All Quotes