Discussion:
[groovy-user] Range with no upper limit?
HamletDRC
2007-10-23 18:02:01 UTC
Permalink
Hi,

I want to define a range with no upper limit.

I looked at RangeTest.groovy and didn't see a usage for this.

A simple example might be:

def range = 5..INFINITY

so the range represents from 5 to infinity.

I'm using INTEGER.MAX_VALUE now, but this doesn't feel right.

Any suggestions?

Thanks,

--
Hamlet D'Arcy

-----
--
Hamlet D'Arcy
http://hamletdarcy.blogspot.com
--
View this message in context: http://www.nabble.com/Range-with-no-upper-limit--tf4679312.html#a13370189
Sent from the groovy - user mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email
Jim White
2007-10-23 20:28:57 UTC
Permalink
Post by HamletDRC
I want to define a range with no upper limit.
I looked at RangeTest.groovy and didn't see a usage for this.
def range = 5..INFINITY
so the range represents from 5 to infinity.
I'm using INTEGER.MAX_VALUE now, but this doesn't feel right.
Any suggestions?
Implement Range in a suitable class.

You won't have the syntactic sugar, but that shouldn't be a big issue
unless you're doing some DSL sort of thing.

And if we're after more sugar for ranges, there should be "exclude from"
as well as "exclude to". Although if that sort of thing were to be
done, syntax for "open ended" would be worthwhile too.

FROM..TO
FROM..<TO
FROM>..TO
FROM>..<TO

Since ~ is used for regex, perhaps % and -% would work for +/- inf.

Range.INFINITY and Range.NEGATIVE_INFINITY could also be defined
(although might be troublesome for parsing since they wouldn't be
defined syntactically).

And a related thing I'd like is array index syntax for the last element:

LIST[%] === LIST[LIST.size() - 1]

Without the double evaluation of LIST of course.

Should be Queue.peek() if the list implements java.util.Queue (which is
a Java 1.5 thing).

Jim


---------------------------------------------------------------------
To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email
Jim White
2007-10-23 20:38:18 UTC
Permalink
Post by Jim White
...
LIST[%] === LIST[LIST.size() - 1]
Without the double evaluation of LIST of course.
Should be Queue.peek() if the list implements java.util.Queue (which is
a Java 1.5 thing).
Similarly for Stack.peek.

And of course an array would use ARRAY.length rather than LIST.size().

The point being that the behavior is to give the same
last element that the 'for (v in LIST) ...' loop would be.

Jim


---------------------------------------------------------------------
To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Continue reading on narkive:
Loading...