deep-nth ( index-seq nested-seq -- elt )


Vocabulary
sequences.extras

Inputs
index-seqa sequence
nested-seqa sequence


Outputs
eltan object


Word description
A version of nth that drills into a nested seq. Gets element n1 from nested-seq, then element n2 from that result, and so on, where n1, n2, etc come from index-seq. When index-seq is exhausted, the result is put on the stack. Note that this usage of "deep" is different from those in sequences.deep.

Examples
An empty index-seq leaves nested-seq on the stack:
USING: sequences.extras prettyprint ; { } { 1 2 3 } deep-nth .
{ 1 2 3 }

An index-seq with one element is the same as calling nth with that element:
USING: kernel sequences sequences.extras prettyprint ; { 1 } "abc" deep-nth 1 "abc" nth = .
t

Multiple elements in index-seq act like repeated calls to nth:
USING: sequences.extras prettyprint ; { 1 1 } { "zero" { "one-zero" "one-one" } } deep-nth .
"one-one"


Errors
Throws a bounds-error if any of the indices are negative, or greater than or equal to the length of nested-seq (at the corresponding nesting level).

Definition


: deep-nth ( index-seq nested-seq -- elt ) [ nth-of ] reduce ;