LibLinq-1.0

API / Enumerable-Aggregate

Enumerable.prototype:Aggregate(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

A value

Usage

Enumerable.From({ 1, 2, 3, 4 }):Aggregate(function(a, b) return a + b end) == 10
Enumerable.From({ 1, 2, 3, 4 }):Aggregate(0, function(a, b) return a + b end) == 10
Enumerable.From({ 1, 2, 3, 4 }):Aggregate(0, function(a, b) return a + b end, function(v) return v * 2 end) == 20
Enumerable.From({ 1, 2, 3, 4 }):Aggregate("a, b => a * b") == 24


Enumerable.prototype:Average(selector)

Compute the average of a sequence

Parameters

selector
optional: A transform to apply to each element

Return value

nil if there are no values to average, otherwise a number representing the average

Usage

Enumerable.Empty():Average() == nil
Enumerable.From({ 1, 2, 3 }):Average() == 2
Enumerable.From({ 1, 2, 3 }):Average(function(x) return x * 2 end) == 4
Enumerable.From({ 1, 2, 3 }):Average("x => x*2") == 4


Enumerable.prototype:Count(predicate)

Return the number of elements in the sequence

Parameters

predicate
optional: a filter to run on each element

Return value

the amount of elements that satisfy the predicate, or if that's nil, the number of elements total

Usage

Enumerable.Empty():Count() == 0
Enumerable.From({ 1, 2, 3 }):Count() == 3
Enumerable.From({ 1, 2, 3 }):Count(function(x) return x % 2 == 1 end) == 2
Enumerable.From({ 1, 2, 3 }):Count("x => x%2 == 1") == 2


Enumerable.prototype:Max(selector)

Return the maximum value in the sequence

Parameters

selector
optional: a transform to apply to the sequence

Return value

a maximum value or nil

Usage

Enumerable.From({ 1, 10, 5 }):Max() == 10
Enumerable.From({ 'alpha', 'charlie', 'bravo' }):Max() == 'charlie'
Enumerable.From({ 'alpha', 'charlie', 'bravo' }):Max(function(x) return x:len() end) == 7
Enumerable.From({ 'alpha', 'charlie', 'bravo' }):Max("x => x:len()") == 7


Enumerable.prototype:MaxBy(key_selector)

Return the maximum value in the sequence by the generated key per element

Parameters

key_selector
a transform to get the key per element

Return value

a maximum value or nil

Usage

Enumerable.From({ 'apple', 'banana', 'cake' }):MaxBy(string.len) == 'banana'
Enumerable.From({ 'apple', 'banana', 'cake' }):MaxBy("x => x:len()") == 'banana'


Enumerable.prototype:Min(selector)

Return the minimum value in the sequence

Parameters

selector
optional: a transform to apply to the sequence

Return value

a minimum value or nil

Usage

Enumerable.From({ 1, 10, 5 }):Min() == 1
Enumerable.From({ 'alpha', 'charlie', 'bravo' }):Min() == 'alpha'
Enumerable.From({ 'alpha', 'charlie', 'bravo' }):Min(function(x) return x:len() end) == 5
Enumerable.From({ 'alpha', 'charlie', 'bravo' }):Min("x => x:len()") == 5


Enumerable.prototype:MinBy(key_selector)

Return the minimum value in the sequence by the generated key per element

Parameters

key_selector
a transform to get the key per element

Return value

a minimum value or nil

Usage

Enumerable.From({ 'apple', 'banana', 'cake' }):MinBy(string.len) == 'cake'
Enumerable.From({ 'apple', 'banana', 'cake' }):MinBy("x => x:len()") == 'cake'


Enumerable.prototype:Sum(selector)

Compute the sum of a sequence

Parameters

selector
optional: A transform to apply to each element

Return value

nil if there are no values to average, otherwise a number representing the average

Usage

Enumerable.Empty():Sum() == nil
Enumerable.From({ 1, 2, 3 }):Sum() == 6
Enumerable.From({ 1, 2, 3 }):Sum(function(x) return x * x end) == 14
Enumerable.From({ 1, 2, 3 }):Sum("x => x*x") == 14


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