This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View file

@ -0,0 +1,35 @@
fs = require 'fs'
make_index = (fns) ->
# words are indexed by filename and 1-based line numbers
index = {}
for fn in fns
for line, line_num in fs.readFileSync(fn).toString().split '\n'
words = get_words line
for word in words
word = mangle(word)
index[word] ||= []
index[word].push [fn, line_num+1]
index
grep = (index, word) ->
console.log "locations for '#{word}':"
locations = index[mangle(word)] || []
for location in locations
[fn, line_num] = location
console.log "#{fn}:#{line_num}"
console.log "\n"
get_words = (line) ->
words = line.replace(/\W/g, ' ').split ' '
(word for word in words when word != '')
mangle = (word) ->
# avoid conflicts with words like "constructor"
'_' + word
do ->
fns = (fn for fn in fs.readdirSync('.') when fn.match /\.coffee/)
index = make_index(fns)
grep index, 'make_index'
grep index, 'sort'

View file

@ -0,0 +1,15 @@
> coffee inverted_index.coffee
locations for 'make_index':
inverted_index.coffee:3
inverted_index.coffee:33
inverted_index.coffee:34
locations for 'sort':
anagrams.coffee:8
derangements.coffee:14
heap.coffee:34
heap.coffee:43
huffman.coffee:81
inverted_index.coffee:35
knuth_sample.coffee:12