You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by Guy Matz <gu...@gmail.com> on 2016/05/16 15:28:25 UTC
re-using a comparison closure
Hi!
I have to sort a list of strings based on a number within the string . . .
I am able to sort using something like:
list.sort( { a,b -> getVersion(a) <=> getVersion(b)})
I need to use this in a bunch of places in my code and was hoping to
replace it with a method, like:
list.sort( compareVersions)
with compareVersions:
def compareVersions(a, b) {
return getVersion(a).toInteger() <=> getVersion(b).toInteger()
}
putting the method (compoareVersions) into the sort as a param doesn't
work. Anyone know what I'm missing?
Thanks!!
Guy
Re: re-using a comparison closure
Posted by Sergei Egorov <bs...@gmail.com>.
Hi,
You can create "sortByVersions" extension method as well:
http://mrhaki.blogspot.com.ee/2013/01/groovy-goodness-adding-extra-methods.html
On Mon, May 16, 2016 at 6:44 PM Søren Berg Glasius (GR8Conf EU) <
sbglasius@gr8conf.org> wrote:
> Hi
>
> You can define it in a class as a static closure
>
>
> class Sorters {
> static compareVersions = { a,b ->
> return getVersion(a).toInteger() <=> getVersion(b).toInteger()
> }
> }
>
> and use it like:
>
> list.sort(Sorters.compareVersions)
>
> Best regards,
> Søren Berg Glasius
> GR8Conf Europe organizing team
>
> GR8Conf ApS
> Mobile: +45 40 44 91 88, Web: www.gr8conf.eu, Skype: sbglasius
> Company Address: Buchwaldsgade 50, 5000 Odense C, Denmark
> Personal Address: Hedevej 1, Gl. Rye, 8680 Ry, Denmark
> --- GR8Conf - Dedicated to the Groovy Ecosystem
>
> From: Guy Matz <gu...@gmail.com> <gu...@gmail.com>
> Reply: users@groovy.apache.org <us...@groovy.apache.org>
> <us...@groovy.apache.org>
> Date: May 16, 2016 at 17:42:21
> To: users@groovy.apache.org <us...@groovy.apache.org>
> <us...@groovy.apache.org>
> Subject: Re: re-using a comparison closure
>
> Thanks! Now, I have a number of methods that need access to that closure
> . . . Can I make the closure global? Is there a better way?
>
> Thanks again,
> Guy
>
> On Mon, May 16, 2016 at 11:30 AM, Søren Berg Glasius (GR8Conf EU) <
> sbglasius@gr8conf.org> wrote:
>
>> Hi Guy
>>
>> Just assign the variable
>>
>> def comapreVersions = { a,b ->
>> return getVersion(a).toInteger() <=> getVersion(b).toInteger()
>> }
>>
>> and then use it in your sort:
>>
>>
>> list.sort(compareVersions)
>>
>>
>>
>> Best regards,
>> Søren Berg Glasius
>> GR8Conf Europe organizing team
>>
>> GR8Conf ApS
>> Mobile: +45 40 44 91 88, Web: www.gr8conf.eu, Skype: sbglasius
>> Company Address: Buchwaldsgade 50, 5000 Odense C, Denmark
>> Personal Address: Hedevej 1, Gl. Rye, 8680 Ry, Denmark
>> --- GR8Conf - Dedicated to the Groovy Ecosystem
>>
>> From: Guy Matz <gu...@gmail.com> <gu...@gmail.com>
>> Reply: users@groovy.apache.org <us...@groovy.apache.org>
>> <us...@groovy.apache.org>
>> Date: May 16, 2016 at 17:28:34
>> To: users@groovy.apache.org <us...@groovy.apache.org>
>> <us...@groovy.apache.org>
>> Subject: re-using a comparison closure
>>
>> Hi!
>> I have to sort a list of strings based on a number within the string . .
>> . I am able to sort using something like:
>> list.sort( { a,b -> getVersion(a) <=> getVersion(b)})
>>
>> I need to use this in a bunch of places in my code and was hoping to
>> replace it with a method, like:
>> list.sort( compareVersions)
>>
>> with compareVersions:
>> def compareVersions(a, b) {
>> return getVersion(a).toInteger() <=> getVersion(b).toInteger()
>> }
>>
>> putting the method (compoareVersions) into the sort as a param doesn't
>> work. Anyone know what I'm missing?
>>
>> Thanks!!
>> Guy
>>
>>
>
Re: re-using a comparison closure
Posted by Guy Matz <gu...@gmail.com>.
Grumble. Still doesn't work, but it may be because I'm working within a
DSL (Artifactory)
Thanks!!!
On Mon, May 16, 2016 at 12:01 PM, Sergei Egorov <bs...@gmail.com> wrote:
> Make it static, i.e. append "static" before "def"
>
> On Mon, May 16, 2016 at 6:57 PM Guy Matz <gu...@gmail.com> wrote:
>
>> Thanks! Not working, though. Getting:
>> No such property: compareVersions for class: Sorters
>>
>> with:
>>
>> class Sorters {
>> def compareVersions = { afile, bfile ->
>> return getVersion(afile).toInteger() <=> getVersion(bfile).toInteger()
>> }
>> }
>>
>> and called with:
>>
>> res.sort(Sorters.compareVersions)
>>
>>
>> Any thoughts are appreciated! Thanks!!
>>
>>
>>
>> On Mon, May 16, 2016 at 11:43 AM, Søren Berg Glasius (GR8Conf EU) <
>> sbglasius@gr8conf.org> wrote:
>>
>>> Hi
>>>
>>> You can define it in a class as a static closure
>>>
>>>
>>> class Sorters {
>>> static compareVersions = { a,b ->
>>> return getVersion(a).toInteger() <=> getVersion(b).toInteger()
>>> }
>>> }
>>>
>>> and use it like:
>>>
>>> list.sort(Sorters.compareVersions)
>>>
>>> Best regards,
>>> Søren Berg Glasius
>>> GR8Conf Europe organizing team
>>>
>>> GR8Conf ApS
>>> Mobile: +45 40 44 91 88, Web: www.gr8conf.eu, Skype: sbglasius
>>> Company Address: Buchwaldsgade 50, 5000 Odense C, Denmark
>>> Personal Address: Hedevej 1, Gl. Rye, 8680 Ry, Denmark
>>> --- GR8Conf - Dedicated to the Groovy Ecosystem
>>>
>>> From: Guy Matz <gu...@gmail.com> <gu...@gmail.com>
>>> Reply: users@groovy.apache.org <us...@groovy.apache.org>
>>> <us...@groovy.apache.org>
>>> Date: May 16, 2016 at 17:42:21
>>> To: users@groovy.apache.org <us...@groovy.apache.org>
>>> <us...@groovy.apache.org>
>>> Subject: Re: re-using a comparison closure
>>>
>>> Thanks! Now, I have a number of methods that need access to that
>>> closure . . . Can I make the closure global? Is there a better way?
>>>
>>> Thanks again,
>>> Guy
>>>
>>> On Mon, May 16, 2016 at 11:30 AM, Søren Berg Glasius (GR8Conf EU) <
>>> sbglasius@gr8conf.org> wrote:
>>>
>>>> Hi Guy
>>>>
>>>> Just assign the variable
>>>>
>>>> def comapreVersions = { a,b ->
>>>> return getVersion(a).toInteger() <=> getVersion(b).toInteger()
>>>> }
>>>>
>>>> and then use it in your sort:
>>>>
>>>>
>>>> list.sort(compareVersions)
>>>>
>>>>
>>>>
>>>> Best regards,
>>>> Søren Berg Glasius
>>>> GR8Conf Europe organizing team
>>>>
>>>> GR8Conf ApS
>>>> Mobile: +45 40 44 91 88, Web: www.gr8conf.eu, Skype: sbglasius
>>>> Company Address: Buchwaldsgade 50, 5000 Odense C, Denmark
>>>> Personal Address: Hedevej 1, Gl. Rye, 8680 Ry, Denmark
>>>> --- GR8Conf - Dedicated to the Groovy Ecosystem
>>>>
>>>> From: Guy Matz <gu...@gmail.com> <gu...@gmail.com>
>>>> Reply: users@groovy.apache.org <us...@groovy.apache.org>
>>>> <us...@groovy.apache.org>
>>>> Date: May 16, 2016 at 17:28:34
>>>> To: users@groovy.apache.org <us...@groovy.apache.org>
>>>> <us...@groovy.apache.org>
>>>> Subject: re-using a comparison closure
>>>>
>>>> Hi!
>>>> I have to sort a list of strings based on a number within the string .
>>>> . . I am able to sort using something like:
>>>> list.sort( { a,b -> getVersion(a) <=> getVersion(b)})
>>>>
>>>> I need to use this in a bunch of places in my code and was hoping to
>>>> replace it with a method, like:
>>>> list.sort( compareVersions)
>>>>
>>>> with compareVersions:
>>>> def compareVersions(a, b) {
>>>> return getVersion(a).toInteger() <=> getVersion(b).toInteger()
>>>> }
>>>>
>>>> putting the method (compoareVersions) into the sort as a param doesn't
>>>> work. Anyone know what I'm missing?
>>>>
>>>> Thanks!!
>>>> Guy
>>>>
>>>>
>>>
>>
Re: re-using a comparison closure
Posted by Sergei Egorov <bs...@gmail.com>.
Make it static, i.e. append "static" before "def"
On Mon, May 16, 2016 at 6:57 PM Guy Matz <gu...@gmail.com> wrote:
> Thanks! Not working, though. Getting:
> No such property: compareVersions for class: Sorters
>
> with:
>
> class Sorters {
> def compareVersions = { afile, bfile ->
> return getVersion(afile).toInteger() <=> getVersion(bfile).toInteger()
> }
> }
>
> and called with:
>
> res.sort(Sorters.compareVersions)
>
>
> Any thoughts are appreciated! Thanks!!
>
>
>
> On Mon, May 16, 2016 at 11:43 AM, Søren Berg Glasius (GR8Conf EU) <
> sbglasius@gr8conf.org> wrote:
>
>> Hi
>>
>> You can define it in a class as a static closure
>>
>>
>> class Sorters {
>> static compareVersions = { a,b ->
>> return getVersion(a).toInteger() <=> getVersion(b).toInteger()
>> }
>> }
>>
>> and use it like:
>>
>> list.sort(Sorters.compareVersions)
>>
>> Best regards,
>> Søren Berg Glasius
>> GR8Conf Europe organizing team
>>
>> GR8Conf ApS
>> Mobile: +45 40 44 91 88, Web: www.gr8conf.eu, Skype: sbglasius
>> Company Address: Buchwaldsgade 50, 5000 Odense C, Denmark
>> Personal Address: Hedevej 1, Gl. Rye, 8680 Ry, Denmark
>> --- GR8Conf - Dedicated to the Groovy Ecosystem
>>
>> From: Guy Matz <gu...@gmail.com> <gu...@gmail.com>
>> Reply: users@groovy.apache.org <us...@groovy.apache.org>
>> <us...@groovy.apache.org>
>> Date: May 16, 2016 at 17:42:21
>> To: users@groovy.apache.org <us...@groovy.apache.org>
>> <us...@groovy.apache.org>
>> Subject: Re: re-using a comparison closure
>>
>> Thanks! Now, I have a number of methods that need access to that closure
>> . . . Can I make the closure global? Is there a better way?
>>
>> Thanks again,
>> Guy
>>
>> On Mon, May 16, 2016 at 11:30 AM, Søren Berg Glasius (GR8Conf EU) <
>> sbglasius@gr8conf.org> wrote:
>>
>>> Hi Guy
>>>
>>> Just assign the variable
>>>
>>> def comapreVersions = { a,b ->
>>> return getVersion(a).toInteger() <=> getVersion(b).toInteger()
>>> }
>>>
>>> and then use it in your sort:
>>>
>>>
>>> list.sort(compareVersions)
>>>
>>>
>>>
>>> Best regards,
>>> Søren Berg Glasius
>>> GR8Conf Europe organizing team
>>>
>>> GR8Conf ApS
>>> Mobile: +45 40 44 91 88, Web: www.gr8conf.eu, Skype: sbglasius
>>> Company Address: Buchwaldsgade 50, 5000 Odense C, Denmark
>>> Personal Address: Hedevej 1, Gl. Rye, 8680 Ry, Denmark
>>> --- GR8Conf - Dedicated to the Groovy Ecosystem
>>>
>>> From: Guy Matz <gu...@gmail.com> <gu...@gmail.com>
>>> Reply: users@groovy.apache.org <us...@groovy.apache.org>
>>> <us...@groovy.apache.org>
>>> Date: May 16, 2016 at 17:28:34
>>> To: users@groovy.apache.org <us...@groovy.apache.org>
>>> <us...@groovy.apache.org>
>>> Subject: re-using a comparison closure
>>>
>>> Hi!
>>> I have to sort a list of strings based on a number within the string . .
>>> . I am able to sort using something like:
>>> list.sort( { a,b -> getVersion(a) <=> getVersion(b)})
>>>
>>> I need to use this in a bunch of places in my code and was hoping to
>>> replace it with a method, like:
>>> list.sort( compareVersions)
>>>
>>> with compareVersions:
>>> def compareVersions(a, b) {
>>> return getVersion(a).toInteger() <=> getVersion(b).toInteger()
>>> }
>>>
>>> putting the method (compoareVersions) into the sort as a param doesn't
>>> work. Anyone know what I'm missing?
>>>
>>> Thanks!!
>>> Guy
>>>
>>>
>>
>
Re: re-using a comparison closure
Posted by Guy Matz <gu...@gmail.com>.
Thanks! Not working, though. Getting:
No such property: compareVersions for class: Sorters
with:
class Sorters {
def compareVersions = { afile, bfile ->
return getVersion(afile).toInteger() <=> getVersion(bfile).toInteger()
}
}
and called with:
res.sort(Sorters.compareVersions)
Any thoughts are appreciated! Thanks!!
On Mon, May 16, 2016 at 11:43 AM, Søren Berg Glasius (GR8Conf EU) <
sbglasius@gr8conf.org> wrote:
> Hi
>
> You can define it in a class as a static closure
>
>
> class Sorters {
> static compareVersions = { a,b ->
> return getVersion(a).toInteger() <=> getVersion(b).toInteger()
> }
> }
>
> and use it like:
>
> list.sort(Sorters.compareVersions)
>
> Best regards,
> Søren Berg Glasius
> GR8Conf Europe organizing team
>
> GR8Conf ApS
> Mobile: +45 40 44 91 88, Web: www.gr8conf.eu, Skype: sbglasius
> Company Address: Buchwaldsgade 50, 5000 Odense C, Denmark
> Personal Address: Hedevej 1, Gl. Rye, 8680 Ry, Denmark
> --- GR8Conf - Dedicated to the Groovy Ecosystem
>
> From: Guy Matz <gu...@gmail.com> <gu...@gmail.com>
> Reply: users@groovy.apache.org <us...@groovy.apache.org>
> <us...@groovy.apache.org>
> Date: May 16, 2016 at 17:42:21
> To: users@groovy.apache.org <us...@groovy.apache.org>
> <us...@groovy.apache.org>
> Subject: Re: re-using a comparison closure
>
> Thanks! Now, I have a number of methods that need access to that closure
> . . . Can I make the closure global? Is there a better way?
>
> Thanks again,
> Guy
>
> On Mon, May 16, 2016 at 11:30 AM, Søren Berg Glasius (GR8Conf EU) <
> sbglasius@gr8conf.org> wrote:
>
>> Hi Guy
>>
>> Just assign the variable
>>
>> def comapreVersions = { a,b ->
>> return getVersion(a).toInteger() <=> getVersion(b).toInteger()
>> }
>>
>> and then use it in your sort:
>>
>>
>> list.sort(compareVersions)
>>
>>
>>
>> Best regards,
>> Søren Berg Glasius
>> GR8Conf Europe organizing team
>>
>> GR8Conf ApS
>> Mobile: +45 40 44 91 88, Web: www.gr8conf.eu, Skype: sbglasius
>> Company Address: Buchwaldsgade 50, 5000 Odense C, Denmark
>> Personal Address: Hedevej 1, Gl. Rye, 8680 Ry, Denmark
>> --- GR8Conf - Dedicated to the Groovy Ecosystem
>>
>> From: Guy Matz <gu...@gmail.com> <gu...@gmail.com>
>> Reply: users@groovy.apache.org <us...@groovy.apache.org>
>> <us...@groovy.apache.org>
>> Date: May 16, 2016 at 17:28:34
>> To: users@groovy.apache.org <us...@groovy.apache.org>
>> <us...@groovy.apache.org>
>> Subject: re-using a comparison closure
>>
>> Hi!
>> I have to sort a list of strings based on a number within the string . .
>> . I am able to sort using something like:
>> list.sort( { a,b -> getVersion(a) <=> getVersion(b)})
>>
>> I need to use this in a bunch of places in my code and was hoping to
>> replace it with a method, like:
>> list.sort( compareVersions)
>>
>> with compareVersions:
>> def compareVersions(a, b) {
>> return getVersion(a).toInteger() <=> getVersion(b).toInteger()
>> }
>>
>> putting the method (compoareVersions) into the sort as a param doesn't
>> work. Anyone know what I'm missing?
>>
>> Thanks!!
>> Guy
>>
>>
>
Re: re-using a comparison closure
Posted by "Søren Berg Glasius (GR8Conf EU)" <sb...@gr8conf.org>.
Hi
You can define it in a class as a static closure
class Sorters {
static compareVersions = { a,b ->
return getVersion(a).toInteger() <=> getVersion(b).toInteger()
}
}
and use it like:
list.sort(Sorters.compareVersions)
Best regards,
Søren Berg Glasius
GR8Conf Europe organizing team
GR8Conf ApS
Mobile: +45 40 44 91 88, Web: www.gr8conf.eu, Skype: sbglasius
Company Address: Buchwaldsgade 50, 5000 Odense C, Denmark
Personal Address: Hedevej 1, Gl. Rye, 8680 Ry, Denmark
--- GR8Conf - Dedicated to the Groovy Ecosystem
From: Guy Matz <gu...@gmail.com>
Reply: users@groovy.apache.org <us...@groovy.apache.org>
Date: May 16, 2016 at 17:42:21
To: users@groovy.apache.org <us...@groovy.apache.org>
Subject: Re: re-using a comparison closure
Thanks! Now, I have a number of methods that need access to that closure . . . Can I make the closure global? Is there a better way?
Thanks again,
Guy
On Mon, May 16, 2016 at 11:30 AM, Søren Berg Glasius (GR8Conf EU) <sb...@gr8conf.org> wrote:
Hi Guy
Just assign the variable
def comapreVersions = { a,b ->
return getVersion(a).toInteger() <=> getVersion(b).toInteger()
}
and then use it in your sort:
list.sort(compareVersions)
Best regards,
Søren Berg Glasius
GR8Conf Europe organizing team
GR8Conf ApS
Mobile: +45 40 44 91 88, Web: www.gr8conf.eu, Skype: sbglasius
Company Address: Buchwaldsgade 50, 5000 Odense C, Denmark
Personal Address: Hedevej 1, Gl. Rye, 8680 Ry, Denmark
--- GR8Conf - Dedicated to the Groovy Ecosystem
From: Guy Matz <gu...@gmail.com>
Reply: users@groovy.apache.org <us...@groovy.apache.org>
Date: May 16, 2016 at 17:28:34
To: users@groovy.apache.org <us...@groovy.apache.org>
Subject: re-using a comparison closure
Hi!
I have to sort a list of strings based on a number within the string . . . I am able to sort using something like:
list.sort( { a,b -> getVersion(a) <=> getVersion(b)})
I need to use this in a bunch of places in my code and was hoping to replace it with a method, like:
list.sort( compareVersions)
with compareVersions:
def compareVersions(a, b) {
return getVersion(a).toInteger() <=> getVersion(b).toInteger()
}
putting the method (compoareVersions) into the sort as a param doesn't work. Anyone know what I'm missing?
Thanks!!
Guy
Re: re-using a comparison closure
Posted by Guy Matz <gu...@gmail.com>.
Thanks! Now, I have a number of methods that need access to that closure .
. . Can I make the closure global? Is there a better way?
Thanks again,
Guy
On Mon, May 16, 2016 at 11:30 AM, Søren Berg Glasius (GR8Conf EU) <
sbglasius@gr8conf.org> wrote:
> Hi Guy
>
> Just assign the variable
>
> def comapreVersions = { a,b ->
> return getVersion(a).toInteger() <=> getVersion(b).toInteger()
> }
>
> and then use it in your sort:
>
>
> list.sort(compareVersions)
>
>
>
> Best regards,
> Søren Berg Glasius
> GR8Conf Europe organizing team
>
> GR8Conf ApS
> Mobile: +45 40 44 91 88, Web: www.gr8conf.eu, Skype: sbglasius
> Company Address: Buchwaldsgade 50, 5000 Odense C, Denmark
> Personal Address: Hedevej 1, Gl. Rye, 8680 Ry, Denmark
> --- GR8Conf - Dedicated to the Groovy Ecosystem
>
> From: Guy Matz <gu...@gmail.com> <gu...@gmail.com>
> Reply: users@groovy.apache.org <us...@groovy.apache.org>
> <us...@groovy.apache.org>
> Date: May 16, 2016 at 17:28:34
> To: users@groovy.apache.org <us...@groovy.apache.org>
> <us...@groovy.apache.org>
> Subject: re-using a comparison closure
>
> Hi!
> I have to sort a list of strings based on a number within the string . .
> . I am able to sort using something like:
> list.sort( { a,b -> getVersion(a) <=> getVersion(b)})
>
> I need to use this in a bunch of places in my code and was hoping to
> replace it with a method, like:
> list.sort( compareVersions)
>
> with compareVersions:
> def compareVersions(a, b) {
> return getVersion(a).toInteger() <=> getVersion(b).toInteger()
> }
>
> putting the method (compoareVersions) into the sort as a param doesn't
> work. Anyone know what I'm missing?
>
> Thanks!!
> Guy
>
>
Re: re-using a comparison closure
Posted by "Søren Berg Glasius (GR8Conf EU)" <sb...@gr8conf.org>.
Hi Guy
Just assign the variable
def comapreVersions = { a,b ->
return getVersion(a).toInteger() <=> getVersion(b).toInteger()
}
and then use it in your sort:
list.sort(compareVersions)
Best regards,
Søren Berg Glasius
GR8Conf Europe organizing team
GR8Conf ApS
Mobile: +45 40 44 91 88, Web: www.gr8conf.eu, Skype: sbglasius
Company Address: Buchwaldsgade 50, 5000 Odense C, Denmark
Personal Address: Hedevej 1, Gl. Rye, 8680 Ry, Denmark
--- GR8Conf - Dedicated to the Groovy Ecosystem
From: Guy Matz <gu...@gmail.com>
Reply: users@groovy.apache.org <us...@groovy.apache.org>
Date: May 16, 2016 at 17:28:34
To: users@groovy.apache.org <us...@groovy.apache.org>
Subject: re-using a comparison closure
Hi!
I have to sort a list of strings based on a number within the string . . . I am able to sort using something like:
list.sort( { a,b -> getVersion(a) <=> getVersion(b)})
I need to use this in a bunch of places in my code and was hoping to replace it with a method, like:
list.sort( compareVersions)
with compareVersions:
def compareVersions(a, b) {
return getVersion(a).toInteger() <=> getVersion(b).toInteger()
}
putting the method (compoareVersions) into the sort as a param doesn't work. Anyone know what I'm missing?
Thanks!!
Guy