igrigorik / tokyo-recipes

Lean & mean Tokyo Cabinet recipes (with Lua)

This URL has Read+Write access

tokyo-recipes / map-reduce / wordcount.lua
100644 24 lines (20 sloc) 0.429 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
--
-- wordcount map-reduce
--
 
function wordcount()
   function mapper(key, value, mapemit)
      for word in string.gmatch(string.lower(value), "%w+") do
         mapemit(word, 1)
      end
      return true
   end
 
   local res = ""
   function reducer(key, values)
      res = res .. key .. "\t" .. #values .. "\n"
      return true
   end
 
   if not _mapreduce(mapper, reducer) then
      res = nil
   end
   return res
end