Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calling .each{} on IntRange returns the range not each integer

I think I've got some funny expectations... I want to iterate the numbers from 1 to 10. As a while loop it goes like this:

def countMe = 1 while (countMe<11) {   println countMe   countMe++ } 

I was expecting that the following would do this also:

[1..10].each { println it } 

But it actually prints the IntRange, not each Integer in the range. What is the (syntactically) closest way to my [x..y].each{} fantasy to get each of a list of numbers?

like image 431
Mikey Avatar asked Sep 15 '12 00:09

Mikey


People also ask

How do you find the range of an integer in Java?

ValueRange. of(minValue, maxValue); range. isValidIntValue(x); it returns true if minValue <= x <= MaxValue - i.e. within the range.

How do you check if a number is in a range Kotlin?

You can also use Kotlin range m..n in a conditional statement like if-statement. Using in keyword, you can check if a value is in the given range.


1 Answers

Use parentheses not brackets:

(1..10).each{println it} 

[1..10] is a list of length 1 containing a single range.

like image 124
Eric Avatar answered Oct 13 '22 04:10

Eric