subseq* ( from to seq -- subseq )


Vocabulary
sequences.extras

Inputs
froman integer
toan integer
seqa sequence


Outputs
subseqa sequence


Word description
Outputs a new sequence using positions relative to one or both ends of the sequence. Positive values describes offsets relative to the start of the sequence, negative values relative to the end. Values of f for from indicate the beginning of the sequence, while an f for to indicates the end of the sequence.

Notes
Both from and to can be safely set to values outside the length of the sequence. Also, from can safely reference a smaller or greater index position than to.

Examples
Using a negative relative index:
USING: prettyprint sequences.extras ; 2 -1 "abcdefg" subseq* .
"cdef"

Using optional indices:
USING: prettyprint sequences.extras ; f -4 "abcdefg" subseq* .
"abc"

Using larger-than-necessary indices:
USING: prettyprint sequences.extras ; 0 10 "abcdefg" subseq* .
"abcdefg"

Trimming from either end of the sequence.
USING: prettyprint sequences.extras ; 1 -1 "abcdefg" subseq* .
"bcdef"


Definition


:: subseq* ( from to seq -- subseq )
seq length :> len from [ dup 0 < [ len + ] when ] [ 0 ] if*
to [ dup 0 < [ len + ] when ] [ len ] if*
[ 0 len clamp ] bi@ dupd max seq subseq ;