slice
#define slice(I, ...)
Construct a Slice
object on the stack over iterable I
.
reverse
#define reverse(I)
Construct a Slice
object that iterates over iterable I
in reverse order.
Usage
var x = tuple(
$S("Hello"), $S("There"), $S("World"), $S("!"));
/* Iterate over elements 0 to 2 */
foreach (s in slice(x, $I(2))) {
print("%s\n", s);
}
/* Iterate over elements 1 to 2 */
foreach (s in slice(x, $I(1), $I(2))) {
print("%s\n", s);
}
/* Iterate over every other element */
foreach (s in slice(x, _, _, $I(2))) {
print("%s\n", s);
}
/* Iterate backwards, starting from element 3 */
foreach (s in slice(x, _, $I(2), $I(-1))) {
print("%s\n", s);
}
Partial Iterable
The Slice
type is an iterable that allows one to only iterate over part of another iterable. Given some start, stop and step, only those entries described by the Slice
are returned in the iteration.
Under the hood the Slice
object still iterates over the whole iterable but it only returns those values in the range given.
struct Slice {
var iter;
var range;
};
assign
cmp
eq
neq
gt
lt
ge
le
name
brief
description
definition
get
set
mem
rem
key_type
val_type
foreach
iter_init
iter_next
iter_type
len
new
del
construct
destruct
show
look
print
scan