rosetta-code.multisplit: new vocab
parent
0f9959a15b
commit
9b143a826d
|
@ -0,0 +1 @@
|
||||||
|
Alexander Ilin
|
|
@ -0,0 +1,8 @@
|
||||||
|
! Copyright (C) 2020 Alexander Ilin.
|
||||||
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
|
USING: sequences strings tools.test rosetta-code.multisplit ;
|
||||||
|
IN: rosetta-code.multisplit.tests
|
||||||
|
|
||||||
|
{ { "a" "" "b" "" "c" } } [
|
||||||
|
"a!===b=!=c" { "==" "!=" "=" } multisplit [ >string ] map
|
||||||
|
] unit-test
|
|
@ -0,0 +1,22 @@
|
||||||
|
! Copyright (C) 2020 Alexander Ilin.
|
||||||
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
|
USING: arrays fry kernel make sequences ;
|
||||||
|
|
||||||
|
IN: rosetta-code.multisplit
|
||||||
|
|
||||||
|
: ?pair ( ? x -- {?,x}/f )
|
||||||
|
over [ 2array ] [ 2drop f ] if ;
|
||||||
|
|
||||||
|
: best-separator ( seq -- pos index )
|
||||||
|
dup [ first ] map infimum '[ first _ = ] filter first first2 ;
|
||||||
|
|
||||||
|
: first-subseq ( separators seq -- n separator )
|
||||||
|
dupd [ swap [ subseq-start ] dip ?pair ] curry map-index sift
|
||||||
|
[ drop f f ] [ best-separator rot nth ] if-empty ;
|
||||||
|
|
||||||
|
: multisplit ( string separators -- seq )
|
||||||
|
'[
|
||||||
|
[ _ over first-subseq dup ] [
|
||||||
|
length -rot cut-slice swap , swap tail-slice
|
||||||
|
] while 2drop ,
|
||||||
|
] { } make ;
|
|
@ -0,0 +1 @@
|
||||||
|
examples
|
Loading…
Reference in New Issue