Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6798579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:44:50+00:00 2026-05-26T18:44:50+00:00

In another question, a user suggested to write code like to that: def list

  • 0

In another question, a user suggested to write code like to that:

def list = ['a', 'b', 'c', 'd']
def i = 0; 
assert list.collect { [i++] } == [0, 1, 2, 3]

Such code is, in other languages, considered bad practice because the content of collect changes the state of it’s context (here it changes the value of i). In other words, the closure has side-effects.

Such higher order functions should be able to run the closure in parallel, and assemble it in a new list again. If the processing in the closure are long, CPU intensive operations, it may be worth executing them in separate threads. It would be easy to change collect to use an ExecutorCompletionService to achieve that, but it would break the above code.

Another example of a problem is if, for some reason, collect browse the collection in, say, reverse order, in which case the result would be [3, 2, 1, 0]. Note that in this case, the list have not been reverted, 0 is really the result of applying the closure to ‘d’!

Interestingly, these functions are documented with “Iterates through this collection” in Collection’s JavaDoc, which suggests the iteration is sequential.

Does the groovy specification explicitly defines the order of execution in higher order functions like collect or each? Is the above code broken, or is it OK?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:44:50+00:00Added an answer on May 26, 2026 at 6:44 pm

    I don’t like explicit external variables being relied upon in my closures for the reasons you give above.

    Indeed, the less variables I have to define, the happier I am 😉

    For the possibly parallel things as well, always code with a view to wrapping it with some level of GPars loveliness should it prove too much for a single thread to handle. For this, as you say, you want as little mutability as possible and to try and completely avoid side-effects (such as the external counter pattern above)

    As for the question itself, if we take collect as an example function, and examine the source code, we can see that given an Object (Collection and Map are done in a similar way with slight differences as to how the Iterator is referenced) it iterates along InvokerHelper.asIterator(self), adding the result of each closure call to the resultant list.

    InvokerHelper.asIterator (again source is here) basically calls the iterator() method on the Object passed in.

    So for Lists, etc it will iterate down the objects in the order defined by the iterator.

    It is therefore possible to compose your own class which follows the Iterable interface design (doesn’t need to implement Iterable though, thanks to duck-typing), and define how the collection will be iterated.

    I think by asking about the Groovy specification though, this answer might not be what you want, but I don’t think there is an answer. Groovy has never really had a ‘complete’ specification (indeed this is point about groovy that some people dislike).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In another question , a user pointed out that the new keyword was dangerous
Another question related to this one . I have a List<SortableObjects> that is the
In another question on SO I answered with code like the one below and
As suggested by a user time ago in another question, I need to inject
This came up when answering another user's question (TheSoftwareJedi)... Given the following table: ROW_PRIORITY
In another question it was recommend that I try resetting a remote tracking branch
Another random question that hit me (I've drank ~9 cups of coffee in the
I tried to implement a suggestion that came up in another question: Stackoverflow question
I posted a question about how to get user input such as YES or
Quick question. Just want to confirm that a network call such as to a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.