def collector collector = {item -> item.collect {element -> element.value instanceof Map ? collector(element.value) : element.value}} assert 4 == collector([a: [q:1, w:1], b: [e:1], c: 1]).flatten().inject(0) { temp, val -> temp + val.value} assert 3 == collector([a: [q:1, w:[z:1, x:1]]]).flatten().inject(0) { temp, val -> temp + val.value}
Code Snippets, Ideas and Rants from a Tech Lead interested primarily in server side web development and leading teams who build web apps.
Monday, September 12, 2016
Flatten a Map of Maps
A question came up that interested me, someone had a map that contains children of possibly lists of other maps, and they wanted to flatten all these maps to count all the values contained in this map of lists of maps.
I just used a recursive closure to collect all the values of the maps and lists, flatten that into a list and then an inject (fold) to count up the values.
Sunday, September 11, 2016
Grails logging debug from your controller logback.groovy
To get your controllers and services logging debug messages edit your logback.groovy. Just add your package structure, for instance if your package structure was com.myapp
root(ERROR, ['STDOUT']) // Sets log level of all logger('grails.app.controllers.com.myapp', DEBUG, ['STDOUT'], false) // Turn on debug for all your controllers logger('grails.app.services.com.myapp', DEBUG, ['STDOUT'], false) // Turn on debug for all your services
Subscribe to:
Posts (Atom)