Hacker Newsnew | comments | show | ask | jobs | submit login

I think ruby has it nailed better.

    a[1..10] # elements from 1 to 10, including 10
    a[1...10] # elements from 1 to 9; k...n is a half open range notation
    a[3, 5] # 5 elements, starting from position 3
Arrays are still zero based, but I feel this is more a homage to for- and while- loops from older languages.

(for (i = 0; i < n; i++) is just a very nice notation, and so is while (i < n))




Ruby offers three very similar syntaxes for the same concept. Contrast that with Python's "there should be one—and preferably only one—obvious way to do it"[1] ethos.

[1] http://www.python.org/dev/peps/pep-0020/

-----


I don't know anything about ruby but that seems extremely error prone to me.

EDIT: I think I know why I think it's error prone:

In all programming languages besides the core syntax of the language idioms appear among the community of coders. For instance, in C you travel through an array using something like:

    for (i = 0; i < n; i++)
        a[i] = ...;
Where n is the length of the array. This is an idiom so common that if for some reason in some code the length of an array is (n + 1), instead of writing for (i = 0; i <= n; i++) I'll write for (i = 0; i < (n + 1); i++).

Because the idiom is so strongly entrenched in my brain (and probably many other C coders) if I read the <= version there's a great chance I'll read it as a "<" and assume i goes from 0 to n - 1.

My point is adding several alternative "syntactic sugar" to express the same thing can end up confusing newbies because they'll have to learn the difference between all version (and recognize them) as well as experienced coders because you're more likely to mistake the meaning of an expression at a cursory glance.

-----


From the perspective of someone who doesn't know Ruby, the fact that a[1..10] is longer than a[1...10] is pretty counterintuitive.

-----


I found it quite unintuitive too at first. My way of remembering it was to think of the distance between the endpoints. The first example is inclusive because it's closer and the endpoint is "included" whereas the second range endpoint is shunned and shoved further away and is exclusive because it's "excluded"!

-----


p..q is a fairly common math notation synonymous with [p,q].

Although, p...q is not very obvious, indeed.

-----


Well, they aren't synonymous in Ruby. It's [p,q] operator is also not intuitive for me.

But then, "not intuitive" just means I'll spend a few minutes more learning the language because of it (except in C++, where it means many hours).

-----


Is it possible to nail something when the approach to a decision between two conventions is to implement all three?

-----


I have to agree with some of the others here and say that not all of the above is very intuitive.

In particular a[3, 5]. I would be expecting it to return elements 3 & 5 (shortcut to a[3] & a[5]) instead of doing a length slice.

For eg, in perl:

  my @a = qw/zero one two three four five six/;

  $a[3];        # "three"   
  @a[3, 5];     # ("three", "five")
  @a[3, 0, 5];  # ("three", "zero", "five")
  @a[1..5];     # ("one", "two", "three", "four", "five")
  
Also the ... operator is slightly at odds when compared against .. operator. I think perl6 has a more intuitive version:

  @a[1..^5];  #  ["one", "two", "three", "four"]
So instead of 1 to 5 (1..5) you think 1 upto 5 (1..^5).

-----


Agreed. It seems messy at first, but what Ruby does is make it possible to write code in the most readable way.

    a[first..last]
    left, right = a[0...pivot], a[pivot...a.len]
    a[start, len]
If the coder picks the right one and the variable names are good, the reader won't have to think about it.

-----


Ruby doesn't nail it using ranges. Ruby nails it by implementing Array#each.

0-index versus 1-index should not be an issue at this level of programming. You shouldn't be using for-loops; you should be iterating from start to finish.

-----




Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | DMCA | Apply to YC | Contact

Search: