]> gitweb.factorcode.org Git - factor.git/commitdiff
splitting: new word split-indices, it's useful compiler.cfg.scheduling so let's add...
authorBjörn Lindqvist <bjourne@gmail.com>
Fri, 7 Nov 2014 23:34:53 +0000 (00:34 +0100)
committerDoug Coleman <doug.coleman@gmail.com>
Sun, 23 Nov 2014 01:31:38 +0000 (17:31 -0800)
core/splitting/splitting-docs.factor
core/splitting/splitting-tests.factor
core/splitting/splitting.factor
extra/tools/gc-decode/gc-decode.factor

index 92c5cb8636457bb36f85a5a7d964ca3185151334..5edadbbdae977986f44905051963c1933bc7b9cd 100644 (file)
@@ -60,6 +60,17 @@ HELP: split-when-slice
 { $values { "seq" sequence } { "quot" { $quotation ( ... elt -- ... ? ) } } { "pieces" "a new array" } }
 { $description "Splits " { $snippet "seq" } " at each occurrence of an element for which " { $snippet "quot" } " gives a true output and outputs an array of pieces as slices. The pieces do not include the elements along which the sequence was split." } ;
 
+HELP: split-indices
+{ $values { "seq" sequence } { "indices" sequence } { "pieces" "a new array" } }
+{ $description "Splits a sequence at the given indices." }
+{ $examples
+  { $example
+    "USING: prettyprint splitting ;"
+    "\"hello world\" { 3 6 } split-indices ."
+    "{ \"hel\" \"lo \" \"world\" }"
+  }
+} ;
+
 HELP: split
 { $values { "seq" sequence } { "separators" sequence } { "pieces" "a new array" } }
 { $description "Splits " { $snippet "seq" } " at each occurrence of an element of " { $snippet "separators" } " and outputs an array of pieces. The pieces do not include the elements along which the sequence was split." }
index 06cffb6964ef55d86463428940fdbec265fdeabb..c44df593e1c588b1607c40c611b01bb88c6f77d6 100644 (file)
@@ -104,3 +104,11 @@ unit-test
 [ "afoobfooc" "" "" replace ] unit-test
 
 { "" } [ "" "" "" replace ] unit-test
+
+{ { "Thi" "s " "i" "s a sequence" } } [
+    "This is a sequence" { 3 5 6 } split-indices
+] unit-test
+
+{ { "" "This" } } [
+    "This" { 0 } split-indices
+] unit-test
index 73afa24c2477057350415da37337411d7d24daf6..bdae704af72ace54a1e9caa5624078984afcc874 100644 (file)
@@ -100,6 +100,10 @@ PRIVATE>
 : split-slice ( seq separators -- pieces )
     [ member? ] curry split-when-slice ; inline
 
+: split-indices ( seq indices -- pieces )
+    over length suffix 0 swap [ dup swapd 2array ] map nip
+    [ first2 rot subseq ] with map ;
+
 GENERIC: string-lines ( str -- seq )
 
 M: string string-lines
index e9f133999c85c9e6efce6562bdf8d17bfd961096..90244750f1e289e18a08843d92529c0b2e3c8bd3 100644 (file)
@@ -1,15 +1,12 @@
 USING: accessors alien alien.c-types alien.data arrays assocs bit-arrays
 bit-arrays.private classes.struct fry grouping kernel math math.statistics
-sequences sequences.repeating vm words ;
+sequences sequences.repeating splitting vm words ;
 IN: tools.gc-decode
 
 ! Utils
 : byte-array>bit-array ( byte-array -- bit-array )
     [ integer>bit-array 8 f pad-tail ] { } map-as concat ;
 
-: split-indices ( seq indices -- parts )
-    over length suffix 0 prefix 2 clump [ first2 rot subseq ] with map ;
-
 : (cut-points) ( counts times -- seq )
     <repeats> cum-sum but-last ;