From b42c6c442533505424646071a72e9c2266499b0c Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Thu, 18 Jun 2009 21:55:33 -0500 Subject: [PATCH] Move histogram words from core/sets to extra/histogram --- core/sets/sets-docs.factor | 76 ----------------------- core/sets/sets-tests.factor | 9 --- core/sets/sets.factor | 22 ------- extra/histogram/histogram-docs.factor | 83 ++++++++++++++++++++++++++ extra/histogram/histogram-tests.factor | 12 ++++ extra/histogram/histogram.factor | 26 ++++++++ 6 files changed, 121 insertions(+), 107 deletions(-) mode change 100644 => 100755 core/sets/sets-tests.factor create mode 100755 extra/histogram/histogram-docs.factor create mode 100755 extra/histogram/histogram-tests.factor create mode 100755 extra/histogram/histogram.factor diff --git a/core/sets/sets-docs.factor b/core/sets/sets-docs.factor index 298fcbeeae..0fce78dd68 100755 --- a/core/sets/sets-docs.factor +++ b/core/sets/sets-docs.factor @@ -20,13 +20,6 @@ $nl { $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 } @@ -134,72 +127,3 @@ HELP: gather { "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." } ; diff --git a/core/sets/sets-tests.factor b/core/sets/sets-tests.factor old mode 100644 new mode 100755 index be195a62cd..f9f8ba9e65 --- a/core/sets/sets-tests.factor +++ b/core/sets/sets-tests.factor @@ -30,12 +30,3 @@ IN: sets.tests [ f ] [ { 1 } { } intersects? ] unit-test -[ - H{ - { 97 2 } - { 98 2 } - { 99 2 } - } -] [ - "aabbcc" histogram -] unit-test diff --git a/core/sets/sets.factor b/core/sets/sets.factor index 421d43bb3d..062b624e8f 100755 --- a/core/sets/sets.factor +++ b/core/sets/sets.factor @@ -54,25 +54,3 @@ PRIVATE> : set= ( seq1 seq2 -- ? ) [ unique ] bi@ = ; - -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 ; diff --git a/extra/histogram/histogram-docs.factor b/extra/histogram/histogram-docs.factor new file mode 100755 index 0000000000..d81400fc0b --- /dev/null +++ b/extra/histogram/histogram-docs.factor @@ -0,0 +1,83 @@ +IN: histogram +USING: help.markup help.syntax sequences hashtables quotations assocs ; + +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." } ; + +ARTICLE: "histogram" "Computing histograms" +"Counting elements in a sequence:" +{ $subsection histogram } +{ $subsection histogram* } +"Combinators for implementing histogram:" +{ $subsection sequence>assoc } +{ $subsection sequence>assoc* } +{ $subsection sequence>hashtable } ; + +ABOUT: "histogram" diff --git a/extra/histogram/histogram-tests.factor b/extra/histogram/histogram-tests.factor new file mode 100755 index 0000000000..f0e7b3e80e --- /dev/null +++ b/extra/histogram/histogram-tests.factor @@ -0,0 +1,12 @@ +IN: histogram.tests +USING: help.markup help.syntax tools.test histogram ; + +[ + H{ + { 97 2 } + { 98 2 } + { 99 2 } + } +] [ + "aabbcc" histogram +] unit-test diff --git a/extra/histogram/histogram.factor b/extra/histogram/histogram.factor new file mode 100755 index 0000000000..70ddfd3af5 --- /dev/null +++ b/extra/histogram/histogram.factor @@ -0,0 +1,26 @@ +! Copyright (C) 2009 Doug Coleman. +! See http://factorcode.org/license.txt for BSD license. +USING: kernel sequences assocs ; +IN: histogram + +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 ;