Skip to content

Instantly share code, notes, and snippets.

@simonask
Created January 6, 2009 10:01
Show Gist options
  • Save simonask/43750 to your computer and use it in GitHub Desktop.
Save simonask/43750 to your computer and use it in GitHub Desktop.
str: load_input // or whatever -- we have a string with newline-separated words
// The worker task for mapreduce
calculate_length_task: (task) {
message: task.pop // Blocks until there is a message
if message = "get_length" // Operator = overloaded for Message class
task.yield message.args[0].length
else
task.yield null
end
}
// Map
words: str.split("\n")
workers: words.map (word) { spawn &calculate_length_task }
// Do
word_lengths: workers.map (worker) { worker.get_length }
// Reduce
sum: word_lengths.sum
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment