Paste: summarize

Author: randy
Mode: factor
Date: Thu, 30 Apr 2009 12:47:21
Plain Text |
! takes a sequence of items, adds the different items to a hash as keys and does something to the value, either based on the item or unconditionally.
! if the key repeats, the value will change again.
 
: (change-at) ( key assoc quot init -- ) ! applies quot or 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 ! key assoc
               over quot/cond nip/cond call  ! key assoc quot
               initial-value (change-at) ] each 
    the-hash ; 
    
! btw, what is a better way to thread the hash thru the code without using global vars or locals?

! example words which use summarize

: count-all ( seq -- hash )
    [ 1 + ] 1 summarize ;
! { 1 2 3 4 4 6 7 } => H{ { 1 1 } { 2 1 } { 3 1 } { 4 2 } { 6 1 } { 7 1 } }

    
: a-them  ( seq -- hash ) [ "a" append ] "" summarize ;
! { 1 2 3 4 4 6 7 } => H{ { 1 "" } { 2 "" } { 3 "" } { 4 "a" } { 6 "" } { 7 "" } }
    
! example conditions    
: conditions ( -- seq-of-quot ) ! must output a quot
 {
    { [ dup even? ] [ drop [ 2 + ] ] } 
    { [ dup odd? ]  [ drop [ 1 + ] ] }
    [ "huh?" throw ] 
 } ;    
 
: x-all ( seq -- hash )
    conditions 2 summarize ;        
! { 1 2 3 4 4 6 7 } => H{ { 1 2 } { 2 2 } { 3 2 } { 4 4 } { 6 2 } { 7 2 } } 


! surely there is already an idiomatic/built-in way to do this ... if u know leave a comment or tell me on irc. thanks

Annotation: note

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

Summary:
Author:
Mode:
Body: