Change how sequence>assoc and friends work. Now they take two quotations, the first of which prepares the key/value pair and the second insert it into the assoc. mrjbq7's group-by, formerly collect-values, is now called collect-by.

db4
Doug Coleman 2011-04-09 23:51:14 -05:00
parent 38fd731fa9
commit 7aa7b08d42
2 changed files with 25 additions and 22 deletions

View File

@ -112,41 +112,42 @@ HELP: sorted-histogram
HELP: sequence>assoc HELP: sequence>assoc
{ $values { $values
{ "seq" sequence } { "quot" quotation } { "exemplar" "an exemplar assoc" } { "seq" sequence } { "quot1" quotation } { "quot2" quotation } { "exemplar" "an exemplar assoc" }
{ "assoc" assoc } { "assoc" assoc }
} }
{ $description "Iterates over a sequence, allowing elements of the sequence to be added to a newly created " { $snippet "assoc" } " according to the passed quotation." } { $description "Iterates over a sequence, allowing elements of the sequence to be added to a newly created " { $snippet "assoc" } ". The first quotation gets passed an element from the sequence and should output whatever the second quotation needs, e.g. ( element -- value key ) if the second quotation is inserting into an assoc." }
{ $examples { $examples
{ $example "! Iterate over a sequence and increment the count at each element" { $example "! Iterate over a sequence and increment the count at each element"
"! The first quotation has stack effect ( key -- key ), a no-op"
"USING: assocs prettyprint math.statistics ;" "USING: assocs prettyprint math.statistics ;"
"\"aaabc\" [ inc-at ] H{ } sequence>assoc ." "\"aaabc\" [ ] [ inc-at ] H{ } sequence>assoc ."
"H{ { 97 3 } { 98 1 } { 99 1 } }" "H{ { 97 3 } { 98 1 } { 99 1 } }"
} }
} ; } ;
HELP: sequence>assoc! HELP: sequence>assoc!
{ $values { $values
{ "assoc" assoc } { "seq" sequence } { "quot" quotation } { "assoc" assoc } { "seq" sequence } { "quot1" quotation } { "quot2" quotation }
} }
{ $description "Iterates over a sequence, allowing elements of the sequence to be added to an existing " { $snippet "assoc" } " according to the passed quotation." } { $description "Iterates over a sequence, allowing elements of the sequence to be added to an existing " { $snippet "assoc" } ". The first quotation gets passed an element from the sequence and should output whatever the second quotation needs, e.g. ( element -- value key ) if the second quotation is inserting into an assoc." }
{ $examples { $examples
{ $example "! Iterate over a sequence and add the counts to an existing assoc" { $example "! Iterate over a sequence and add the counts to an existing assoc"
"USING: assocs prettyprint math.statistics kernel ;" "USING: assocs prettyprint math.statistics kernel ;"
"H{ { 97 2 } { 98 1 } } clone \"aaabc\" [ inc-at ] sequence>assoc! ." "H{ { 97 2 } { 98 1 } } clone \"aaabc\" [ ] [ inc-at ] sequence>assoc! ."
"H{ { 97 5 } { 98 2 } { 99 1 } }" "H{ { 97 5 } { 98 2 } { 99 1 } }"
} }
} ; } ;
HELP: sequence>hashtable HELP: sequence>hashtable
{ $values { $values
{ "seq" sequence } { "quot" quotation } { "seq" sequence } { "quot1" quotation } { "quot2" quotation }
{ "hashtable" hashtable } { "hashtable" hashtable }
} }
{ $description "Iterates over a sequence, allowing elements of the sequence to be added to a hashtable according to the passed quotation." } { $description "Iterates over a sequence, allowing elements of the sequence to be added to a hashtable according a combination of the first and second quotations. The quot1 is passed each element, and quot2 gets the hashtable on the top of the stack with quot1's results underneath for inserting into the hashtable." }
{ $examples { $examples
{ $example "! Count the number of times an element occurs in a sequence" { $example "! Count the number of times an element occurs in a sequence"
"USING: assocs prettyprint math.statistics ;" "USING: assocs prettyprint math.statistics ;"
"\"aaabc\" [ inc-at ] sequence>hashtable ." "\"aaabc\" [ ] [ inc-at ] sequence>hashtable ."
"H{ { 97 3 } { 98 1 } { 99 1 } }" "H{ { 97 3 } { 98 1 } { 99 1 } }"
} }
} ; } ;

View File

@ -1,8 +1,7 @@
! Copyright (C) 2008 Doug Coleman, Michael Judge. ! Copyright (C) 2008 Doug Coleman, Michael Judge.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: arrays combinators kernel math math.functions USING: assocs combinators generalizations kernel locals math
math.order sequences sorting locals sequences.private math.functions math.order sequences sequences.private sorting ;
assocs fry ;
IN: math.statistics IN: math.statistics
: mean ( seq -- x ) : mean ( seq -- x )
@ -59,31 +58,34 @@ IN: math.statistics
<PRIVATE <PRIVATE
: (sequence>assoc) ( seq quot assoc -- assoc ) : (sequence>assoc) ( seq quot1 quot2 assoc -- assoc )
[ swap curry each ] keep ; inline [ swap curry compose each ] keep ; inline
PRIVATE> PRIVATE>
: sequence>assoc! ( assoc seq quot: ( obj assoc -- ) -- assoc ) : sequence>assoc! ( assoc seq quot1 quot2 -- assoc )
rot (sequence>assoc) ; inline 4 nrot (sequence>assoc) ; inline
: sequence>assoc ( seq quot: ( obj assoc -- ) exemplar -- assoc ) : sequence>assoc ( seq quot1 quot2 exemplar -- assoc )
clone (sequence>assoc) ; inline clone (sequence>assoc) ; inline
: sequence>hashtable ( seq quot: ( obj hashtable -- ) -- hashtable ) : sequence>hashtable ( seq quot1 quot2 -- hashtable )
H{ } sequence>assoc ; inline H{ } sequence>assoc ; inline
: histogram! ( hashtable seq -- hashtable ) : histogram! ( hashtable seq -- hashtable )
[ inc-at ] sequence>assoc! ; [ ] [ inc-at ] sequence>assoc! ;
: histogram ( seq -- hashtable ) : histogram ( seq -- hashtable )
[ inc-at ] sequence>hashtable ; [ ] [ inc-at ] sequence>hashtable ;
: sorted-histogram ( seq -- alist ) : sorted-histogram ( seq -- alist )
histogram >alist sort-values ; histogram >alist sort-values ;
: collect-values ( seq quot: ( obj hashtable -- ) -- hash ) : collect-pairs ( seq quot -- hashtable )
'[ [ dup @ ] dip push-at ] sequence>hashtable ; inline [ push-at ] sequence>hashtable ; inline
: collect-by ( seq quot -- hashtable )
[ dup ] prepose collect-pairs ; inline
: mode ( seq -- x ) : mode ( seq -- x )
histogram >alist histogram >alist