开发者

Object range with conditions

开发者 https://www.devze.com 2023-03-23 10:46 出处:网络
In groovy I can write def n = 10 print 1..<n Output: [1, 2, 3, 4, 5, 6, 7, 8, 9] Are there other language that allow to specify range with conditions?

In groovy I can write

def n = 10
print 1..<n

Output: [1, 2, 3, 4, 5, 6, 7, 8, 9]

Are there other language that allow to specify range with conditions?

examples

def n = 10
print 1<=..n

Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

def n = -2
print 1<=..n

Output: [1]

def n = -开发者_运维问答2
print 1..n

Output: [1, 0, -1, -2]


Python has the range() method which does a similar thing. While it does not use operators for the condition you can specify a start value, stop value and step value. It then creates a list containing all values starting with the start value, then start+step, ... until it reaches the end value (which is not included).

0

精彩评论

暂无评论...
验证码 换一张
取 消