Language Comparison – List Mapping

Continuing with the language comparison theme, I’ll look at another common list processing operation: mapping a list to another list. Say I’ve got a list of user objects and I want to turn that into a list of user names instead. Here’s how it looks in each of the languages.

Java

Using the standard for-each approach:

List<String> userNames = new ArrayList<String>();
for (User user : users) {
  userNames.add(user.getName())
}

Or, using a functional-style library:

List<String> userNames = ListUtils.map(users, new Mapper<User, String>() {
  public String map(User user) {
    return user.getName();
  }
};

Note that in this case, using the functional library actually ends up using an extra line, depending on how you format your curly braces. Either way, it’s not concise and it’s not pretty.

GScript:

var userNames = users.map(\user -> user.Name)

Ruby:

userNames = users.map{|user| user.Name}

In Ruby, there’s always more than one way to do things, so “map” and “collect” are synonyms for the exact same function. In addition, there are destructive versions named map! and collect! that modify the array in place instead of creating a copy.

Python:

userNames = [user.getName() for user in users]

This is an example of a list comprehension, which is basically map + filter + iteration built in to the language (though my guess is that it’s not the most efficient way to iterate a list, since it creates a new list . . . I could be wrong there, though). Python does also have a built-in map() method that takes a function and an iterable, but the idiomatic python way is to use a list comprehension. My Python knowledge is also letting me down here; I haven’t seen any language-level support for properties in Python like there is in Ruby or GScript, so I’m assuming that you’ll have to actually perform a method call here to get the name out.


7 Comments on “Language Comparison – List Mapping”

  1. Carson Gross says:

    What? Is the GScript version so perfect that no explanation or discussion is needed?

    Cheers,
    Carson

  2. Carson Gross says:

    There should be a <smile/> tag above, but wordpress ate it.

  3. Stephen Colebourne says:

    FYI, Kijaro has an implementation of list comprehensions for Java.
    https://kijaro.dev.java.net/
    https://kijaro.dev.java.net/servlets/ProjectDocumentList

  4. Doug says:

    The gscript version requires typing one more character than the ruby version, so there’s some room for improvement there (not counting the var declaration, which we’ll take as a functional improvement over ruby).

  5. Alan Keefer says:

    @Stephen:
    Thanks for the link. That looks interesting . . . I hope the efforts there result in changes to the mainline compiler and JDK, but I’m glad people are trying to improve things.

    @Doug:
    Yeah . . . I guess the \ -> is one more character than | |, but you can’t win them all . . . well, not without totally copying the syntax, and we think this version is a little more obvious than the Ruby syntax. As an added bonus, our editor tuns the \ symbol into a lambda character and the -> characters into a single right-arrow character . . . which probably just confuses people who aren’t hardcore programmers.

  6. Scott McKinney says:

    Lets not forget that GScript is statically typed, which trumps the Ruby one-less-character syntax : )

  7. About python, my comment here https://guidewiredevelopment.wordpress.com/2008/04/17/language-comparison-properties/#comment-164 might shed some light on the python case.

    Also, instead of a list comprehension, you can use a generator comprehension:

    userNames = (u.name for u in users)

    that will be lazily evaluated (ie, it won’t make a new list, but a new generator, which can be iterated over)

    There also more advanced stuff like:

    (u.name for u in users if u.isAdmin)

    Though I think this was introduced in Python 2.4 (or 2.5? not sure)


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