Sequence protocol
Factor handbook » The language » Collections » Sequence operations

Next:The f object as a sequence


All sequences must be instances of a mixin class:
sequence

sequence? ( object -- ? )


All sequences must know their length:
length ( seq -- n )


At least one of the following two generic words must have a method for accessing elements; the sequence mixin has default definitions which are mutually recursive:
nth ( n seq -- elt )

nth-unsafe ( n seq -- elt )

?nth ( n seq -- elt/f )


Note that sequences are always indexed starting from zero.

At least one of the following two generic words must have a method for storing elements; the sequence mixin has default definitions which are mutually recursive:
set-nth ( elt n seq -- )

set-nth-unsafe ( elt n seq -- )

?set-nth ( elt n seq -- )


If your sequence is immutable, then you must implement either set-nth or set-nth-unsafe to simply call immutable to signal an error.

The following two generic words are optional, as not all sequences are resizable:
set-length ( n seq -- )

lengthen ( n seq -- )


An optional generic word for creating sequences of the same class as a given sequence:
like ( seq exemplar -- newseq )


Optional generic words for optimization purposes:
new-sequence ( len seq -- newseq )

new-resizable ( len seq -- newseq )


See also
Unsafe sequence operations