]> gitweb.factorcode.org Git - factor.git/commitdiff
grouping.extras: new word group-by, like sql GROUP BY but is order-preserving
authorBjörn Lindqvist <bjourne@gmail.com>
Sat, 14 Dec 2013 20:11:39 +0000 (21:11 +0100)
committerBjörn Lindqvist <bjourne@gmail.com>
Sat, 14 Dec 2013 20:11:39 +0000 (21:11 +0100)
extra/grouping/extras/extras-docs.factor [new file with mode: 0644]
extra/grouping/extras/extras-tests.factor
extra/grouping/extras/extras.factor

diff --git a/extra/grouping/extras/extras-docs.factor b/extra/grouping/extras/extras-docs.factor
new file mode 100644 (file)
index 0000000..50f6d62
--- /dev/null
@@ -0,0 +1,14 @@
+USING: help.markup help.syntax sequences splitting strings ;
+
+IN: grouping.extras
+
+HELP: group-by
+{ $values { "seq" "a sequence" } { "quot" { $quotation "( ... elt -- ... key )" } } { "groups" "a new assoc" } }
+{ $description "Groups the elements by the key received by applying quot to each element in the sequence." }
+{ $examples
+  { $example
+    "USING: grouping.extras unicode.data ;"
+    "\"THis String Has  CasE!\" [ category ] group-by [ last >string ] map ."
+    "{ \"TH\" \"is\" \" \" \"S\" \"tring\" \" \" \"H\" \"as\" \"  \" \"C\" \"as\" \"E\" \"!\" }"
+  }
+} ;
index 76ef71f627b8c440202c63f1c9a9e3f36a7d6998..d696f5f4806a1ff33e830277fb9066700c392c87 100644 (file)
@@ -1,4 +1,4 @@
-USING: arrays tools.test ;
+USING: arrays kernel math math.functions sequences tools.test ;
 IN: grouping.extras
 
 { { } } [ { 1 } [ 2array ] 2clump-map ] unit-test
@@ -21,3 +21,21 @@ IN: grouping.extras
 
 { { B{ 97 115 } B{ 100 102 } } } [ "asdf" 2 B{ } group-as ] unit-test
 { { { 97 115 } { 115 100 } { 100 102 } } } [ "asdf" 2 { } clump-as ] unit-test
+
+[
+    {
+        { 0 { 0 1 2 } }
+        { 1 { 3 4 5 } }
+        { 2 { 6 7 8 } }
+        { 3 { 9 } } }
+] [
+    10 iota [ 3 / floor ] group-by
+] unit-test
+
+[
+    { { t { 0 1 2 3 4 5 6 7 8 9 } } }
+] [ 10 iota [ drop t ] group-by ] unit-test
+
+[
+    { }
+] [ { } [ drop t ] group-by ] unit-test
index b48806528cd4ea194b0e5e1be67ba0c113fb47c9..fdac237e59570337f74bac1d3510251aff2eb16c 100644 (file)
@@ -1,4 +1,4 @@
-USING: accessors combinators fry grouping kernel macros math
+USING: accessors arrays combinators fry grouping kernel macros math
 math.ranges sequences sequences.generalizations
 sequences.private ;
 
@@ -48,3 +48,13 @@ INSTANCE: tail-clumps immutable-sequence
 
 : group-as ( seq n exemplar -- array )
     [ <groups> ] dip [ like ] curry map ;
+
+: (group-by-loop) ( elt key groups -- groups' )
+    2dup [ nip empty? ] [ ?last ?first = not ] 2bi or [
+        -rot swap 1array
+    ] [
+        nip unclip-last rot [ first2 ] dip suffix
+    ] if 2array suffix ;
+
+: group-by ( seq quot: ( elt -- key ) -- groups )
+    '[ dup _ call( x -- y ) rot (group-by-loop) ] { } swap reduce ;