News for hackers. Stuff that matters.
Re:Apple Lossless Decoder in Coffeescript by jashkenas on Monday December 19, 2011 @07:15PM (#3371698)
This is one of the grayer areas. After your experience writing this decoder ... would you prefer CoffeeScript to keep its loops that can go in either direction, or would you prefer loops to always iterate upwards, and have to be explicit if you'd like to count from "[100..1] by -1" ?
Re:Apple Lossless Decoder in Coffeescript by Stratoscope on Tuesday December 20, 2011 @04:07AM (#3372806)
I've programmed in dozens of languages, and I can't think of another one where loops could automatically run either up or down depending on the start and end values.

Not knowing CoffeeScript well, when I see this code:

n = -10 for i in [0...n] doSomething()

I'd expect it to call doSomething zero times. At least that's what I'd expect in JavaScript or any of the other languages I've used.

Re:Apple Lossless Decoder in Coffeescript by jashkenas on Tuesday December 20, 2011 @07:56AM (#3373250)
I agree with you at least 40%, and that's originally how CoffeeScript behaved. Would you still feel that way if you had explicitly written:

    for i in [10..0]
I've opened a ticket to discuss this further: https://github.com/jashkenas/coffee-script/issues/1952
Re:Apple Lossless Decoder in Coffeescript by devongovett on Monday December 19, 2011 @07:24PM (#3371721)
Hmmm, well I think it's better if they can go in any direction by default otherwise people will be confused. The `by 1` should be considered an optimization IMO.
Re:Apple Lossless Decoder in Coffeescript by nitrogen on Monday December 19, 2011 @09:15PM (#3372012)
What if you compiled it something like this:

  for i in [a...b]
becomes

  if(a <= b) {
      for(i = a; i < b; i++) {
          // Code
      }
  } else {
      for(i = a; i > b; i--) {
          // Code again
      }
  }
Or otherwise moved the extra conditions outside the loop so they only get computed once?
Re:Apple Lossless Decoder in Coffeescript by jashkenas on Monday December 19, 2011 @09:28PM (#3372046)
Unfortunately, that requires repeating the entire body of the loop, and so isn't workable/acceptable for our purposes. If you'd like to see the original conversation that led to the current compilation, it's all available on the GitHub issues.
Re:Apple Lossless Decoder in Coffeescript by repsilat on Tuesday December 20, 2011 @01:54AM (#3372579)
I'll have a look for the discussion. Any idea if the branches are more expensive than some multiplies?

    step = (a<=b)?1:-1;
    for (i = a; step*i < step*b; i += step) {
      doSomething();
    }
I guess if the branch predictor is any good it could be slower with multiplies, but I have no idea what to expect in a dynamic language.