DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Beyond Algorithms: The Human Element in AI-Driven Cybersecurity
  • The Unreasonable Effectiveness of the Actor Model for Creating Agentic LLM Applications
  • Migrating from React Router v5 to v6: A Comprehensive Guide
  • Playwright: Filter Visible Elements With locator.filter({ visible: true })

Trending

  • The Cross-Lingual RAG Problem Nobody Is Talking About
  • AI-Driven RAG Systems: Practical Implementation With LangChain
  • Alternative Structured Concurrency
  • The Hidden Cost of AI Tokens: Engineering Patterns for 10x Resource Efficiency
  1. DZone
  2. Coding
  3. Languages
  4. Groovy Goodness: Transform Elements While Flattening [Snippet]

Groovy Goodness: Transform Elements While Flattening [Snippet]

Learn more about elements in this Groovy snippet.

By 
Hubert Klein Ikkink user avatar
Hubert Klein Ikkink
·
Jan. 14, 20 · Code Snippet
Likes (3)
Comment
Save
Tweet
Share
8.2K Views

Join the DZone community and get the full member experience.

Join For Free

Learn more about elements in this Groovy snippet.
We can use the flatten method in Groovy to flatten a collection that contains other collections into a single collection with all elements. We can pass a closure as an extra argument to the flatten method to transform each element that is flattened. The argument of the closure is the element from the original collection.
You may also like: [DZone Refcard] Getting Started With Groovy

In the following example, we first use the flatten method without a closure argument. Then we pass a closure argument and transform the element:

Groovy




x


 
1
def list = [1, [2, 3], [[4]]]
2
 
          
3
// Simple flatten the nested collections.
4
assert list.flatten() == [1, 2, 3, 4]
5
 
          
6
// We can use a closure to transform
7
// the elements in the resulting collection.
8
assert list.flatten { it * 2 } == [2, 4, 6, 8]



Written with Groovy 2.5.7.

Further Reading

[DZone Refcard] Getting Started With Groovy

Statically Compiled Groovy: Give Groovy a Chance

Groovy (programming language) Element Snippet (programming)

Published at DZone with permission of Hubert Klein Ikkink. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Beyond Algorithms: The Human Element in AI-Driven Cybersecurity
  • The Unreasonable Effectiveness of the Actor Model for Creating Agentic LLM Applications
  • Migrating from React Router v5 to v6: A Comprehensive Guide
  • Playwright: Filter Visible Elements With locator.filter({ visible: true })

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook