split1-when-slice ( ... seq quot: ( ... elt -- ... ? ) -- ... before-slice after-slice ) (original) (raw)

split1-when-slice ( ... seq quot: ( ... elt -- ... ? ) -- ... before-slice after-slice )
Factor handbook » The language » Collections » Sequence operations » Splitting sequences

Prev: split1-when ( ... seq quot: ( ... elt -- ... ? ) -- ... before after )
Next: split1-last ( seq subseq -- before after )

Vocabulary
splitting

Inputs

seq a sequence
quot a quotation with stack effect ( ... elt -- ... ? )

Outputs

before-slice a slice
after-slice a slice

Word description
Splits seq at the first occurrence of an element for which quot gives a true output and outputs the pieces before and after the split as slices. If subseq does not occur in seq, then before is just seq and after is f.

Definition

USING: kernel math sequences ;

IN: splitting

: split1-when-slice
( ... seq quot: ( ... elt -- ... ? ) -- ... before-slice after-slice )
[ find drop ] keepd swap
[ dup 1 + rot snip-slice ] [ f ] if* ; inline