From 422c4b205138378469acac67d66ef1207a02e226 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Lindqvist?= Date: Sat, 8 Nov 2014 00:34:53 +0100 Subject: [PATCH] splitting: new word split-indices, it's useful compiler.cfg.scheduling so let's add it to the splitting vocab too --- core/splitting/splitting-docs.factor | 11 +++++++++++ core/splitting/splitting-tests.factor | 8 ++++++++ core/splitting/splitting.factor | 4 ++++ extra/tools/gc-decode/gc-decode.factor | 5 +---- 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/core/splitting/splitting-docs.factor b/core/splitting/splitting-docs.factor index 92c5cb8636..5edadbbdae 100644 --- a/core/splitting/splitting-docs.factor +++ b/core/splitting/splitting-docs.factor @@ -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." } diff --git a/core/splitting/splitting-tests.factor b/core/splitting/splitting-tests.factor index 06cffb6964..c44df593e1 100644 --- a/core/splitting/splitting-tests.factor +++ b/core/splitting/splitting-tests.factor @@ -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 diff --git a/core/splitting/splitting.factor b/core/splitting/splitting.factor index 73afa24c24..bdae704af7 100644 --- a/core/splitting/splitting.factor +++ b/core/splitting/splitting.factor @@ -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 diff --git a/extra/tools/gc-decode/gc-decode.factor b/extra/tools/gc-decode/gc-decode.factor index e9f133999c..90244750f1 100644 --- a/extra/tools/gc-decode/gc-decode.factor +++ b/extra/tools/gc-decode/gc-decode.factor @@ -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 ) cum-sum but-last ;