News for hackers. Stuff that matters.
Re:Array Iteration Interview Problem by telemachos on Wednesday August 01, 2012 @09:05PM (#4326662)
> Are you using iterators like .each or .map, or are you using a normal, index-based for loop?

I doubt you mean anything very judgmental by 'normal', but this still jumped out at me. In Ruby an index-based for loop is pretty unusual. The for loop is non-idiomatic, and it also has a scope leak: https://gist.github.com/3232118.

Re:Array Iteration Interview Problem by djur on Thursday August 02, 2012 @01:21PM (#4329837)
There actually _is no_ "normal, index-based" for loop in Ruby, only a for ... in iteration construct. Most C-style for loops can be implemented as #times or #up_to/#down_to/#step iterations.
Re:Array Iteration Interview Problem by jazzychad on Wednesday August 01, 2012 @09:13PM (#4326686)
hmm, my apologies if that sounded judgmental. I started coding a long time ago, so doing for loops was a very fundamental part of learning how to code. I (incorrectly?) assumed most people learned that form of looping first.
Re:Array Iteration Interview Problem by telemachos on Wednesday August 01, 2012 @09:18PM (#4326705)
Again, I didn't really think you were being judgmental. It's just that in Ruby, the for loop definitely doesn't have pride of place at all. You're may be right that most programmers learn languages where for is more common, but I wonder about people who start with Ruby. That is, do they think for is weirdly primitive when they start doing Javascript?

Anyhow, no offense taken at all. But I would be shocked if any Rubyist did use for.

Re:Array Iteration Interview Problem by bunderbunder on Wednesday August 01, 2012 @09:36PM (#4326749)
Even in languages like C# the standard for-loop seems to be heading in the direction of being considered harmful. Its status hasn't gone nearly as far along the path as for Ruby, but the tide's turning that way.

As far as creating a new array (or several new arrays) goes, there are some new considerations such as composability that are starting to be valued more highly than efficiency. And pure functions are more composable than ones that mutate the existing data.

Granted, the n < 0 case could still be handled in a way that's more efficient but still pure by allocating an array and filling it backward rather than using two reverses and an iterator.