|
View:
New views
9 Messages
—
Rating Filter:
Alert me
|
|
|
Simpler way to do simple mapping?I am missing something in my groovy idiom collection: A simpler way
to create maps out of lists. Essentially, I have some source object 'X' and it has 2 methods, getKeys() and getValue(key). It should be easy to compose a map of keys onto values, but I have not yet found a good way to do it. The problem with .groupBy is that the values it returns are lists, not single objects. I need: map([1, 2, 3]) { it <= 2 ? 'safe' : 'charm' } -> [1: 'safe', 2: 'safe', 3: 'charm'] Here is a recent use case, using an MBean to demonstrate. In the 'After' case, I've composed a map() function which does what I want. Is this something that already exists in Groovy? -- Jon -- Before -- Map getAttributes(GroovyMBean mbean) { GroovyMBean mbean = getMBean('java.lang:type=Memory') def heapUsage = mbean.HeapMemoryUsage Map res = [:] heapUsage.compositeType.keySet().each { keyName -> res[keyName] = heapUsage.get(keyName) } res } -- After -- Map map(Collection vals, Closure c) { Map res = [:] vals.each { res[it] = c(it) } res } Map getHeapUsage() { GroovyMBean mbean = getMBean('java.lang:type=Memory') def heapUsage = mbean.HeapMemoryUsage map(heapUsage.compositeType.keySet()) { heapUsage.get(it) } } --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
|
Re: Simpler way to do simple mapping?Jon Travis schrieb:
> I am missing something in my groovy idiom collection: A simpler way > to create maps out of lists. > > Essentially, I have some source object 'X' and it has 2 methods, > getKeys() and getValue(key). It should be easy to > compose a map of keys onto values, but I have not yet found a good way to do it. > > The problem with .groupBy is that the values it returns are lists, not > single objects. > > I need: map([1, 2, 3]) { it <= 2 ? 'safe' : 'charm' } -> [1: > 'safe', 2: 'safe', 3: 'charm'] def x = [1,2,3].inject([:]) {map, it -> map[it] = it <= 2 ? 'safe' : 'charm' map } only stupid part here is the surplus looking return map in the closure, but it works bye blackdrag -- Jochen "blackdrag" Theodorou The Groovy Project Tech Lead (http://groovy.codehaus.org) http://blackdragsview.blogspot.com/ --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
|
Re: Simpler way to do simple mapping?On Wed, Oct 28, 2009 at 23:20, Jochen Theodorou <blackdrag@...> wrote:
> [...] > def x = [1,2,3].inject([:]) {map, it -> > map[it] = it <= 2 ? 'safe' : 'charm' > map > } > > only stupid part here is the surplus looking return map in the closure, but > it works There's this variant too: def x = [1,2,3].inject([:]) { map, it -> [*:map, (it): it <= 2 ? 'safe' : 'charm'] } assert x == [1: 'safe', 2: 'safe', 3: 'charm'] That's without that additional return of the map. But this version, although perhaps more elegant, is probably not very good performance-wise (especially for big collections), as there's a lot of map creations. -- Guillaume Laforge Groovy Project Manager Head of Groovy Development at SpringSource http://www.springsource.com/g2one --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
|
Re: Simpler way to do simple mapping?I've been kicking around a form of inject which assumes the item isn't changed.
~~ Robert Fischer, Smokejumper IT Consulting. Enfranchised Mind Blog http://EnfranchisedMind.com/blog Grails Expert Retainer Services http://smokejumperit.com/grails-retainer/ Guillaume Laforge wrote: > On Wed, Oct 28, 2009 at 23:20, Jochen Theodorou <blackdrag@...> wrote: >> [...] >> def x = [1,2,3].inject([:]) {map, it -> >> map[it] = it <= 2 ? 'safe' : 'charm' >> map >> } >> >> only stupid part here is the surplus looking return map in the closure, but >> it works > > There's this variant too: > > def x = [1,2,3].inject([:]) { map, it -> > [*:map, (it): it <= 2 ? 'safe' : 'charm'] > } > > assert x == [1: 'safe', 2: 'safe', 3: 'charm'] > > That's without that additional return of the map. > But this version, although perhaps more elegant, is probably not very > good performance-wise (especially for big collections), as there's a > lot of map creations. > --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
|
Re: Simpler way to do simple mapping?Robert Fischer schrieb:
> I've been kicking around a form of inject which assumes the item isn't > changed. well... besides the initial value we could also give in an optional boolean telling if the return value of the closure should be used or if the object is modified directly. In terms of functional programming the creation of a new map is of course just fine. bye blackdrag -- Jochen "blackdrag" Theodorou The Groovy Project Tech Lead (http://groovy.codehaus.org) http://blackdragsview.blogspot.com/ --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
|
Call a Groovy script from another scriptHi Folks,
Is there a simple way to call a script from another script? I had a search on the web but couldn't really find anything other than (from memory) creating a new ShellScript and passing in a File object to run. The web page was complaining that it should be a lot easier than this but the blog was about five years old! So I was wondering if anything had changed.... Thanks in advance, J --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
|
|
|
|
Re: Simpler way to do simple mapping?Doesn't sound like anyone is on board with adding map() to the gdk.
It's readable, and I know I stole the method/semantics from another language, just can't place it now. -- Jon On Thu, Oct 29, 2009 at 7:36 AM, Jochen Theodorou <blackdrag@...> wrote: > Robert Fischer schrieb: >> >> I've been kicking around a form of inject which assumes the item isn't >> changed. > > well... besides the initial value we could also give in an optional boolean > telling if the return value of the closure should be used or if the object > is modified directly. In terms of functional programming the creation of a > new map is of course just fine. > > bye blackdrag > > -- > Jochen "blackdrag" Theodorou > The Groovy Project Tech Lead (http://groovy.codehaus.org) > http://blackdragsview.blogspot.com/ > > > --------------------------------------------------------------------- > To unsubscribe from this list, please visit: > > http://xircles.codehaus.org/manage_email > > > --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
|
Re: Simpler way to do simple mapping?In most functional languages, "map" means what Groovy means by "collect".
We want some kind of function which is kinda like each, kinda like inject. This kind of function doesn't really make sense in a pure functional programming context, because it's only useful with side-effects. Don't have a good name for it, so let's call it "foo". It'd be implemented something like this: Collection.foo = { item, Closure body -> delegate.each { body.delegate = it body(item) } item } ~~ Robert Fischer, Smokejumper IT Consulting. Enfranchised Mind Blog http://EnfranchisedMind.com/blog Grails Expert Retainer Services http://smokejumperit.com/grails-retainer/ Jon Travis wrote: > Doesn't sound like anyone is on board with adding map() to the gdk. > It's readable, and I know I stole the method/semantics from another > language, just can't place it now. > > -- Jon > > > On Thu, Oct 29, 2009 at 7:36 AM, Jochen Theodorou <blackdrag@...> wrote: >> Robert Fischer schrieb: >>> I've been kicking around a form of inject which assumes the item isn't >>> changed. >> well... besides the initial value we could also give in an optional boolean >> telling if the return value of the closure should be used or if the object >> is modified directly. In terms of functional programming the creation of a >> new map is of course just fine. >> >> bye blackdrag >> >> -- >> Jochen "blackdrag" Theodorou >> The Groovy Project Tech Lead (http://groovy.codehaus.org) >> http://blackdragsview.blogspot.com/ >> >> >> --------------------------------------------------------------------- >> To unsubscribe from this list, please visit: >> >> http://xircles.codehaus.org/manage_email >> >> >> > > --------------------------------------------------------------------- > To unsubscribe from this list, please visit: > > http://xircles.codehaus.org/manage_email > > > --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
| Free embeddable forum powered by Nabble | Forum Help |