Skip to content

Instantly share code, notes, and snippets.

@geekduck
Created July 13, 2012 06:38
Show Gist options
  • Save geekduck/3103182 to your computer and use it in GitHub Desktop.
Save geekduck/3103182 to your computer and use it in GitHub Desktop.
normalizing mapArray
def mapArray = [
[id: 1, entry1: 1, entry2: 10],
[id: 2, entry1: 3, entry2: 4],
[id: 3, entry1: 9, entry2: 2],
[id: 4, entry1: 2, entry2: 5],
[id: 5, entry1: 5, entry2: 1]
]
def hoge = [
entry1: [max: 100, min: 1],
entry2: [max: 20, min: 10]
]
def searchElementMaxAndMin(def mapArray) {
def resultMap = [:]
mapArray.each { map ->
map.each {
if(resultMap[it.key]) {
resultMap[it.key].max = resultMap[it.key].max > it.value ? resultMap[it.key].max : it.value
resultMap[it.key].min = resultMap[it.key].min < it.value ? resultMap[it.key].min : it.value
} else {
resultMap.putAt(it.key, [max: it.value, min: it.value])
}
}
}
return resultMap
}
def maxminMap = searchElementMaxAndMin(mapArray)
def normalizing = { BigDecimal target, BigDecimal max, BigDecimal min ->
max != min ? (target - min) / (max - min) : 1.0
}
println mapArray.each { map -> map.collect {
//it.value = (maxminMap[it.key].max != maxminMap[it.key].min ? (it.value - maxminMap[it.key].min) / (maxminMap[it.key].max - maxminMap[it.key].min) : 1.0)
it.value = normalizing(it.value, maxminMap[it.key].max, maxminMap[it.key].min)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment