]> gitweb.factorcode.org Git - factor.git/commitdiff
rosetta-code.multisplit: refactor
authorDoug Coleman <doug.coleman@gmail.com>
Fri, 29 Jul 2022 21:29:25 +0000 (16:29 -0500)
committerDoug Coleman <doug.coleman@gmail.com>
Sun, 31 Jul 2022 18:25:41 +0000 (13:25 -0500)
extra/rosetta-code/multisplit/multisplit.factor

index 430077f11085ec10a98a24c1f9bc9540805f0363..3437741229f4cbba1a829b1265e4b2bdfde227b2 100644 (file)
@@ -1,22 +1,17 @@
 ! Copyright (C) 2020 Alexander Ilin.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: arrays kernel make sequences ;
+USING: arrays assocs 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 _ = ] find nip 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 ;
+: first-subseq ( seq separators -- n separator )
+    tuck
+    [ [ subsequence-starts ] dip 2array ] withd map-index sift-keys
+    [ drop f f ] [ [ first ] infimum-by first2 rot nth ] if-empty ;
 
 : multisplit ( string separators -- seq )
     '[
-        [ _ over first-subseq dup ] [
-            length -rot cut-slice swap , swap tail-slice
+        [ dup _ first-subseq dup ] [
+            length -rot cut-slice [ , ] dip swap tail-slice
         ] while 2drop ,
     ] { } make ;