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

  • The Unreasonable Effectiveness of the Actor Model for Creating Agentic LLM Applications
  • 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

Trending

  • The Trust Problem in Modern SaaS: Why Your Authentication Succeeded, and You Still Got Breached
  • Introducing RAI Audit Kit: Evidence-Grade Responsible AI Audits in Python
  • Your AI Is Not Failing, Your Context Is
  • Your AI Coding Agent Can't Steal What It Never Had: The Docker Sandbox Isolation Story
  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.6K 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. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • The Unreasonable Effectiveness of the Actor Model for Creating Agentic LLM Applications
  • 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

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