Module Sqib
Sqib, a sequence query facility for Lua
This project is available on Github.
Info:
- Copyright: © 2020 psmay
- Release: 0.3.1-aa-20200804a
- License: MIT
- Author: psmay
Functions
| empty () | Returns a new Sqib.Seq containing zero elements. |
| from (value) | Produces a Sqib.Seq by guessing the appropriate conversion for value. |
| from_all (...) | Produces a Sqib.Seq by converting each parameter to a Sqib.Seq and concatenating the results. |
| from_array (a[, n]) | Returns a Sqib.Seq based on an array or its first elements. |
| from_array_slice (a, start, limit) | Returns a Sqib.Seq based on the elements of a subsequence of an array between two specified indexes. |
| from_iterate (iterate[, ...]) | Returns a Sqib.Seq based on the supplied iterate function. |
| from_keys (t) | Returns a Sqib.Seq based on the keys of a table. |
| from_packed (t) | Returns a Sqib.Seq based on a packed list. |
| from_pairs (t[, result_selector]) | Returns a Sqib.Seq based on the key-value pairs of a table. |
| from_values (t) | Returns a Sqib.Seq based on the values of a table. |
| from_yielder (yielder[, ...]) | Returns a Sqib.Seq based on the supplied yielder function. |
| over (...) | Returns a Sqib.Seq over the supplied sequence of parameters. |
| range (start_value, limit_value[, step=1]) | Returns a new Sqib.Seq over the specified range. |
| times (value, count) | Returns a new Sqib.Seq that repeats a specified value a specified number of times. |
Class Sqib.Seq
| Sqib.Seq:all (predicate) | Returns whether all elements of this sequence satisfy a predicate. |
| Sqib.Seq:any ([predicate]) | Returns whether this sequence contains any elements or contains any elements that satisfy a predicate. |
| Sqib.Seq:append (...) | Produces a Sqib.Seq consisting of this sequence followed by the specified elements. |
| Sqib.Seq:batch (block_size[, result_selector]) | Produces a Sqib.Seq consisting of this sequence’s elements as blocks of a specified number of elements each. |
| Sqib.Seq:call (f[, ...]) | Calls the specified function as if it were a method on this sequence. |
| Sqib.Seq:concat (...) | Returns a Sqib.Seq consisting of this sequence followed by the specified additional sequences. |
| Sqib.Seq:copy_into_array (a[, start_index=1]) | Copies all elements of this sequence into an existing array or object. |
| Sqib.Seq:count ([predicate]) | Counts the number of elements, or the number of elements that satisfy a predicate, in this sequence. |
| Sqib.Seq:filter (predicate) | Filters this sequence to only the elements that satisfy a predicate. |
| Sqib.Seq:flat_map ([selector[, convert_result=true]]) | Maps this sequence by applying a sequence-producing selector, then returns a sequence from the concatenated results. |
| Sqib.Seq:force () | Forces immediate evaluation of this sequence. |
| Sqib.Seq:is_sqib_seq () | Returns whether this object should be treated as Sqib.Seq. |
| Sqib.Seq:iterate () | Returns a closure-based iterator over this sequence. |
| Sqib.Seq:map (selector) | Maps each element of this sequence by applying a selector. |
| Sqib.Seq:new ([o={}]) | The constructor for an abstract Sqib.Seq. |
| Sqib.Seq:pack () | Copies this sequence into a new packed list. |
| Sqib.Seq:pairs_to_hash (pair_selector) | Creates a new dictionary-style table using the provided selector to determine each key-value pair. |
| Sqib.Seq:reversed () | Returns a new Sqib.Seq that consists of the elements of this sequence in reverse order. |
| Sqib.Seq:skip (count) | Skips a specified number of elements, then produces the remainder. |
| Sqib.Seq:skip_while (predicate) | Skips all elements while a predicate is satisfied, then produces the remainder. |
| Sqib.Seq:sorted (...) | Returns a new Sqib.Seq that consists of the elements of this sequence sorted according to the specified options. |
| Sqib.Seq:take (count) | Passes a specified number of elements, then ends the sequence. |
| Sqib.Seq:take_while (predicate) | Passes all elements while a predicate is satisfied, then ends the sequence. |
| Sqib.Seq:times (count) | Returns a new Sqib.Seq that repeats this entire sequence a specified number of times. |
| Sqib.Seq:to_array ([include_length=false]) | Copies this sequence into a new array. |
| Sqib.Seq:to_hash ([key_selector[, value_selector]]) | Creates a new dictionary-style table using the provided selectors to determine the keys and values. |
| Sqib.Seq:to_sqib_seq () | Returns this Sqib.Seq object. |
| Sqib.Seq:unique ([key_selector]) | Filters this sequence to distinct elements by including only the first element having a selected key. |
| Sqib.Seq:unpack () | Returns this entire sequence as a return value list. |
Functions
- empty ()
-
Returns a new Sqib.Seq containing zero elements.
Returns:
-
A new, empty Sqib.Seq.
- from (value)
-
Produces a Sqib.Seq by guessing the appropriate conversion for
value.- If
valueis a table,- If
value.is_sqib_seqexists as a function andvalue:is_sqib_seq()returns true,valueis used directly. - If
value.to_sqib_seqexists as a function, the result ofvalue:to_sqib_seq()is used.- If the value
seqreturned byvalue:to_sqib_seq()is not a table, orseq.is_sqib_seqis not a function, orseq:is_sqib_seq()does not return true, an error is raised.
- If the value
- Otherwise, if
value.nexists as a nonnegative integer, the result of Sqib.from_packed(value) is used. - Otherwise, the result of Sqib.from_array(value) is used.
- If
- If
valueis a function, the result of Sqib.from_yielder(value) is used. - Otherwise, an error is raised.
Parameters:
- value A source of sequence data to be used as a sequence.
Returns:
-
A Sqib.Seq obtained by automatically converting
value.Raises:
- When
valuehas no automatic conversion to a sequence. - When
value.to_sqib_seqis a function butvalue:to_sqib_seq()returns a value that does not appear to be a sequence (i.e., does not pass theis_sqib_seq()test).
See also:
- If
- from_all (...)
-
Produces a Sqib.Seq by converting each parameter to a Sqib.Seq and concatenating the results.
Parameters:
- ... Sequence-like values to be converted to sequences (using the same rules as Sqib.from) and concatenated.
Returns:
-
A Sqib.Seq obtained by automatically converting every parameter to a Sqib.Seq, then concatenating the
results.
Raises:
- When any parameter has no automatic conversion to a sequence.
- When, for any parameter
v,v.to_sqib_seqis a function butv:to_sqib_seq()returns a value that does not appear to be a sequence (i.e., does not pass theis_sqib_seq()test).
See also:
- from_array (a[, n])
-
Returns a Sqib.Seq based on an array or its first elements.
Parameters:
- a An array on which to base the new sequence.
- n
The number of elements to include from
a. If omitted, the length is recomputed from#aat the beginning of each new iteration. (optional)
Returns:
-
A Sqib.Seq consisting of the first
nelements ofa, or, ifnis omitted, the first#aelements ofa.See also:
- from_array_slice (a, start, limit)
-
Returns a Sqib.Seq based on the elements of a subsequence of an array between two specified indexes.
Parameters:
- a An array on which to base the new sequence.
- start
The inclusive index of
aat which to start the sequence. Must be an integer, but need not be positive. - limit
The inclusive index of
aat which to end the sequence. Must be an integer, but need not be positive.
Returns:
-
A Sqib.Seq consisting of the elements of
afroma[start]toa[limit], inclusive, or an empty sequence ifstartis greater thanlimit.See also:
- from_iterate (iterate[, ...])
-
Returns a Sqib.Seq based on the supplied iterate function.
The function iterate is expected to return an iterator function and optionally stateless iterator parameters. iterate is used in a construction similar to
for _, v in iterate() do … endand has a similar contract to the built-inipairs()orpairs()functions.The iteration is expected to produce each successive element of the represented sequence by returning either:
- a pair
x, v, wherexis any non-nilvalue andvis the next element value, or nil, signaling the end of the sequence.
The index values produced by the iterator need not be in any particular order; the requirement is only that each index value be non-
nilif a value is being returned ornilonce the sequence is exhausted. The sequence returned from this method discards the indexes from the iterator and provides its own indexes that comply with the contract of iterate.function example_iterate(start, limit) local i = start - 1 return function() if i < limit then i = i + 1 return true, i end end end local seq = Sqib.from_iterate(example_iterate, 10, 13) -- sequence is 10, 11, 12, 13[Implementation detail: The number of parameters in
…is subject to the same limitations as unpack.]Parameters:
- iterate A function which returns an iterator function.
- ... Parameters that will be passed to iterate at the beginning of each new iteration. (optional)
Returns:
See also:
- a pair
- from_keys (t)
-
Returns a Sqib.Seq based on the keys of a table.
Parameters:
- t A table whose keys to traverse.
Returns:
-
A new Sqib.Seq representing the keys of
t, in no particular order. - from_packed (t)
-
Returns a Sqib.Seq based on a packed list.
For the purposes of this method, a packed list is a table
twhich has a fieldt.nthat is a nonnegative integer and which has elementst[1]throught[t.n](any or all of which may benil) that constitute a conceptual list. This method produces a Sqib.Seq representing this list.Parameters:
- t A packed list.
Returns:
-
A new Sqib.Seq based on the elements of the packed list
t. - from_pairs (t[, result_selector])
-
Returns a Sqib.Seq based on the key-value pairs of a table.
Parameters:
- t A table whose key-value pairs to traverse.
- result_selector
A function
(k, v)that selects the output element based on each key-value pair. If omitted, the selector produces a two-element array{k, v}containing the key and the value. (optional)
Returns:
-
A new Sqib.Seq representing the key-value pairs of
t, in no particular order. - from_values (t)
-
Returns a Sqib.Seq based on the values of a table.
Parameters:
- t A table whose values to traverse.
Returns:
-
A new Sqib.Seq representing the values of
t, in no particular order. - from_yielder (yielder[, ...])
-
Returns a Sqib.Seq based on the supplied yielder function.
The function
yielderis expected to callcoroutine.yield(v)once for each successive valuevin the iteration.yielderis called from a coroutine using the supplied parameters, if any, at the beginning of each new iteration.function example_yielder(start, limit) for i = start, limit do for j = start, limit do coroutine.yield("(" .. i .. "," .. j .. ")") end end end local seq = Sqib.from_yielder(example_yielder, 1, 2) -- seq is "(1,1)", "(1,2)", "(2,1)", "(2,2)"[Implementation detail: The number of parameters in
…is subject to the same limitations as unpack.]Parameters:
- yielder A function which returns an iterator function.
- ... Parameters that will be passed to iterate at the beginning of each new iteration. (optional)
Returns:
- over (...)
-
Returns a Sqib.Seq over the supplied sequence of parameters.
Parameters:
- ... Elements to be traversed by the new sequence.
Returns:
-
A new Sqib.Seq based on the values of
…. - range (start_value, limit_value[, step=1])
-
Returns a new Sqib.Seq over the specified range.
The sequence produced is the same sequence produced by the
forloopfor i=start_value, limit_value, step do ... endParameters:
- start_value The initial value of the range.
- limit_value The limit for the final value of the range.
- step The step value for iteration. (default 1)
Returns:
-
A new Sqib.Seq with contents based on the specified range.
- times (value, count)
-
Returns a new Sqib.Seq that repeats a specified value a specified number of times.
Parameters:
- value The element value to be repeated.
- count
The number of times to repeat
value.
Returns:
Class Sqib.Seq
Derived classes must implement Sqib.Seq:iterate. If a derived class can determine its own size in less-than-linear time, it should also override Sqib.Seq:count.
- Sqib.Seq:all (predicate)
-
Returns whether all elements of this sequence satisfy a predicate.
If this sequence is empty, returns
true.Parameters:
- predicate
A function
(v, i)that returns true if the current element satisfies a condition or false otherwise.
Returns:
trueif all elements of this sequence satisfypredicate, orfalseotherwise. - predicate
A function
- Sqib.Seq:any ([predicate])
-
Returns whether this sequence contains any elements or contains any elements that satisfy a predicate.
If this sequence is empty, returns
false.Parameters:
- predicate
A function
(v, i)that returns true if the current element satisfies a condition or false otherwise. (optional)
Returns:
-
If
predicateis supplied,trueif any element in this sequence satisfiespredicate, orfalseotherwise. Ifpredicateis omitted,trueif this sequence contains any elements, orfalseotherwise. - predicate
A function
- Sqib.Seq:append (...)
-
Produces a Sqib.Seq consisting of this sequence followed by the specified elements.
Parameters:
- ... Elements to append to this sequence.
Returns:
-
A Sqib.Seq consisting of the elements of this sequence followed by the specified additional elements.
- Sqib.Seq:batch (block_size[, result_selector])
-
Produces a Sqib.Seq consisting of this sequence’s elements as blocks of a specified number of elements each.
Each block before the final block contains exactly
block_sizeelements. The final block contains the remainder of the sequence, which is as few as one element or as many asblock_sizeelements. No block will ever contain zero elements.By default, the resulting sequence will produce each block as a packed list. If another form is more useful, specify a
result_selectorthat accepts an array and size and produces the desired element.-- Example: Produces each block as a new Sqib.Seq local seq_of_seq = seq:batch(block_size, function(a, n) return Sqib.from_array(a, n) end)Parameters:
- block_size The number of elements to include in each block. Must be a positive integer.
- result_selector
A function
(a, n)to be applied to each block before it is returned from the iterator, whereais an array containing the elements of the block andnis the number of elements in the block. If omitted, the result selector defaults to returning a packed list (i.e. by settinga.ntonand returninga). (optional)
Returns:
-
A Sqib.Seq that iterates over this sequence
block_sizeelements at a time. - Sqib.Seq:call (f[, ...])
-
Calls the specified function as if it were a method on this sequence.
This is a simple way to call custom sequence operations fluently without assigning them directly to to Sqib.Seq table.
function my_every_n(seq, n) return seq:filter(function(_,i) return i % n == 0 end) end -- Use call local seq = Sqib.over(1,2,3,4,5,6,7,8,9,10) :call(my_every_n, 3) -- seq is now the sequence 3, 6, 9Parameters:
- f A function to call as if it were a method.
- ...
Additional parameters for
f. (optional)
Returns:
-
The result of calling
f(self, …), whereselfis this sequence. - Sqib.Seq:concat (...)
-
Returns a Sqib.Seq consisting of this sequence followed by the specified additional sequences.
Parameters:
- ... Sequence-like values to be converted to sequences (using the same rules as Sqib.from) and concatenated to this sequence.
Returns:
-
A Sqib.Seq consisting of the elements of this sequence followed by the elements of each of the specified
additional sequences.
Raises:
- When any parameter has no automatic conversion to a sequence.
- When, for any parameter
v,v:to_sqib_seq()is found but returns a value that does not appear to be a sequence (i.e., does not pass theis_sqib_seq()test).
See also:
- Sqib.Seq:copy_into_array (a[, start_index=1])
-
Copies all elements of this sequence into an existing array or object.
The first element of this sequence is copied to
a[start_index], the second toa[start_index + 1], and so on. Any values already in these positions are overwritten.Parameters:
- a An array or array-like object into which elements will be copied.
- start_index
The first index of
ato use for copying elements. If provided, this must be an integer. The value need not be positive (the caveats of using non-1-based arrays in Lua are well documented elsewhere). (default 1)
Returns:
-
The number of elements copied. (The copied elements are
a[start_index]througha[start_index - 1 + <number of elements copied>].) - Sqib.Seq:count ([predicate])
-
Counts the number of elements, or the number of elements that satisfy a predicate, in this sequence.
Parameters:
- predicate
A function
(v, i)that returns true if the current element satisfies a condition or false otherwise. If omitted, all elements are counted. (optional)
Returns:
-
The total number of elements (if
predicateis omitted) or the number of elements that satisfypredicate(ifpredicateis supplied) in this sequence. - predicate
A function
- Sqib.Seq:filter (predicate)
-
Filters this sequence to only the elements that satisfy a predicate.
Parameters:
- predicate
A function
(v, i)that returns true if the current element satisfies a condition or false otherwise.
Returns:
-
A new Sqib.Seq consisting of the elements of this sequence that satisfy
predicate. - predicate
A function
- Sqib.Seq:flat_map ([selector[, convert_result=true]])
-
Maps this sequence by applying a sequence-producing selector, then returns a sequence from the concatenated results.
Parameters:
- selector
A sequence-returning function
(v, i)to apply to each element in the sequence. If omitted, the selector returns the element value. (optional) - convert_result
Indicates whether the value returned from the selector should be converted into a
Sqib.Seq using Sqib.from, which allows the operation to flatten certain other non-Sqib.Seq sequences. Set to
falseif the selector always produces a Sqib.Seq and doesn’t need the additional conversion. (default true)
Returns:
-
A new Sqib.Seq consisting of the concatenated elements of the sequences returned by
selectoras applied to each element from this sequence. - selector
A sequence-returning function
- Sqib.Seq:force ()
-
Forces immediate evaluation of this sequence.
This method iterates this sequence completely, making an immutable copy, and returns a Sqib.Seq based on the copy.
The main effects of using the returned Sqib.Seq in place of the original sequence are:
- The resulting sequence itself doesn’t need to perform any non-trivial computations. Any (potentially intensive) computation specified before the force only happens once.
- The resulting sequence will not vary between iterations, even if
- this source sequence is mutable (such as an array passed to Sqib.from, if anything other than the sequence still has a reference to it)
- this source sequence is based on computations which use variables that are themselves mutable
- The resulting sequence generally occupies more memory.
The effect of conversions such as Sqib.Seq:to_array or Sqib.Seq:pack is similar, except that those methods do not directly produce a Sqib.Seq while this method does not produce an object which can be accessed as a table.
Returns:
- Sqib.Seq:is_sqib_seq ()
-
Returns whether this object should be treated as Sqib.Seq.
A function named is_sqib_seq on any object is assumed (e.g. by Sqib.from) to return true if the object is a Sqib.Seq (or close enough to one that it can be treated as one). This method exists for the purpose of identifying an object as a Sqib.Seq in absence of any reliable way to do so in Lua.
The is_sqib_seq method of Sqib.Seq is specified to always return
true. This should not be overridden by derived types of Sqib.Seq.Returns:
true.See also:
- Sqib.Seq:iterate ()
-
Returns a closure-based iterator over this sequence.
Each time the returned iterator is called, it returns a pair of values
i, v(an index and a value, respectively) that represent each successive element from the sequence, until the sequence is exhausted, at which pointiwill benil.The index
iis defined to start at 1 and increase by 1 with each new element.Returns:
-
A closure-based (non-stateless) iterator over this sequence.
- Sqib.Seq:map (selector)
-
Maps each element of this sequence by applying a selector.
Parameters:
- selector
A function
(v, i)to apply to each element in the sequence.
Returns:
-
A new Sqib.Seq consisting of the results of applying
selectorto each element of this sequence. - selector
A function
- Sqib.Seq:new ([o={}])
-
The constructor for an abstract Sqib.Seq.
The implementation of this method does nothing beyond creating the object and setting the type’s index and the instance’s metatable.
See the functions of Sqib (for example, Sqib.from) for simple ways to create Sqib.Seq objects from actual data.
Parameters:
- o A table to convert into this type. (default {})
Returns:
o, having been converted to this type. - Sqib.Seq:pack ()
-
Copies this sequence into a new packed list.
Returns:
-
A new table containing the elements of this sequence, with a field
ncontaining the length of the list. - Sqib.Seq:pairs_to_hash (pair_selector)
-
Creates a new dictionary-style table using the provided selector to determine each key-value pair.
Parameters:
- pair_selector
Function
(v, i)that returnsk, v, the key and value for a pair.
Returns:
-
The newly created table.
Raises:
- When
pair_selectoris not provided. - When any key appears more than once.
See also:
- pair_selector
Function
- Sqib.Seq:reversed ()
-
Returns a new Sqib.Seq that consists of the elements of this sequence in reverse order.
The reverse operation itself is deferred and occurs whenever the returned Sqib.Seq is actually iterated. When an iterator is retrieved, the entire contents of the source sequence are copied into an internal table. The resulting element values are iterated in reverse order.
Returns:
-
A Sqib.Seq representing a copy of this sequence whose elements have been reversed.
- Sqib.Seq:skip (count)
-
Skips a specified number of elements, then produces the remainder.
Parameters:
- count A number of elements to skip. If this is zero or less, the result is the entire sequence. If this is the length of the sequence or greater, the result is empty.
Returns:
- Sqib.Seq:skip_while (predicate)
-
Skips all elements while a predicate is satisfied, then produces the remainder.
Parameters:
- predicate
A function
(v, i)to test elements. Elements are skipped until the first element for which this returns false; the remaining elements are passed through untested.
Returns:
-
A Sqib.Seq consisting of all elements of this sequence including and after the first element for which the
predicate returns false.
- predicate
A function
- Sqib.Seq:sorted (...)
-
Returns a new Sqib.Seq that consists of the elements of this sequence sorted according to the specified
options.
With no options specified, the call
local result = seq:sorted()is equivalent to
local result = seq:sorted({ by = function(v) return v end, compare = function(a, b) return (a < b) and -1 or (a > b) and 1 or 0 end, ascending = true, stable = false, })Otherwise, one or more orderings can be specified, each with its own full set of parameters. If two elements are tied according to the first ordering, then the second ordering is tried and so on until the tie is broken.
-- Example of sorting first by one field, then another, then another -- (stable = true in any of the orderings causes the entire sort to be stable) local result = seq:sorted( { by = function(v) return v.primary_field end, stable = true }, { by = function(v) return v.secondary_field end, ascending = false }, { by = function(v) return v.tertiary_field end, compare = some_compare_function } )If all comparisons tie for two elements and
stableis true for any of the orderings, then the element that occurred earlier in the input sequence will appear earlier in the output as well. If all comparisons tie for two elements andstableis not true for any of the orderings, the elements may appear in either order.The sort operation itself is deferred and occurs at the beginning of each new iteration. All elements of this sequence are iterated at once, copied internally, and sorted, then this sorted copy is iterated.
Parameters:
- ...
Ordering specifications to be applied in order while comparing elements; each parameter is a table that may define any of the following:
by: A function(v)that returns a value that will be the subject of the sort. If omitted, the sort will be performed on the value itself.compare: A ternary compare function(a, b)that returns a negative number ifacomes beforeb, a positive number ifacomes afterb, or 0 ifaandbare tied. As a backward compatibility measure, if this method returns a non-number value, such astrueorfalse, it will be treated as an ordinary Lua comparer (returns truthy ifacomes beforebor falsy otherwise), but ifstableis set to true or if multiple orderings are specified, then the comparer may need to be called twice (once in each order) to determine whether two elements are tied. If omitted, the comparison is equivalent to the expression(a < b) and -1 or (a > b) and 1 or 0.ascending: If true, the elements are arranged in ascending order. If false, the elements are arranged in descending order. If omitted, defaults to true.stable: If true, the sort is stable; that is, any two elements that are tied will be sorted in the same order in which they appear in the input. If false, the sort is not guaranteed to be stable. If omitted, defaults to false. If multiple orderings are specified and at least one specifiesstableas true, the entire result will be stable.
Returns:
-
A Sqib.Seq representing a copy of this sequence whose elements have been sorted as specified.
- ...
- Sqib.Seq:take (count)
-
Passes a specified number of elements, then ends the sequence.
Parameters:
- count A number of elements to take. If this is zero or less, the result is empty. If this is the length of the sequence or greater, the result is the entire sequence.
Returns:
- Sqib.Seq:take_while (predicate)
-
Passes all elements while a predicate is satisfied, then ends the sequence.
Parameters:
- predicate
A function
(v, i)to test elements. Elements are passed until the first element for which this returnsfalse; the remaining elements are ignored.
Returns:
-
A Sqib.Seq consisting of all elements of this sequence before, but not including, the first element for
which the predicate returns false.
- predicate
A function
- Sqib.Seq:times (count)
-
Returns a new Sqib.Seq that repeats this entire sequence a specified number of times.
Note that a new iteration of this sequence is started for every repetition. This may include reevaluating sequence contents, filters, mappings, orderings, and so forth each time. If this isn’t what you intended, use e.g. force to fully evaluate the sequence once before using this method.
Parameters:
- count The number of times to repeat this sequence.
Returns:
- Sqib.Seq:to_array ([include_length=false])
-
Copies this sequence into a new array.
Parameters:
- include_length If true, this method returns the new array and the number of elements; otherwise, this method returns only the array. (default false)
Returns:
a, n, ifinclude_length, ora, otherwise, whereais the new array andnis the number of elements copied. - Sqib.Seq:to_hash ([key_selector[, value_selector]])
-
Creates a new dictionary-style table using the provided selectors to determine the keys and values.
Parameters:
- key_selector
Function
(v, i)that selects the key for an item. If omitted, the value is selected. (optional) - value_selector
Function
(v, i)that selects the value for an item. If omitted, the value is selected. (optional)
Returns:
-
The newly created table.
Raises:
When any key appears more than once.See also:
- key_selector
Function
- Sqib.Seq:to_sqib_seq ()
-
Returns this Sqib.Seq object.
A method named to_sqib_seq on any object is assumed (e.g. by from) to return a Sqib sequence equivalent to the object.
The to_sqib_seq method of Sqib.Seq is specified to always return the implied
selfparameter. This should not be overridden by derived types of Sqib.Seq.Returns:
self(the object on which this method was called).See also:
- Sqib.Seq:unique ([key_selector])
-
Filters this sequence to distinct elements by including only the first element having a selected key.
This implementation is table-based; a table is used to keep track of the keys of already seen items.
Any two elements are considered distinct if their keys do not refer to the same item when used to index a table; that is, for
aandb, for some tableseen,seen[key_selector(a)]andseen[key_selector(b)]don’t refer to the same item.A special case is implemented so that
nilcan be used as a key.Parameters:
- key_selector
A function
(v, i)that selects a key by which to determine uniqueness. If this is omitted, the key selector returns the value itself as a key. (optional)
Returns:
-
A Sqib.Seq consisting of each element of this sequence for which the selected key has not already
appeared.
- key_selector
A function
- Sqib.Seq:unpack ()
-
Returns this entire sequence as a return value list.
Similarly to the built-in unpack, this method produces the values from a sequence in a form suitable for multiple assignment or for appending to an array or parameter list.
local seq = Sqib.over(10, 20, 30) local q, r, s = seq:unpack() -- like q = 10, r = 20, s = 30 local a = {seq:unpack()} -- like a = {10, 20, 30} local a2 = {0, seq:unpack()} -- like a = {0, 10, 20, 30} print("values", seq:unpack()) -- like print("values", 10, 20, 30)[Implementation detail: This implementation may cause a stack overflow condition for a very large number of elements (as determined during testing, a little over a million; may vary by target environment). The implementation of this method relies on a recursive function call that Lua 5.1 seems unable to tail-call optimize. The depth of the call stack is directly proportional to the number of elements.]
Returns:
-
All elements of this sequence in order.