Paste: count occurrences (please improve)

Author: sconcrete
Mode: factor
Date: Fri, 23 Jan 2009 19:07:13
Plain Text |
! increment the value, or if the key does not exist, set the value to 1
: increment-val-for-key ( key assoc -- )
    2dup
    at* 
    [ 1 + ] [ drop 1 ] if
    -rot        ! have key assoc val, need val key assoc for input to set-at
    set-at ;

! count the number of occurrences of each character in str
: count-occurrences ( str -- assoc )
    V{ } dup
    -rot
    [ increment-val-for-key ] curry each ;

Annotation: count occurrences improved (thanks littledan)

Author: sconcrete
Mode: factor
Date: Fri, 23 Jan 2009 19:46:42
Plain Text |
! increments the value for the given key or sets it to 1 if key not found
: at++ ( key assoc -- )
    1 -rot at+ ;

! count the number of occurrences of each char in str    
: count-char-frequency ( str -- hashtable )
    H{ } tuck [ at++ ] curry each ;

Annotation: maybe an improvement?

Author: Dan
Mode: factor
Date: Fri, 23 Jan 2009 19:51:01
Plain Text |
: frequency ( seq -- hashtable )
    H{ } clone tuck '[ 1 swap _ at+ ] each ;

New Annotation

Summary:
Author:
Mode:
Body: