I stumbled across this page on the c2 wiki today. It's a discussion about why this:
foreach my $item (@list) { ...do something here... }is less desiarable to this smalltalk/rubyish construct:
@list.foreach {|item| ...do something here... }In Perl (and Ruby), the syntactic difference is subtle. In Java, the syntactic difference is larger. (See the Wiki discussion for an example.) The main difference is that throwing a closure at a
foreach
method on a collection allows you to use a
named function just as easily -- instant reuse! So stop copying the
body of a for loop over and over again (or just as bad, write it from
scratch every time you need it) and start composing code through
combinations of higher order operations.
All of this is well and good in the abstract. But at some point, this code has to execute. It's nice to talk about ivory tower constructs that are platonically "better" than the brute force coding. How does it all work in practice?
Ward Cunningham has some insight as to why Java programmers are so hesitant to adopt these Smalltalk memes:
I read in a recent performance tuning book that inner classes hava a ram footprint of about 3K each before they are even instantiated. Compare this to two or three bytes per (latent) block in Smalltalk and you get a feeling for why each language has its own conventions. -- WardCunninghamHm. I smell a leaky abstraction.