More on Smalltalk Productivity
Along the lines of this post, a reader submits this example:
Problem: I have a list of objects. I want to create another list containing the 'id' property of those objects.
Solutions:
Ruby
list.map { |i| i.id }Java
List ids = new ArrayList(); for (Iterator i = list.iterator(); i.hasNext(); ) { ids.add(new Long(((Thingy)i.next()).getId())); } Java w/ 1.5-style Generics and For Loop
List ids = new ArrayList(); for (Thingy x :list) { ids.add(new Long(x.getId())); }And the Smalltalk for that:
list collect: [[:each | each id]I think that all speaks for itself...
Yes, it certainly does. Just in sheer time at the keyboard, Java loses...

Comments
Re: More on Smalltalk Productivity
[Reinout] September 29, 2003 13:12:32.613
Comment on More on Smalltalk Productivity by Reinout
Why do people keep saying the keyboarding is so important? IMO it's the *reading it back* that should be optimal (because that happens more often), so that is what code should be optimized for.
Note also that low character count is not a good criterium; a while ago I provided a Smalltalk implementation for a puzzle, compare the array languages to the Smalltalk implementation at http://www.vector.org.uk/v174/puzzl174.htm
.The array languages win hands down in character count, but the author of that particular piece was pleased with 'the revealing Smalltalk solution' even though it is much more verbose than the array language solutions.
RE: More on Smalltalk productivity
[Babu] September 29, 2003 18:52:11.877
I'd say productivity is a measure of keyboard stress _and_ the readback stress. Java's list processing capabilities is annoying (C# with foreach, is slightly better). Perhaps Python is a compromise?
i.id for i in collection
I'm not a Java hater; especially since it earns my bread. But, for all the good aspects about it, I don't think it is the _best_ for lot of solutions.
A bit further even...
[Travis Griggs] October 1, 2003 0:27:09.458
I know the point you're trying to make, but have you really made it? I mean, they're all on one line, aren't they? And as for terseness, doesn't the Ruby version actually come out ahead of conventional Smalltalk (read: I'm sure S# has some psycho way of doing this under all kinds of pretexts)?
Personally, I haven't written the line
in quite some time. I always write
which in my mind takes the terseness reward back from Ruby but preserves the communicate clearly nature of Smalltalk. It is so easy for others to do this. Just define the following method:
Symbol>>value: anObject ^anObject perform: self
I know, it looks scary, but it's really not. We've been using this pervasively in systems for more than a year now. There have been no ill or evil side affects. Except that our code is even cleaner than before.
Re: A bit further even...
[Michael Lucas-Smith] October 1, 2003 6:57:16.515
Comment on A bit further even... by Michael Lucas-Smith
list collect: #id
Untitled
[Hmm] October 2, 2003 2:44:27.715
Let's be fair and use the same name for the temporary variable in all examples.
Autoboxing
[Mike Brazinski] October 2, 2003 8:38:17.649
The Java example isn't taking advantage of autoboxing. Primitives and object wrappers are mostly interchangable with 1.5. The example would look like:
List ids = new ArrayList(); for (Thingy x :list) { ids.add(x.getId()); }I know... your point is still made. But it needs to be fair to Java.Ruby has #collect too
[Joey Gibson] October 2, 2003 9:18:48.382
- map in Ruby is a synonum for #collect, so you could rewrite your example as
list.collect { |i| i.id } which now look almost like the Smalltalk example.Untitled
[] February 14, 2004 3:35:05.762
Java can also do it in a single line. For example, using com.webobjects.foundation: NSArray ids = (NSArray)list.valueForKey("id");