News for hackers. Stuff that matters.
Re:Write your first MapReduce program in 20 minutes by strmpnk on Saturday June 25, 2011 @10:43AM (#2695734)
Kind of a odd example just to use map/reduce where each works just fine:

    result = Hash.new(0)
    Dir['test?.txt'].each do |file|
      File.read(file).split(' ').each {|word|
        result[word.downcase.tr('.,\'', '')] += 1}
    end
If you really needed map/reduce here you'd probably want to write it this way:

    Dir['test?.txt'].map do |file|
      Hash.new(0).tap {|result|
        File.read(file).split(' ').each {|word|
          result[word.downcase.tr('.,\'', '')] += 1}}
    end.reduce do |result, partial|
      result.merge(partial) {|k, v1, v2| v1 + v2}
    end
Re:Write your first MapReduce program in 20 minutes by wedesoft on Saturday June 25, 2011 @11:43AM (#2695859)
Interesting. I didn't know that you could pass a block to merge.