1 ! Copyright (C) 2020 Alexander Ilin.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays kernel make sequences ;
5 IN: rosetta-code.multisplit
7 : ?pair ( ? x -- {?,x}/f )
8 over [ 2array ] [ 2drop f ] if ;
10 : best-separator ( seq -- pos index )
11 dup [ first ] map infimum '[ first _ = ] find nip first2 ;
13 : first-subseq ( separators seq -- n separator )
14 dupd [ swap [ subseq-start ] dip ?pair ] curry map-index sift
15 [ drop f f ] [ best-separator rot nth ] if-empty ;
17 : multisplit ( string separators -- seq )
19 [ _ over first-subseq dup ] [
20 length -rot cut-slice swap , swap tail-slice