slice

slice <iterable> <start> [end]

A bounded sub-range of an iterable, as a new iterable.

slice takes two or three arguments. With a start only, it yields every element from that index onward; with a start and an end, it yields the elements at indices start up to but not including end.

Arguments

Returns

An iterable over the selected elements. Consume it with each, count, first, or another iterable consumer.

Examples

after command (say) {
  do "say middle = [count [slice [words "a b c d e"] 1 3]]"
}

slice ... 1 3 keeps the elements at indices 1 and 2 ("b" and "c"), so [count ...] evaluates to 2 and the mob says "middle = 2."

after command (say) {
  each [slice [words "a b c d e"] 2] { <w>
    do "say tail word $w"
  }
}

With no end, the slice runs from index 2 to the end — "c", "d", and "e" — and the mob says one line per word. (Counting that same slice gives 3.)

See also