GDK Collection.remove() ?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

GDK Collection.remove() ?

Edward Povazan
Hello,

Is there a Groovy way to remove items from a collection based on the result of a
closure call? I've looked in the DGM source, and haven't found one.

Here is a little demo based implemented as a category.

class CollectionCategory {
        static void remove(Collection collection, Closure clos) {
                def iter = collection.iterator()
                while (iter.hasNext()) {
                        def item = iter.next()
                        if (clos.call(item) == true) {
                                iter.remove()
                                break;
                        }
                }
        }
       
        static void removeAll(Collection collection, Closure clos) {
                def iter = collection.iterator()
                while (iter.hasNext()) {
                        def item = iter.next()
                        if (clos.call(item) == true)
                                iter.remove()
                }
        }
}

use(CollectionCategory) {
   def l = [1, 2, 3, 4]
   l.remove { (it == 2 || it == 4) }
   println l
   l = [1, 2, 3, 4, 3, 3, 5]
   l.removeAll { it == 3 }
   println l
}
 > [1, 3, 4]
 > [1, 2, 4, 5]


I know I can use findAll() but I don't want to needlessly create a new list. And
of course I could write the code seen in the category, but that is very UnGroovy :)
This would be useful for all collections. Perhaps a removeFirst() method? Or
remove() and removeAll() to parallel find() and findAll().

-Ed
Reply | Threaded
Open this post in threaded view
|

Re: GDK Collection.remove() ?

Guillaume Laforge
Administrator
There's a potential patch for a removeIf() method:

http://jira.codehaus.org/browse/GROOVY-867

Is it what you're looking for?

On 3/31/06, Edward Povazan <[hidden email]> wrote:

> Hello,
>
> Is there a Groovy way to remove items from a collection based on the result of a
> closure call? I've looked in the DGM source, and haven't found one.
>
> Here is a little demo based implemented as a category.
>
> class CollectionCategory {
>         static void remove(Collection collection, Closure clos) {
>                 def iter = collection.iterator()
>                 while (iter.hasNext()) {
>                         def item = iter.next()
>                         if (clos.call(item) == true) {
>                                 iter.remove()
>                                 break;
>                         }
>                 }
>         }
>
>         static void removeAll(Collection collection, Closure clos) {
>                 def iter = collection.iterator()
>                 while (iter.hasNext()) {
>                         def item = iter.next()
>                         if (clos.call(item) == true)
>                                 iter.remove()
>                 }
>         }
> }
>
> use(CollectionCategory) {
>    def l = [1, 2, 3, 4]
>    l.remove { (it == 2 || it == 4) }
>    println l
>    l = [1, 2, 3, 4, 3, 3, 5]
>    l.removeAll { it == 3 }
>    println l
> }
>  > [1, 3, 4]
>  > [1, 2, 4, 5]
>
>
> I know I can use findAll() but I don't want to needlessly create a new list. And
> of course I could write the code seen in the category, but that is very UnGroovy :)
> This would be useful for all collections. Perhaps a removeFirst() method? Or
> remove() and removeAll() to parallel find() and findAll().
>
> -Ed
>


--
Guillaume Laforge
Groovy Project Manager
http://glaforge.free.fr/blog/groovy
Reply | Threaded
Open this post in threaded view
|

Re: GDK Collection.remove() ?

Edward Povazan
Yes, thanks. I guess I will wait for RC1, and use my CollectionCategory for now
(categories rock!)
-Ed

Guillaume Laforge wrote:

> There's a potential patch for a removeIf() method:
>
> http://jira.codehaus.org/browse/GROOVY-867
>
> Is it what you're looking for?
>
> On 3/31/06, Edward Povazan <[hidden email]> wrote:
>> Hello,
>>
>> Is there a Groovy way to remove items from a collection based on the result of a
>> closure call? I've looked in the DGM source, and haven't found one.
>>
>> Here is a little demo based implemented as a category.
>>
>> class CollectionCategory {
>>         static void remove(Collection collection, Closure clos) {
>>                 def iter = collection.iterator()
>>                 while (iter.hasNext()) {
>>                         def item = iter.next()
>>                         if (clos.call(item) == true) {
>>                                 iter.remove()
>>                                 break;
>>                         }
>>                 }
>>         }
>>
>>         static void removeAll(Collection collection, Closure clos) {
>>                 def iter = collection.iterator()
>>                 while (iter.hasNext()) {
>>                         def item = iter.next()
>>                         if (clos.call(item) == true)
>>                                 iter.remove()
>>                 }
>>         }
>> }
>>
>> use(CollectionCategory) {
>>    def l = [1, 2, 3, 4]
>>    l.remove { (it == 2 || it == 4) }
>>    println l
>>    l = [1, 2, 3, 4, 3, 3, 5]
>>    l.removeAll { it == 3 }
>>    println l
>> }
>>  > [1, 3, 4]
>>  > [1, 2, 4, 5]
>>
>>
>> I know I can use findAll() but I don't want to needlessly create a new list. And
>> of course I could write the code seen in the category, but that is very UnGroovy :)
>> This would be useful for all collections. Perhaps a removeFirst() method? Or
>> remove() and removeAll() to parallel find() and findAll().
>>
>> -Ed
>>
>
>
> --
> Guillaume Laforge
> Groovy Project Manager
> http://glaforge.free.fr/blog/groovy
>