{ $subsection set= }
"A word used to implement the above:"
{ $subsection unique }
-"Counting elements in a sequence:"
-{ $subsection histogram }
-{ $subsection histogram* }
-"Combinators for implementing histogram:"
-{ $subsection sequence>assoc }
-{ $subsection sequence>assoc* }
-{ $subsection sequence>hashtable }
"Adding elements to sets:"
{ $subsection adjoin }
{ $subsection conjoin }
{ "newseq" sequence } }
{ $description "Maps a quotation onto a sequence, concatenates the results of the mapping, and removes duplicates." } ;
-HELP: histogram
-{ $values
- { "seq" sequence }
- { "hashtable" hashtable }
-}
-{ $examples
- { $example "! Count the number of times an element appears in a sequence."
- "USING: prettyprint sets ;"
- "\"aaabc\" histogram ."
- "H{ { 97 3 } { 98 1 } { 99 1 } }"
- }
-}
-{ $description "Returns a hashtable where the keys are the elements of the sequence and the values are the number of times they appeared in that sequence." } ;
-
-HELP: histogram*
-{ $values
- { "hashtable" hashtable } { "seq" sequence }
- { "hashtable" hashtable }
-}
-{ $examples
- { $example "! Count the number of times the elements of two sequences appear."
- "USING: prettyprint sets ;"
- "\"aaabc\" histogram \"aaaaaabc\" histogram* ."
- "H{ { 97 9 } { 98 2 } { 99 2 } }"
- }
-}
-{ $description "Takes an existing hashtable and uses " { $link histogram } " to continue counting the number of occurences of each element." } ;
-
-HELP: sequence>assoc
-{ $values
- { "seq" sequence } { "quot" quotation } { "exemplar" "an exemplar assoc" }
- { "assoc" assoc }
-}
-{ $examples
- { $example "! Iterate over a sequence and increment the count at each element"
- "USING: assocs prettyprint sets ;"
- "\"aaabc\" [ inc-at ] H{ } sequence>assoc ."
- "H{ { 97 3 } { 98 1 } { 99 1 } }"
- }
-}
-{ $description "Iterates over a sequence, allowing elements of the sequence to be added to a newly created " { $snippet "assoc" } " according to the passed quotation." } ;
-
-HELP: sequence>assoc*
-{ $values
- { "assoc" assoc } { "seq" sequence } { "quot" quotation }
- { "assoc" assoc }
-}
-{ $examples
- { $example "! Iterate over a sequence and add the counts to an existing assoc"
- "USING: assocs prettyprint sets kernel ;"
- "H{ { 97 2 } { 98 1 } } clone \"aaabc\" [ inc-at ] sequence>assoc* ."
- "H{ { 97 5 } { 98 2 } { 99 1 } }"
- }
-}
-{ $description "Iterates over a sequence, allowing elements of the sequence to be added to an existing " { $snippet "assoc" } " according to the passed quotation." } ;
-
-HELP: sequence>hashtable
-{ $values
- { "seq" sequence } { "quot" quotation }
- { "hashtable" hashtable }
-}
-{ $examples
- { $example "! Count the number of times an element occurs in a sequence"
- "USING: assocs prettyprint sets ;"
- "\"aaabc\" [ inc-at ] sequence>hashtable ."
- "H{ { 97 3 } { 98 1 } { 99 1 } }"
- }
-}
-{ $description "Iterates over a sequence, allowing elements of the sequence to be added to a hashtable according to the passed quotation." } ;
[ f ] [ { 1 } { } intersects? ] unit-test
-[
- H{
- { 97 2 }
- { 98 2 }
- { 99 2 }
- }
-] [
- "aabbcc" histogram
-] unit-test
: set= ( seq1 seq2 -- ? )
[ unique ] bi@ = ;
-
-<PRIVATE
-
-: (sequence>assoc) ( seq quot assoc -- assoc )
- [ swap curry each ] keep ; inline
-
-PRIVATE>
-
-: sequence>assoc* ( assoc seq quot: ( obj assoc -- ) -- assoc )
- rot (sequence>assoc) ; inline
-
-: sequence>assoc ( seq quot: ( obj assoc -- ) exemplar -- assoc )
- clone (sequence>assoc) ; inline
-
-: sequence>hashtable ( seq quot: ( obj hashtable -- ) -- hashtable )
- H{ } sequence>assoc ; inline
-
-: histogram* ( hashtable seq -- hashtable )
- [ inc-at ] sequence>assoc* ;
-
-: histogram ( seq -- hashtable )
- [ inc-at ] sequence>hashtable ;
--- /dev/null
+IN: histogram\r
+USING: help.markup help.syntax sequences hashtables quotations assocs ;\r
+\r
+HELP: histogram\r
+{ $values\r
+ { "seq" sequence }\r
+ { "hashtable" hashtable }\r
+}\r
+{ $examples \r
+ { $example "! Count the number of times an element appears in a sequence."\r
+ "USING: prettyprint sets ;"\r
+ "\"aaabc\" histogram ."\r
+ "H{ { 97 3 } { 98 1 } { 99 1 } }"\r
+ }\r
+}\r
+{ $description "Returns a hashtable where the keys are the elements of the sequence and the values are the number of times they appeared in that sequence." } ;\r
+\r
+HELP: histogram*\r
+{ $values\r
+ { "hashtable" hashtable } { "seq" sequence }\r
+ { "hashtable" hashtable }\r
+}\r
+{ $examples \r
+ { $example "! Count the number of times the elements of two sequences appear."\r
+ "USING: prettyprint sets ;"\r
+ "\"aaabc\" histogram \"aaaaaabc\" histogram* ."\r
+ "H{ { 97 9 } { 98 2 } { 99 2 } }"\r
+ }\r
+}\r
+{ $description "Takes an existing hashtable and uses " { $link histogram } " to continue counting the number of occurences of each element." } ;\r
+\r
+HELP: sequence>assoc\r
+{ $values\r
+ { "seq" sequence } { "quot" quotation } { "exemplar" "an exemplar assoc" }\r
+ { "assoc" assoc }\r
+}\r
+{ $examples \r
+ { $example "! Iterate over a sequence and increment the count at each element"\r
+ "USING: assocs prettyprint sets ;"\r
+ "\"aaabc\" [ inc-at ] H{ } sequence>assoc ."\r
+ "H{ { 97 3 } { 98 1 } { 99 1 } }"\r
+ }\r
+}\r
+{ $description "Iterates over a sequence, allowing elements of the sequence to be added to a newly created " { $snippet "assoc" } " according to the passed quotation." } ;\r
+\r
+HELP: sequence>assoc*\r
+{ $values\r
+ { "assoc" assoc } { "seq" sequence } { "quot" quotation }\r
+ { "assoc" assoc }\r
+}\r
+{ $examples \r
+ { $example "! Iterate over a sequence and add the counts to an existing assoc"\r
+ "USING: assocs prettyprint sets kernel ;"\r
+ "H{ { 97 2 } { 98 1 } } clone \"aaabc\" [ inc-at ] sequence>assoc* ."\r
+ "H{ { 97 5 } { 98 2 } { 99 1 } }"\r
+ }\r
+}\r
+{ $description "Iterates over a sequence, allowing elements of the sequence to be added to an existing " { $snippet "assoc" } " according to the passed quotation." } ;\r
+\r
+HELP: sequence>hashtable\r
+{ $values\r
+ { "seq" sequence } { "quot" quotation }\r
+ { "hashtable" hashtable }\r
+}\r
+{ $examples \r
+ { $example "! Count the number of times an element occurs in a sequence"\r
+ "USING: assocs prettyprint sets ;"\r
+ "\"aaabc\" [ inc-at ] sequence>hashtable ."\r
+ "H{ { 97 3 } { 98 1 } { 99 1 } }"\r
+ }\r
+}\r
+{ $description "Iterates over a sequence, allowing elements of the sequence to be added to a hashtable according to the passed quotation." } ;\r
+\r
+ARTICLE: "histogram" "Computing histograms"\r
+"Counting elements in a sequence:"\r
+{ $subsection histogram }\r
+{ $subsection histogram* }\r
+"Combinators for implementing histogram:"\r
+{ $subsection sequence>assoc }\r
+{ $subsection sequence>assoc* }\r
+{ $subsection sequence>hashtable } ;\r
+\r
+ABOUT: "histogram"\r
--- /dev/null
+IN: histogram.tests\r
+USING: help.markup help.syntax tools.test histogram ;\r
+\r
+[\r
+ H{\r
+ { 97 2 }\r
+ { 98 2 }\r
+ { 99 2 }\r
+ }\r
+] [\r
+ "aabbcc" histogram\r
+] unit-test\r
--- /dev/null
+! Copyright (C) 2009 Doug Coleman.\r
+! See http://factorcode.org/license.txt for BSD license.\r
+USING: kernel sequences assocs ;\r
+IN: histogram\r
+\r
+<PRIVATE\r
+\r
+: (sequence>assoc) ( seq quot assoc -- assoc )\r
+ [ swap curry each ] keep ; inline\r
+\r
+PRIVATE>\r
+\r
+: sequence>assoc* ( assoc seq quot: ( obj assoc -- ) -- assoc )\r
+ rot (sequence>assoc) ; inline\r
+\r
+: sequence>assoc ( seq quot: ( obj assoc -- ) exemplar -- assoc )\r
+ clone (sequence>assoc) ; inline\r
+\r
+: sequence>hashtable ( seq quot: ( obj hashtable -- ) -- hashtable )\r
+ H{ } sequence>assoc ; inline\r
+\r
+: histogram* ( hashtable seq -- hashtable )\r
+ [ inc-at ] sequence>assoc* ;\r
+\r
+: histogram ( seq -- hashtable )\r
+ [ inc-at ] sequence>hashtable ;\r