Module pl.seq

Manipulating sequences as iterators. functions & algorithms Steve Donovan, 2007-2009

Functions

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.
count_map (iter) A table where the key/values are the values and value counts of the sequence.
enum (iter) a sequence with a sequence count and the original value.
filter (iter, pred, arg) filter a sequence using a predicate function
foreach (iter, fn) call the function on each element of the sequence.
keys (t) return the keys of the table.
last (iter) a sequence of (last,current) values from another sequence.
lines (f) create a wrapped iterator over all lines in the file.
list (t) sequence adaptor for a table.
map (fn, iter, arg) return a sequence where every element of a sequence has been transformed by a function.
mapmethod (iter, name, arg1, arg2) map using a named method over a sequence.
matching (s, a) given a string, return a function(y) which matches y against the string.
minmax (iter) return the minimum and the maximum value of the sequence.
random (n, l, u) return an iterator of random numbers.
range (start, finish) create an iterator over a numerical range.
reduce (fun, seq, oldval) 'reduce' a sequence using a binary function.
sort (iter, comp) return an iterator to the sorted elements of a sequence.
sum (iter, fn) return the sum and element count of the sequence.
take (iter, n) take the first n values from the sequence.
unpack_copy (iter) useful to do Python-like assignments to sequences
zip (iter1, iter2) return an iterator which returns elements of two sequences.


Functions

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) == List{1,2,3}

Return value:

    a List
copy2 (iter, i1, i2)
create a table of pairs from the double-valued sequence.

Parameters:

  • iter: a double-valued sequence
  • i1:
  • i2:

Return value:

    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

Return value:

    a list-like table
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:

Return values:

  1. a map-like table
  2. a table

See also:

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

Return value:

    sequence of (i,v), i = 1..n and v is from iter.
filter (iter, pred, arg)
filter a sequence using a predicate function

Parameters:

  • iter: a sequence of one or two values
  • pred: a boolean function; may take two arguments
  • arg: optional argument to pass to function.
foreach (iter, fn)
call the function on each element of the sequence.

Parameters:

  • iter: a sequence with up to 3 values
  • fn: a function
keys (t)
return the keys of the table.

Parameters:

  • t: a list-like table

Return value:

    iterator over keys
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
lines (f)
create a wrapped iterator over all lines in the file.

Parameters:

  • f: either a filename or nil (for standard input)

Return value:

    a sequence wrapper
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
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 arguments
  • iter: a sequence of one or two values
  • arg: optional argument to pass to function.
mapmethod (iter, name, arg1, arg2)
map using a named method over a sequence.

Parameters:

  • iter: a sequence
  • name: the method name
  • arg1: optional second extra argument
  • arg2:
matching (s, a)
given a string, return a function(y) which matches y against the string.

Parameters:

  • s:
  • a: string
minmax (iter)
return the minimum and the maximum value of the sequence.

Parameters:

  • iter: a sequence
random (n, l, u)
return an iterator of random numbers.

Parameters:

  • n: the length of the sequence
  • l: same as the first optional argument to math.random
  • u: same as the second optional argument to math.random

Return value:

    a sequnce
range (start, finish)
create an iterator over a numerical range. Like the standard Python function xrange.

Parameters:

  • start: a number
  • finish: a number greater than start
reduce (fun, seq, oldval)
'reduce' a sequence using a binary function.

Parameters:

  • fun: a function of two arguments
  • seq: a sequence
  • oldval:

Usage:

    seq.reduce(operator.add,seq.list{1,2,3,4}) == 10
sort (iter, comp)
return an iterator to the sorted elements of a sequence.

Parameters:

  • iter: a sequence
  • comp: an optional comparison function (comp(x,y) is true if x < y)
sum (iter, fn)
return the sum and element count of the sequence.

Parameters:

  • iter: a sequence
  • fn: an optional function to apply to the values
take (iter, n)
take the first n values from the sequence.

Parameters:

  • iter: a sequence
  • n: number of values to take

Return value:

    a sequence of at most n values
unpack_copy (iter)
useful to do Python-like assignments to sequences

Parameters:

  • iter: a sequence

Usage:

    x,y = unpack_copy(numbers '10 20')) leaves x==10, y==20.
zip (iter1, iter2)
return an iterator which returns elements of two sequences.

Parameters:

  • iter1: a sequence
  • iter2: a sequence

Usage:

    for x,y in seq.zip(ls1,ls2) do....end

Valid XHTML 1.0!