new-nth ( val i seq -- seq' )
Persistent sequence protocol
Next:ppush ( val seq -- seq' )


Vocabulary
persistent.sequences

Inputs and outputs
valan object
ian integer
seqa sequence
seq'a sequence


Generic word contract
Persistent analogue of set-nth. Outputs a new sequence with the ith element replaced by val.

Definition
IN: persistent.sequences

GENERIC: new-nth ( val i seq -- seq' )


Methods
USING: accessors kernel math persistent.sequences
persistent.vectors ;

M: persistent-vector new-nth
2dup count>> =
[ nip ppush ] [
clone 2dup tail-offset >=
[ [ node-mask ] dip [ node-set-nth ] change-tail ]
[ [ (new-nth) ] change-root ] if
] if ;


USING: kernel persistent.sequences sequences ;

M: sequence new-nth clone [ set-nth ] keep ;