Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groovy range with a 0.5 step size

What's the most elgant way in Groovy to specify a range of integers and the 0.5 steps between them? e.g.: 1, 1.5, 2, 2.5, 3, 3.5, 4

Edit: To clarify: As an end result I need a range object for use in a Grails constraint. Though I suppose a list would be OK too.

like image 215
Michael Borgwardt Avatar asked Feb 21 '09 14:02

Michael Borgwardt


2 Answers

Best way I can see is using the step command.

i.e.


    1.step(4, 0.5){ print "$it "}

would print out: "1 1.5 2.0 2.5 3.0 3.5"

like image 152
kgrad Avatar answered Nov 02 '22 06:11

kgrad


A little late, but this works too

A one-liner for your above set:

(2..8)*.div(2)

like image 29
Reverend Gonzo Avatar answered Nov 02 '22 05:11

Reverend Gonzo