Paste: Ruby Quiz 12
Author: | jwmerrill |
Mode: | factor |
Date: | Tue, 9 Dec 2008 04:36:18 |
Plain Text |
USING: kernel io io.files io.encodings.utf8 sequences sorting unicode.case
math math.order hashtables assocs vectors arrays fry sets prettyprint
strings ;
IN: scrabble-stems
: seven-letter-words
"/usr/share/dict/words" utf8 file-lines [ length 7 = ] filter ;
: word-class >lower [ <=> ] sort ;
: stems
word-class [ length ] keep
'[ _ [ remove-nth ] [ nth ] 2bi ] { } map>assoc prune ;
: add-association
[ push-at ] keep ;
: add-word
stems swap [ first2 spin add-association ] reduce ;
: stem-hash
H{ } clone [ add-word ] reduce [ prune ] assoc-map ;
: print-results
seven-letter-words stem-hash >alist
[ [ second length ] bi@ <=> ] sort reverse
[ second length 16 > ] filter
[ [ >string write " " write ] [ length . ] bi* ] assoc-each ;
MAIN: print-results
New Annotation