Groovy inconsistent destructuring / decomposition in lists?

Positive case: may be listed

groovy> println GroovySystem.version  
groovy> final data1 = [[99,2] , [100,4]] 
groovy> data1.collect{x,y->x+y} 

2.2.1
Result: [101, 104]

      

Negative case: fails to do the same

groovy> println GroovySystem.version  
groovy> final data = [x:[99,2] , y:[100,4]] 
groovy> data.collect{key,  val-> 
groovy>    val.collect{x,y->x+y} 
groovy> }.flatten() 

2.2.1
Exception thrown

groovy.lang.MissingMethodException: No signature of method: ConsoleScript80$_run_closure1_closure2.doCall() is applicable for argument types: (java.lang.Integer) values: [99]
Possible solutions: doCall(java.lang.Object, java.lang.Object), findAll(), findAll(), isCase(java.lang.Object), isCase(java.lang.Object)
    at ConsoleScript80$_run_closure1.doCall(ConsoleScript80:5)
    at ConsoleScript80.run(ConsoleScript80:4)

      

+3


source to share


2 answers


An alternative is



data.collect { key, val -> val.with { x, y -> x + y } }

      

+1


source


maybe you want



data.values().collect{x,y->x+y}

      

+2


source







All Articles