From a96ff62f5c77fb74be70081bf8a8cf5e8b2dd65a Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Mon, 1 Apr 2013 09:14:27 -0700 Subject: [PATCH] splitting.extras: adding split* and split-find. --- extra/splitting/extras/extras-docs.factor | 16 ++++++++++ extra/splitting/extras/extras-tests.factor | 28 ++++++++++++++++++ extra/splitting/extras/extras.factor | 34 ++++++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 extra/splitting/extras/extras-docs.factor create mode 100644 extra/splitting/extras/extras-tests.factor create mode 100644 extra/splitting/extras/extras.factor diff --git a/extra/splitting/extras/extras-docs.factor b/extra/splitting/extras/extras-docs.factor new file mode 100644 index 0000000000..96f497160f --- /dev/null +++ b/extra/splitting/extras/extras-docs.factor @@ -0,0 +1,16 @@ +USING: help.markup help.syntax sequences splitting strings ; +IN: splitting.extras + +HELP: split*-when +{ $values { "seq" "a sequence" } { "quot" { $quotation "( ... elt -- ... ? )" } } { "pieces" "a new array" } } +{ $description "A variant of " { $link split-when } " that includes the elements along which the sequence was split." } +{ $examples { $example "USING: ascii kernel prettyprint splitting.extras ;" "\"hello,world-how.are:you\" [ letter? not ] split*-when ." "{ \"hello\" \",\" \"world\" \"-\" \"how\" \".\" \"are\" \":\" \"you\" }" } } ; + +HELP: split* +{ $values { "seq" "a sequence" } { "separators" "a sequence" } { "pieces" "a new array" } } +{ $description "A variant of " { $link split } " that includes the elements along which the sequence was split." } +{ $examples { $example "USING: prettyprint splitting.extras ;" "\"hello world-how are you?\" \" -\" split* ." "{ \"hello\" \" \" \"world\" \"-\" \"how\" \" \" \"are\" \" \" \"you?\" }" } } ; + +HELP: split-find +{ $values { "seq" "a sequence" } { "quot" { $quotation "( seq -- i )" } } { "pieces" "a new array" } } +{ $description "Splits a sequence into slices using the provided quotation to find split points." } ; diff --git a/extra/splitting/extras/extras-tests.factor b/extra/splitting/extras/extras-tests.factor new file mode 100644 index 0000000000..fd8bd2d297 --- /dev/null +++ b/extra/splitting/extras/extras-tests.factor @@ -0,0 +1,28 @@ + +USING: ascii kernel math sequences strings tools.test ; + +IN: splitting.extras + +{ { } } [ { } { 0 } split* ] unit-test +{ { { 1 2 3 } } } [ { 1 2 3 } { 0 } split* ] unit-test +{ { { 0 } } } [ { 0 } { 0 } split* ] unit-test +{ { { 0 } { 0 } } } [ { 0 0 } { 0 } split* ] unit-test +{ { { 1 2 } { 0 } { 3 } { 0 } { 0 } } } [ { 1 2 0 3 0 0 } { 0 } split* ] unit-test +{ { "hello" } } [ "hello" " " split* ] unit-test +{ { " " " " "hello" } } [ " hello" " " split* ] unit-test +{ { "hello" " " " " " " "world" } } [ "hello world" " " split* ] unit-test +{ { "hello" " " " " " " "world" " " } } [ "hello world " " " split* ] unit-test + +{ { } } [ { } [ 0 > ] split*-when ] unit-test +{ { { 0 } } } [ { 0 } [ 0 > ] split*-when ] unit-test +{ { { 0 0 } } } [ { 0 0 } [ 0 > ] split*-when ] unit-test +{ { { 1 } { 2 } { 0 } { 3 } { 0 0 } } } [ { 1 2 0 3 0 0 } [ 0 > ] split*-when ] unit-test +{ { { 1 } { 2 3 } { 1 } { 4 5 } { 1 } { 6 } } } [ + 1 { 1 2 3 1 4 5 1 6 } [ dupd = ] split*-when nip +] unit-test + +{ { "hello" " " " " " " "world" } } [ + "hello world" + [ [ blank? ] find drop ] split-find + [ >string ] map +] unit-test diff --git a/extra/splitting/extras/extras.factor b/extra/splitting/extras/extras.factor new file mode 100644 index 0000000000..e950a608bb --- /dev/null +++ b/extra/splitting/extras/extras.factor @@ -0,0 +1,34 @@ +USING: kernel math sequences ; + +IN: splitting.extras + + + +: split*-when ( ... seq quot: ( ... elt -- ... ? ) -- ... pieces ) + [ 0 ] 2dip [ subseq ] (split*) ; inline + +: split*-when-slice ( ... seq quot: ( ... elt -- ... ? ) -- ... pieces ) + [ 0 ] 2dip [ ] (split*) ; inline + +: split* ( seq separators -- pieces ) + [ member? ] curry split*-when ; inline + +: split*-slice ( seq separators -- pieces ) + [ member? ] curry split*-when-slice ; inline + +: split-find ( seq quot: ( seq -- i ) -- pieces ) + [ dup empty? not ] swap [ [ dup ] ] dip + [ [ [ 1 ] when-zero cut-slice swap ] [ f swap ] if* ] compose + compose produce nip ; inline