API/Enumerable-Paging

Enumerable.prototype:ElementAt(index)

Return the element at a specified index in a sequence.
This will error if the element does not exist.

Parameters

index
the 1-based index of the element

Return value

an element of the sequence

Usage

Enumerable.From({ 'a', 'b', 'c' }):ElementAt(1) == 'a'
Enumerable.From({ 'a', 'b', 'c' }):ElementAt(2) == 'b'
Enumerable.From({ 'a', 'b', 'c' }):ElementAt(3) == 'c'


Enumerable.prototype:ElementAtOrDefault(index, default)

Return the element at a specified index in a sequence, or a default if the index is out of range.

Parameters

index
the 1-based index of the element
default
the default value to return if the index is out of range.

Return value

an element of the sequence or the default value

Usage

Enumerable.From({ 'a', 'b', 'c' }):ElementAtOrDefault(1, 'x') == 'a'
Enumerable.From({ 'a', 'b', 'c' }):ElementAtOrDefault(4, 'x') == 'x'


Enumerable.prototype:First(predicate)

Return the first element in the sequence.
If the sequence contains no elements, this will error.

Parameters

predicate
optional: A filter to apply to the sequence.

Return value

the first value that satisfies the predicate

Usage

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


Enumerable.prototype:FirstOrDefault(default, predicate)

Return the first element in the sequence or a default if one can't be found.

Parameters

default
predicate
optional: A filter to apply to the sequence.

Return value

the first value that satisfies the predicate

Usage

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


Enumerable.prototype:IndexOf(item)

Returns the 1-based index of the first occurrence of a value.
This will return -1 if the value cannot be found

Parameters

item
the value to search for

Usage

Enumerable.From({ 'a', 'b', 'c' }):IndexOf('b') == 2
Enumerable.From({ 'a', 'b', 'c' }):IndexOf('d') == -1


Enumerable.prototype:Last(predicate)

Return the last element in the sequence.
If the sequence contains no elements, this will error.

Parameters

predicate
optional: A filter to apply to the sequence.

Return value

the first value that satisfies the predicate

Usage

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


Enumerable.prototype:LastIndexOf(item)

Returns the 1-based index of the last occurrence of a value.
This will return -1 if the value cannot be found

Parameters

item
the value to search for

Usage

Enumerable.From({ 'a', 'b', 'c', 'b' }):LastIndexOf('b') == 4
Enumerable.From({ 'a', 'b', 'c' }):LastIndexOf('d') == -1


Enumerable.prototype:LastOrDefault(default, predicate)

Return the last element in the sequence or a default if one can't be found.

Parameters

default
predicate
optional: A filter to apply to the sequence.

Return value

the last value that satisfies the predicate

Usage

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


Enumerable.prototype:PickRandom()

Return a randomly-chosen element from the sequence.
If the sequence contains no elements, this will error.

Return value

a random value

Usage

Enumerable.From({ 1, 2, 3 }):PickRandom() == 2


Enumerable.prototype:PickRandomOrDefault(default)

Return a randomly-chosen element from the sequence or a default if one can't be found.
If the sequence contains no elements, this will error.

Parameters

default
the default value to return if the sequence is empty

Return value

a random value

Usage

Enumerable.From({ 1, 2, 3 }):PickRandomOrDefault(0) == 2
Enumerable.From({ }):PickRandomOrDefault(0) == 0


Enumerable.prototype:Single(predicate)

Return the singular element in the sequence.
If the sequence contains no elements, this will error. If the sequence contains more than one element, this will error.

Parameters

predicate
optional: A filter to apply to the sequence.

Return value

the singular value that satisfies the predicate

Usage

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


Enumerable.prototype:SingleOrDefault(default, predicate)

Return the singular element in the sequence or a default if one can't be found.
If the sequence contains more than one element, this will error.

Parameters

default
predicate
optional: A filter to apply to the sequence.

Return value

the first value that satisfies the predicate

Usage

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


Enumerable.prototype:Skip(count)

Bypass a specified number of items and return the remaining elements

Parameters

count
the number of elements to skip over

Return value

an Enumerable

Usage

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


Enumerable.prototype:SkipWhile(predicate)

Bypass elements in a sequence as long as a predicate remains true, then return the rest of the elements

Parameters

predicate
the function to be run to check whether to skip an element

Return value

an Enumerable

Usage

Enumerable.From({ 1, 2, 3 }):SkipWhile(function(x) return x ~= 2 end):ToString() == "[2, 3]"
Enumerable.From({ 1, 2, 3 }):SkipWhile("x => x ~= 2"):ToString() == "[2, 3]"


Enumerable.prototype:Take(count)

Returns a specified number of elements in a sequence

Parameters

count
the number of elements to return

Return value

an Enumerable

Usage

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


Enumerable.prototype:TakeExceptLast(count)

Return the elements in a sequence except for the last count

Parameters

count
optional: the amount of elements to skip at the end

Return value

an Enumerable

Usage

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


Enumerable.prototype:TakeFromLast(count)

Return the last elements in a sequence

Parameters

count
optional: the amount of elements to skip at the end

Return value

an Enumerable

Usage

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


Enumerable.prototype:TakeWhile(predicate)

Return elements in a sequence as long as a predicate remains true, then stop abruptly

Parameters

predicate
the function to be run to check whether to return an element

Return value

an Enumerable

Usage

Enumerable.From({ 1, 2, 3 }):TakeWhile(function(x) return x ~= 2 end):ToString() == "[1]"
Enumerable.From({ 1, 2, 3 }):TakeWhile("x => x ~= 2"):ToString() == "[1]"



Comments

Posts Quoted:
Reply
Clear All Quotes