justaprogrammer
Monday, February 07, 2005
 
Practical coding
Over the past week, I've had several reasons to think about Smalltalk.

From code-samples written in Ruby to my thoughts on Object Protocols as I rewrite a significant body of Python into Java.

Variable names changed for obvious reasons :-)
In Python
anotherSubset = [ x for x in state.masterListOfPeople if x not in state.subsetOfPeople ]

Compare with the Java...

ArrayList anotherSubset = new ArrayList();
for (Iterator i = state.getMasterListOfPeople().iterator(); i.hasNext(); ) {
PersonState child = (PersonState)i.next();
if (!state.getSubsetOfPeople().contains(child)) {
listOfPeople.add(child);
}
}

or in Smalltalk
anotherSubset := masterListOfPeople reject: [: child | state subsetOfPeople contains: child ]

Smalltalk being the probably the most elegant...admittedly you have to know the Collection class inside and out...

Comments: Post a Comment

<< Home

Powered by Blogger