smalltalk

More on Smalltalk Productivity

September 29, 2003 11:03:47.444

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...

 Share Tweet This
-->