Tidying up factor code
Jan 15th, 2008 by Phil Dawes
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 ;

Hi Phil,
To get all words used by a word, recursively, use
[ word-def quot-uses ] closure
Also words list>hashtable is just vocab-words
And list>hashtable can be written as
: list>hashtable [ “” ] H{ } map>assoc ;
Assuming you want “” as the values, but usually we use equal keys/values when representing a set as a hashtable:
: list>hashtable [ dup ] H{ } map>assoc ;