News for hackers. Stuff that matters.
Re:Judge Alsup learns to code and schools Oracle by jerf on Wednesday May 16, 2012 @02:51AM (#3980813)
In Java, no. In other languages, yes.

    sub rangeCheck {
        my ($len, $from, $to) = @_;
        if (($from > $to) || ($from < 0) || ($to > $len)) {
            die "Illegal index $len ($from / $to)";
        }
    }
Java, as usual, makes a big hairy deal out of it. If the lack of types bothers you it's trivial in Haskell too. (Though trying to be idiomatic would lead you down a very different road. Perl is more similar to Java than Haskell, even as they are all very distant from each other.)

As it happens I've recently written somewhat similar code in Perl where I want to explicitly deal with out-of-range accesses (in a way other than silently receiving undefs) in a particular case, so this isn't even that odd.

Re:Judge Alsup learns to code and schools Oracle by felipemnoa on Wednesday May 16, 2012 @04:46AM (#3981088)
This is the the version in Java. Only four lines (including the curly bracket).

       private static void rangeCheck(int length, int fromIndex, int toIndex) {
           if (fromIndex > toIndex || fromIndex < 0 || toIndex > length) 
               throw new Exception("Illegal index");
       }
Re:Judge Alsup learns to code and schools Oracle by 6ren on Wednesday May 16, 2012 @07:28AM (#3981543)
You're throwing a generic exception; Bloch's version throws two specific exceptions, together with appropriate information. Java tends to have excellent error reporting.

All this reminds me of people saying that could code stackoverflow or facebook in a weekend. The difference is that rangeCheck actually is trivial - it's just that it's easy to underestimate the work involved in coding something. Even something trivial.

Re:Judge Alsup learns to code and schools Oracle by felipemnoa on Wednesday May 16, 2012 @06:21PM (#3984361)
>>You're throwing a generic exception; Bloch's version throws two specific exceptions, together with appropriate information. Java tends to have excellent error reporting.

I know. I reduced it to one generic exception because that is what the person I was replying to did. Bloch's version is better of course.

Re:Judge Alsup learns to code and schools Oracle by self on Wednesday May 16, 2012 @06:28AM (#3981387)
Shouldn't that be "toIndex >= length"?
Re:Judge Alsup learns to code and schools Oracle by 6ren on Wednesday May 16, 2012 @11:39AM (#3982442)
Bloch used > http://news.ycombinator.com/item?id=3940683

I think in defining a range, the toIndex means up-to-but-not-including the element at that index. Thus, to include the full array, you'd use the range fromIndex=0, toIndex=length (one past the last one, since the index of the last element is length-1). With this notation, you can then make ranges of any width, from 0 up to length. If instead you included the element at that index, you couldn't have 0-width ranges (unless you allowed toIndex to equal one less than fromIndex).

I don't know whether 0-width ranges are directly useful for the Timsort algorithm; or whether it's just a convenient notation that happens to have that ability as an unused side-effect.