set-nth ( elt n seq -- )
Factor documentation > Factor handbook > The language > Collections > Sequence operations > Sequence protocol
Prev:nth-unsafe ( n seq -- elt )
Next:set-nth-unsafe ( elt n seq -- )


Vocabulary
sequences

Inputs and outputs
eltan object
na non-negative integer
seqa mutable sequence


Generic word contract
Sets the nth element of the sequence. Storing beyond the end of a resizable sequence such as a vector or string buffer grows the sequence.

Errors
Throws an error if the index is negative, or if the sequence is not resizable and the index is greater than or equal to the length of the sequence.

Throws an error if the sequence cannot hold elements of the given type.

Side effects
Modifies seq

Definition
IN: sequences

GENERIC: set-nth ( elt n seq -- )


Methods
USING: grouping.private kernel sequences ;

M: chunking set-nth group@ <slice> 0 swap copy ;


USING: growable sequences sequences.private ;

M: growable set-nth ensure set-nth-unsafe ; inline


USING: sequences ;

M: immutable-sequence set-nth immutable ;


USING: accessors assocs game.models.util kernel locals
sequences ;

M:: indexed-seq set-nth ( elt n seq -- )
seq dseq>> :> dseq seq iseq>> :> iseq seq rassoc>>
:> rassoc seq length n = not [ seq immutable ] when
elt rassoc at
[ iseq push ] [
dseq length [ elt rassoc set-at ] [ iseq push ] bi
elt dseq push
] if* ; inline


USING: sequences sequences.modified ;

M: modified set-nth modified-set-nth ;


USING: sequences sequences.private ;

M: sequence set-nth bounds-check set-nth-unsafe ; inline


USING: accessors kernel sequences xml.data ;

M: tag set-nth [ children>> ] call \ set-nth execute ;


USING: sequences ;

M: virtual-sequence set-nth virtual@ set-nth ; inline


USING: accessors kernel sequences xml.data ;

M: xml-chunk set-nth [ seq>> ] call \ set-nth execute ;


USING: accessors kernel sequences xml.data ;

M: xml set-nth [ body>> ] call \ set-nth execute ;