A little utility I wrote for clearing up code: words-not-used. You give it a word and a vocabulary and it tells you all the definitions in the vocab that aren't used by the execution of the word. It's handy for clearing up old code that is no longer used.

(n.b. the word doesn't have to be defined in the vocabulary)

Usage: e.g. "phil.vocab" \ doit words-not-used . { unused-word another-unused-word ... }

Here's the code. Is it useful enough to go in the factor distribution?


USING: kernel assocs definitions vocabs vars sequences namespaces ;
IN: prune-code

VAR: wordset

: add-word ( word -- )
  "" swap wordset> set-at ;

: (recursive-uses) ( defspec -- )
  dup wordset> key? 
  [ drop ] 
  [ dup add-word uses [ (recursive-uses) ] each ] if ;

: recursive-uses ( defspec -- hashtable )
  H{ } clone wordset [ (recursive-uses) wordset> ] with-variable ;

: list>hashtable ( list -- hash )
  H{ } clone tuck [ "" -rot set-at ] curry each ;

! returns all the words in the vocab not used by the recursively by defspec
: words-not-used ( vocab defspec -- list ) 
  recursive-uses swap words list>hashtable diff keys ;