Module pl.seq
Manipulating iterators as sequences.
See The Guide
Dependencies: pl.utils , debug
Functions
| matching (s) | given a string, return a function(y) which matches y against the string. |
| list (t) | sequence adaptor for a table. |
| keys (t) | return the keys of the table. |
| range (start, finish) | create an iterator over a numerical range. |
| minmax (iter) | return the minimum and the maximum value of the sequence. |
| sum (iter, fn) | return the sum and element count of the sequence. |
| copy (iter) | create a table from the sequence. |
| copy2 (iter, i1, i2) | create a table of pairs from the double-valued sequence. |
| copy_tuples (iter) | create a table of ‘tuples’ from a multi-valued sequence. |
| random (n, l, u) | return an iterator of random numbers. |
| sort (iter, comp) | return an iterator to the sorted elements of a sequence. |
| zip (iter1, iter2) | return an iterator which returns elements of two sequences. |
| count_map (iter) | A table where the key/values are the values and value counts of the sequence. |
| printall (iter, sep, nfields, fmt) | print out a sequence iter with a separator. |
| map (fn, iter, arg) | return a sequence where every element of a sequence has been transformed by a function. |
| filter (iter, pred, arg) | filter a sequence using a predicate function |
| reduce (fun, iter, oldval) | ‘reduce’ a sequence using a binary function. |
| take (iter, n) | take the first n values from the sequence. |
| skip (iter, n) | skip the first n values of a sequence |
| enum (iter) | a sequence with a sequence count and the original value. |
| mapmethod (iter, name, arg1, arg2) | map using a named method over a sequence. |
| last (iter) | a sequence of (last,current) values from another sequence. |
| foreach (iter, fn) | call the function on each element of the sequence. |
| lines (f) | create a wrapped iterator over all lines in the file. |
Functions
- matching (s)
-
given a string, return a function(y) which matches y against the string.
Parameters:
s: a string
- list (t)
-
sequence adaptor for a table. Note that if any generic function is
passed a table, it will automatically use seq.list()
Parameters:
t: a list-like table
Usage:
sum(list(t)) is the sum of all elements of t
for x in list(t) do...end
- keys (t)
-
return the keys of the table.
Parameters:
t: a list-like table
Returns:
-
iterator over keys
- range (start, finish)
-
create an iterator over a numerical range. Like the standard Python function xrange.
Parameters:
start: a numberfinish: a number greater than start
- minmax (iter)
-
return the minimum and the maximum value of the sequence.
Parameters:
iter: a sequence
Returns:
- minimum value
- maximum value
- sum (iter, fn)
-
return the sum and element count of the sequence.
Parameters:
iter: a sequencefn: an optional function to apply to the values
- copy (iter)
-
create a table from the sequence. (This will make the result a List.)
Parameters:
iter: a sequence
Usage:
copy(list(ls)) is equal to ls
copy(list {1,2,3}) == List{1,2,3}
Returns:
-
a List
- copy2 (iter, i1, i2)
-
create a table of pairs from the double-valued sequence.
Parameters:
iter: a double-valued sequencei1: used to capture extra iterator valuesi2: as with pairs & ipairs
Usage:
copy2(ipairs{10,20,30}) == {{1,10},{2,20},{3,30}}Returns:
-
a list-like table
- copy_tuples (iter)
-
create a table of ‘tuples’ from a multi-valued sequence.
A generalization of copy2 above
Parameters:
iter: a multiple-valued sequence
Returns:
-
a list-like table
- random (n, l, u)
-
return an iterator of random numbers.
Parameters:
n: the length of the sequencel: same as the first optional argument to math.randomu: same as the second optional argument to math.random
Returns:
-
a sequnce
- sort (iter, comp)
-
return an iterator to the sorted elements of a sequence.
Parameters:
iter: a sequencecomp: an optional comparison function (comp(x,y) is true if x < y)
- zip (iter1, iter2)
-
return an iterator which returns elements of two sequences.
Parameters:
iter1: a sequenceiter2: a sequence
Usage:
for x,y in seq.zip(ls1,ls2) do....end
- count_map (iter)
-
A table where the key/values are the values and value counts of the sequence.
This version works with ‘hashable’ values like strings and numbers.
pl.tablex.count_map is more general.Parameters:
iter: a sequence
Returns:
- a map-like table
- a table
see also:
- printall (iter, sep, nfields, fmt)
-
print out a sequence iter with a separator.
Parameters:
iter: a sequencesep: the separator (default space)nfields: maximum number of values per line (default 7)fmt: optional format function for each value
- map (fn, iter, arg)
-
return a sequence where every element of a sequence has been transformed
by a function. If you don’t supply an argument, then the function will
receive both values of a double-valued sequence, otherwise behaves rather like
tablex.map.
Parameters:
fn: a function to apply to elements; may take two argumentsiter: a sequence of one or two valuesarg: optional argument to pass to function.
- filter (iter, pred, arg)
-
filter a sequence using a predicate function
Parameters:
iter: a sequence of one or two valuespred: a boolean function; may take two argumentsarg: optional argument to pass to function.
- reduce (fun, iter, oldval)
-
‘reduce’ a sequence using a binary function.
Parameters:
fun: a function of two argumentsiter: a sequenceoldval: optional initial value
Usage:
seq.reduce(operator.add,seq.list{1,2,3,4}) == 10seq.reduce('-',{1,2,3,4,5}) == -13
- take (iter, n)
-
take the first n values from the sequence.
Parameters:
iter: a sequence of one or two valuesn: number of items to take
Returns:
-
a sequence of at most n items
- skip (iter, n)
-
skip the first n values of a sequence
Parameters:
iter: a sequence of one or more valuesn: number of items to skip
- enum (iter)
-
a sequence with a sequence count and the original value.
enum(copy(ls)) is a roundabout way of saying ipairs(ls).Parameters:
iter: a single or double valued sequence
Returns:
-
sequence of (i,v), i = 1..n and v is from iter.
- mapmethod (iter, name, arg1, arg2)
-
map using a named method over a sequence.
Parameters:
iter: a sequencename: the method namearg1: optional first extra argumentarg2: optional second extra argument
- last (iter)
-
a sequence of (last,current) values from another sequence.
This will return S(i-1),S(i) if given S(i)
Parameters:
iter: a sequence
- foreach (iter, fn)
-
call the function on each element of the sequence.
Parameters:
iter: a sequence with up to 3 valuesfn: a function
- lines (f)
-
create a wrapped iterator over all lines in the file.
Parameters:
f: either a filename or nil (for standard input)
Returns:
-
a sequence wrapper