Not logged in

Artifact a433321878465eba78d173c2e1c33a4be536feab

File util/wc.rx part of check-in [0400d5b703] - move some stuff into 'util' directory by crc on 2011-03-30 17:18:58. [annotate] Also file wc.rx part of check-in [268a006de8] - add 'wc' example by crc on 2011-03-24 16:45:34. [annotate]


{{
  3 elements content lines words

  : clean ( $- )
    withLength
    [ dup @ 10 13 within [ 999 swap &! sip ] ifTrue 1+ ] times drop ;

  : remaining ( $-n )
    32 ^strings'splitAtChar ^strings'chop drop ;

  : getLine ( $-$$ )
    999 ^strings'splitAtChar ^strings'chop ;

  : countWords ( $- )
    [ words ++ remaining dup 1 <> ] while drop ;

  : loadSourceData ( $- )
    here swap ^files'slurp here !content heap +! 32 , 0 ,
    @content clean ;

  : countLines ( $- )
    [ getLine lines ++ countWords dup 1 <> ] while drop ;
---reveal---
  : wc ( $-nnn )
    lines words [ 0 swap ! ] bi@
    heap [ loadSourceData @content countLines ] preserve
    @lines @words @content getLength [ 1- ] tri@ ;
}}