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

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Why and How To Integrate Elastic APM in Apache JMeter
  • Transitioning From Groovy to Kotlin for Gradle Android Projects
  • Streamlining Your Workflow With the Jenkins HTTP Request Plugin: A Guide to Replacing CURL in Scripts
  • Why "Polyglot Programming" or "Do It Yourself Programming Languages" or "Language Oriented Programming" sucks?

Trending

  • The Human Side of Logs: What Unstructured Data Is Trying to Tell You
  • Hybrid Cloud vs Multi-Cloud: Choosing the Right Strategy for AI Scalability and Security
  • Apache Doris vs Elasticsearch: An In-Depth Comparative Analysis
  • The Cypress Edge: Next-Level Testing Strategies for React Developers
  1. DZone
  2. Coding
  3. Languages
  4. Groovy Goodness: Use Range by Method to Set Steps between Numbers

Groovy Goodness: Use Range by Method to Set Steps between Numbers

Let's take a look at how to use Groovy's ''by'' method when it comes to number ranges and setting the step size used.

By 
Hubert Klein Ikkink user avatar
Hubert Klein Ikkink
·
Jun. 11, 18 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
10.3K Views

Join the DZone community and get the full member experience.

Join For Free

Groovy has support for defining ranges in the language. When we define a range of numbers, the steps between the values in the range is 1 by default. We can change the step size using the step method. This method accepts a int value with a new step size. The result is a List object with the values. Since Groovy 2.5.0 the by method is added to ranges with numbers. The by method accepts also decimal numbers and the result of the method is a NumberRange object instead of a List.

In the following example Groovy script, we first define a range with int values. We use the by method to change the step size using both an int value and BigDecimal value. We also use the by method for a range of BigDecimal numbers:

// Define range with int values.
def ints = 1..10

assert ints.from == 1
assert ints.to == 10
assert ints == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
assert ints.class.name == 'groovy.lang.IntRange'

// Use by method to set steps
// between int values in range.
// The results is a new range.
def intsBy = ints.by(3)

assert intsBy.from == 1
assert intsBy.to == 10
assert intsBy == [1, 4, 7, 10]
assert intsBy.class.name == 'groovy.lang.NumberRange'

// Use step method to set steps
// between int values in range.
// The range is now converted to a List.
def intsStep = ints.step(3)

assert intsStep == [1, 4, 7, 10]
assert intsStep.class.name == 'java.util.ArrayList'

// Use by method to set step size to 0.9.
def intsBy2 = ints.by(0.9)

assert intsBy2.from == 1
assert intsBy2.to == 10
assert intsBy2 == [1, 1.9, 2.8, 3.7, 4.6, 5.5, 6.4, 7.3, 8.2, 9.1, 10.0]
assert intsBy2.class.name == 'groovy.lang.NumberRange'


// Define range with BigDecimal numbers.
def numbers = 1.0..4.5

assert numbers.from == 1.0
assert numbers.to == 4.5
assert numbers == [1.0, 2.0, 3.0, 4.0]
assert numbers.class.name == 'groovy.lang.NumberRange'

// Use by method to set step size
// between numbers to 0.5.
def numbersBy = numbers.by(0.5)

assert numbersBy.from == 1.0
assert numbersBy.to == 4.5
assert numbersBy == [1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5]
assert numbersBy.class.name == 'groovy.lang.NumberRange'

// We cannot use the step method to set
// step size to 0.5,
// because the step method only accepts
// int values as argument.
try {
    numbers.step(0.5)
} catch (MissingMethodException e) {
    assert e.message.contains('Possible solutions: step(int), step(int, groovy.lang.Closure)')
}


Written with Groovy 2.5.0.

Groovy (programming language)

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

Opinions expressed by DZone contributors are their own.

Related

  • Why and How To Integrate Elastic APM in Apache JMeter
  • Transitioning From Groovy to Kotlin for Gradle Android Projects
  • Streamlining Your Workflow With the Jenkins HTTP Request Plugin: A Guide to Replacing CURL in Scripts
  • Why "Polyglot Programming" or "Do It Yourself Programming Languages" or "Language Oriented Programming" sucks?

Partner Resources

×

Comments

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: