//inspiration: http://hamletdarcy.blogspot.ca/2008/01/mapreduce-for-mere-mortals.html
// general purpose function
def mapReduceFunc = { data, mapFunction, reduceFunction ->
def mappedData = data.collect(mapFunction)
reduceFunction(mappedData)
}
// the data
Map dictionary = [
abacus: "a device for making arithmetic calculations",
arc: "any unbroken part of the circumference of a circle",
beaver: "a large, amphibious rodent of the genus Castor"
]
// the mapping function impl
def startingCharacter = { pair ->
pair.value = pair.key[0].toLowerCase()
return pair
}
// the reducing function impl
def countCharacters = { dataMap ->
Map result = [:]
('a'..'z').each{ result[it] = 0 }
dataMap.each { pair -> result[pair.value]++ }
return result
}
// put it all together!
def result1 = mapReduceFunc(dictionary, startingCharacter, countCharacters)
println result1
//// TWEAK THE I/O FUNCS! ////
// the mapping function impl
startingCharacter = { pair ->
return pair.key[0].toLowerCase()
}
// the reducing function impl
countCharacters = { dataList ->
Map result = [:]
('a'..'z').each{ result[it] = 0 }
dataList.each { it -> result[it]++ }
return result
}
// put it all together!
def result2 = mapReduceFunc(dictionary, startingCharacter, countCharacters)
println result2
assert result1 == result2
Showing posts with label reduce. Show all posts
Showing posts with label reduce. Show all posts
Wednesday, July 11, 2012
Conceptual Map/Reduce Example
Tuesday, March 29, 2011
Map/Reduce Constructs
.collect() & .inject():
def map1 = { it -> it > 5 ? it * 2 : it * 10 }
def reduce1 = { coll, i -> if (i > 29) coll << i; coll }
println ([1,2,3,4,5,6,7,8,11,88].collect(map1).inject([], reduce1))
def map2 = { it -> it.value *= 2; it }
def reduce2 = { coll, me -> String key = me.key[0]; coll[key] = coll[key] ?: 0; coll[key] += me.value; coll }
println ([a1:1,b1:2,a2:3,b2:4].collect(map2).inject([:], reduce2))
http://groovyconsole.appspot.com/script/450002
Subscribe to:
Comments (Atom)