|
|
|
|
Re: How do I create a FOR loop that runs once? [message #879097 is a reply to message #879080] |
Wed, 30 May 2012 14:06   |
Eclipse User |
|
|
|
> :-/ I fear that creates another hole: I can't create a loop that doesn't
> loop because «FOR i: 0..-1» would run from 0 to -1 (two iterations).
>
So the topic should be "How do I create a FOR loop that doesn't loop?"
P.S.: here is one that doesn't loop:
«FOR i: Integer::MIN_VALUE..0»
|
|
|
|
|
Re: How do I create a FOR loop that runs once? [message #879287 is a reply to message #879104] |
Wed, 30 May 2012 23:13   |
Eclipse User |
|
|
|
On 2012-30-05 16:18, Christian Dietrich wrote:
> Hi,
>
> it does loop. in fac very often
>
> 1..0 doesnt loop
That looks like a bug - I would expect to iterations (for '1' and for '0').
Why not just use a function exclusive(T, T) and/or variant taking single
range as argument, where exclusive returns range that is "one less" or
an empty iterator if range is empty.
for(x : exclusive(0..0))
for(x : exclusive(0, 0))
- henrik
|
|
|
|
|
|
Re: How do I create a FOR loop that runs once? [message #879495 is a reply to message #879287] |
Thu, 31 May 2012 10:07   |
Eclipse User |
|
|
|
Hi
The equivalent x..y construct is poorly specified in OCL; it actually
involves an upward search from x to y that never terminates since OCL's
numbers are unlimited.
I was proposing to generalize as: x to y inclusive, in the direction of
y, but found that this had some bad properties when y is a computed
result such as size() that ends up smaller than expected.
If X**** still has the discretion to define this, I recommend an empty
iteration for y < x. The iteration is x up to y, so long as x <= y.
Regards
Ed Willink
On 31/05/2012 00:13, Henrik Lindberg wrote:
> On 2012-30-05 16:18, Christian Dietrich wrote:
>> Hi,
>>
>> it does loop. in fac very often
>>
>> 1..0 doesnt loop
> That looks like a bug - I would expect to iterations (for '1' and for
> '0').
>
> Why not just use a function exclusive(T, T) and/or variant taking
> single range as argument, where exclusive returns range that is "one
> less" or an empty iterator if range is empty.
>
> for(x : exclusive(0..0))
> for(x : exclusive(0, 0))
>
> - henrik
|
|
|
|
Re: How do I create a FOR loop that runs once? [message #879611 is a reply to message #879594] |
Thu, 31 May 2012 14:08   |
Eclipse User |
|
|
|
Hi
It's an almost arbitrary design choice.
In casual English, the ambiguity can be resolved as inclusive, and when
booking hotel rooms this can lead to trouble on the final day.
e.g. A decimal number is a number from 1 to 10; well actually 0 to 9
inclusive (in Java) or 1 to 10 inclusive (in OCL) or is it????
Inclusive has the almost irrelevant benefit that you can include
MAX_INTEGER in the range.
Regards
Ed Willink
On 31/05/2012 14:42, Aaron Digulla wrote:
> Can you give an example when you could need an inclusive range?
>
> My common use case is that I need an index while I iterate over a
> list. That index must go from 0 to size-1 (inclusive) or 0 <= index <
> size.
|
|
|
Re: How do I create a FOR loop that runs once? [message #880016 is a reply to message #879611] |
Fri, 01 June 2012 09:31  |
Eclipse User |
|
|
|
That design choice isn't as arbitrary as it might seem because it has consequences. Let's look at the Java String API. We want to get the sub string before the first comma:
int pos = s.indexOf( ',' )
String result = s.substring( 0, pos )
String rest = s.substring( pos )
String restWithoutComma = s.substring( pos + 1 )
If the range parameter for substring() was upper-inclusive, I'd have to write "s.substring( 0, pos-1 )".
Someone might want to fix this by returning a 1-based index from indexOf() (i.e. 0 would mean "not found" and 6 for indexOf( "Hello,", ',' ) instead of 5). That would give this code:
int pos = s.indexOf( ',' )
String result = s.substring( 0, pos )
String rest = s.substring( pos - 1 )
String restWithoutComma = s.substring( pos )
unless we make the start-index 1-based too which would make it incompatible with arrays. If someone wants to fix that: Why do you write code in Java when you'd be better off with Pascal? And don't come complaining when you realize how many 1-off errors you start to make all of a sudden.
This would also influence all such APIs like Regexp matching and range loops.
To get the first four characters would be "substring( 0, 3 )" instead of "substring( 0, 4 )".
If you still have doubts that this is a bad thing, have a look at the JDBC API which uses 1-based indices for columns. I got my "loop over all columns" loops so often wrong that I've wrote my own wrapper library to get proper 0-based indices.
Conclusion: Trying to solve this design problem by applying English language rules might seem natural but it tries to hide the underlying hardware model with an either inconsistent or clumsy design that causes problems as soon as you try to start to use it.
Proof: The Xtend developers, who seem to be pretty smart, got IntIterator wrong in Xbase 2.2.x.
|
|
|
Powered by
FUDForum. Page generated in 0.07120 seconds