Slicing¶
This section contains functions to make subsequences from iterators.
Basic¶
- fun.nth(n, gen, param, state)¶
- iterator:nth(n)
- Parameters
n (uint) – a sequential number (indexed starting from
1
, like Lua tables)- Returns
n-th element of
gen, param, state
iterator
This function returns the n-th element of
gen, param, state
iterator. If the iterator does not have n items thennil
is returned.Examples:
> print(nth(2, range(5))) 2 > print(nth(10, range(5))) nil > print(nth(2, {"a", "b", "c", "d", "e"})) b > print(nth(2, drop_n(3, {"a", "b", "c", "d", "e"}))) e > print(nth(2, enumerate({"a", "b", "c", "d", "e"}))) 2 b
This function is optimized for basic array and string iterators and has
O(1)
complexity for these cases.
- fun.head(gen, param, state)¶
- iterator:head()
- Returns
a first element of
gen, param, state
iterator
Extract the first element of
gen, param, state
iterator. If the iterator is empty then an error is raised.Examples:
> print(head({"a", "b", "c", "d", "e"})) a > print(head({})) error: head: iterator is empty > print(head(range(0))) error: head: iterator is empty > print(head(enumerate({"a", "b"}))) 1 a
- fun.tail(gen, param, state)¶
- iterator:tail()
- Returns
gen, param, state
iterator without a first element
Return a copy of
gen, param, state
iterator without its first element. If the iterator is empty then an empty iterator is returned.Examples:
> each(print, tail({"a", "b", "c", "d", "e"})) b c d e > each(print, tail({})) > each(print, tail(range(0))) > each(print, tail(enumerate({"a", "b", "c"}))) 2 b 3 c
Subsequences¶
- fun.take_n(n, gen, param, state)¶
- iterator:take_n(n)
- Parameters
n (uint) – a number of elements to take
- Returns
an iterator on the subsequence of first n elements
Examples:
> each(print, take_n(5, range(10))) 1 2 3 4 5 > each(print, take_n(5, enumerate(duplicate('x')))) 1 x 2 x 3 x 4 x 5 x
- fun.take_while(predicate, gen, param, state)¶
- iterator:take_while(predicate)
- Returns
an iterator on the longest prefix of
gen, param, state
elements that satisfy predicate.
Examples:
> each(print, take_while(function(x) return x < 5 end, range(10))) 1 2 3 4 > each(print, take_while(function(i, a) return i ~=a end, enumerate({5, 3, 4, 4, 2}))) 1 5 2 3 3 4
See also
- fun.take(n_or_predicate, gen, param, state)¶
- iterator:take(n_or_predicate)
An alias for
take_n()
andtake_while()
that autodetects required function based on n_or_predicate type.
- fun.drop_n(n, gen, param, state)¶
- iterator:drop_n(n)
- Parameters
n (uint) – the number of elements to drop
- Returns
gen, param, state
iterator after skipping first n elements
Examples:
> each(print, drop_n(2, range(5))) 3 4 5 > each(print, drop_n(2, enumerate({'a', 'b', 'c', 'd', 'e'}))) 3 c 4 d 5 e
- fun.drop_while(predicate, gen, param, state)¶
- iterator:drop_while(predicate)
- Returns
gen, param, state
after skipping the longest prefix of elements that satisfy predicate.
Examples:
> each(print, drop_while(function(x) return x < 5 end, range(10))) 5 6 7 8 9 10
See also
- fun.drop(n_or_predicate, gen, param, state)¶
- iterator:drop(n_or_predicate)
An alias for
drop_n()
anddrop_while()
that autodetects required function based on n_or_predicate type.
- fun.span(n_or_predicate, gen, param, state)¶
- iterator:span(n_or_predicate)
- Returns
iterator, iterator
Return an iterator pair where the first operates on the longest prefix (possibly empty) of
gen, param, state
iterator of elements that satisfy predicate and second operates the remainder ofgen, param, state
iterator. Equivalent to:return take(n_or_predicate, gen, param, state), drop(n_or_predicate, gen, param, state);
Examples:
> each(print, zip(span(function(x) return x < 5 end, range(10)))) 1 5 2 6 3 7 4 8 > each(print, zip(span(5, range(10)))) 1 6 2 7 3 8 4 9 5 10
Note
gen, param, state
must be pure functional to work properly with the function.See also