Paste: summarize
Author: | randy |
Mode: | factor |
Date: | Thu, 30 Apr 2009 12:47:21 |
Plain Text |
: (change-at) ( key assoc quot init -- )
[ 2dup at* ] 2dip '[ [ @ ] [ drop _ ] if ] call -rot set-at ;
:: summarize ( sequence quot/cond initial-value -- hash )
H{ } clone :> the-hash
quot/cond quotation? [ [ nip ] ] [ [ cond ] ] if :> nip/cond
sequence [ the-hash
over quot/cond nip/cond call
initial-value (change-at) ] each
the-hash ;
: count-all ( seq -- hash )
[ 1 + ] 1 summarize ;
: a-them ( seq -- hash ) [ "a" append ] "" summarize ;
: conditions ( -- seq-of-quot )
{
{ [ dup even? ] [ drop [ 2 + ] ] }
{ [ dup odd? ] [ drop [ 1 + ] ] }
[ "huh?" throw ]
} ;
: x-all ( seq -- hash )
conditions 2 summarize ;
Author: | rand |
Mode: | factor |
Date: | Thu, 30 Apr 2009 13:09:01 |
Plain Text |
the cond part changes the value based on the key in the sequence.
should the key's value change based on the stored value and a fixed quotation when meeting key in the sequence?
what is a use case for this situation?
New Annotation