Language Comparison – List Iteration

In my spare time I’ve started to learn Python (I’m working through the most excellent, and free, Dive Into Python book). Learning other languages, and how programmers work in other languages, is something that can improve your code regardless of the language you’re programming in. And of course, it can also be fun and interesting, and as someone who plays at least a peripheral role in the development of the GScript language, it’s always good to know what features and styles in other languages turn out to be useful.

In order for GScript to be viable on its own some day, it’ll need to be at least as useful as the languages that seem to have the most popularity today, so I figured it might be interesting to put together a small series of posts comparing how different languages solve similar problems. Since we’ve pushed the development of GScript largely to combat many of the deficiencies in Java, I’ll probably end up focusing on the problems that Java doesn’t solve well. To keep the comparisons from getting too crazy, I won’t include every language I could, and instead I’ll focus on some of the languages that one might consider for starting a modern web-development project: Java, GScript, Ruby, and Python.

For each post I’ll look at just one specific problem as a way to keep them shorter and more to the point. I’ll start the series off by looking at one of the most basic language operations: list iteration. List iteration and manipulation together tend to form a large component of any code base, so many languages have specialized constructs for doing them (and Lisp, of course, is literally all list processing). I’ll focus on just iteration here and look at various types of list manipulations in later posts. So what does it look like to iterate over a list of Strings and invoke a method on each item using the canonical list-iteration techniques in each language?

Java:

Java has several different ways to iterate over a list. The oldest is the basic C-style for loop:

for (int i = 0; i < list.size(); i++) {
  foo(list.get(i));
}

Starting with Java 5, Java added in the Iterable interface and the ability to use a simpler for loop syntax:

for (String s : list) {
  foo(s);
}

And lastly, some more functional-programming types occasionally attempt to abstract things even further and push the code into a more functional form, resulting in code like:

ListUtils.each(list, new ListProcessor<String>() {
  public void process(String s) {
    foo(s);
  }
}

GScript:

GScript provides two different ways to iterate lists. It provides a for loop with syntax similar to the Java 1.5 syntax:

for (s in list) {
  foo(s)
}

Starting with the Bedrock set of releases you could also use blocks to do:

list.each(\s -> foo(s))

Ruby:

GScript takes the name for blocks (and their general style) from Ruby, so unsurprisingly there are a lot of similarities here. While you can use for loops in ruby, no one actually does (except for Java programmers trying to learn Ruby). Instead, the canonical list-iteration pattern is to use the each method:

list.each {|s| foo s}

Ruby also allows you to easily apply an if statement to the end of a statement, making it easier to conditionally perform some action:

list.each{|s| foo s if s == "a"}

Doing that in either Java or GScript requires wrapping the print in an if statement instead.

Python:

You could also use a for loop in Python that looks similar to the GScript one:

for s in list:
  foo(s)

That’s not the best example of Python’s list-processing capabilities, however. When we look into list manipulations, we’ll see that Python’s list comprehensions feature makes more complicated list manipulations relatively easy and concise, but they’re used for list transformation and not for iteration.

I’m not really an expert in either Python or Ruby, and I have to confess to not having tried out the Ruby examples (I don’t currently have a good Ruby-editing environment set up at home), so feel free to correct me if I’ve made any mistakes or missed out on some easier way to do things.


3 Comments on “Language Comparison – List Iteration”

  1. Carson Gross says:

    I don’t understand list comprehensions as a language feature. Why bake something like that into the language when you can add a simple, well known construct, closures, that will give you general abstraction of data structure algorithms, and then let the library guys figure out all the great methods to add to whatever data structures they so desire?

    Guido hates closures enough to take them out of the language. I wonder why?

    Cheers,
    Carson

  2. Alan Keefer says:

    That’s not entirely fair; Python has lambda statements, though they’re restricted to 1 line and I’m not sure about the scoping rules therein (so maybe they don’t count as proper closures). But Python does let you define a named function inline within another function in case you need something more than a one-liner. Between list comprehensions, one-line lambda statements, and inline function definitions you get basically the same level of conciseness as Ruby or GScript. And some methods that we’d put on the List, like map(), are built in to Python. Closures have weird scoping rules (one part of them that I hate), and as such I don’t think it’s unreasonable to be opposed to them when there are other options.

    That said, I definitely prefer Ruby/GScript approach, since I think it reads a lot more naturally and is more versatile.

    I can understand the list comprehensions thing, though; if you’re used to Lisp, where everything is a list, it makes sense to bake in support for operating on lists, and unsurprisingly people tend to use lists heavily in Python because of that. I can’t be sure that was Guido’s motivation since I haven’t read up on it enough, but that’s where I’m guessing it came from.

  3. Peter Rexer says:

    You might want to look at Aptana Studio for your Ruby programming. The free version looks like it handles a decent amount of code-completion and Rails helper functions, but doesn’t get in the way too much to be annoying. At least not for a Ruby N00b like me.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s