Johnny's Software Saloon

Weblog where I discuss things that really interest me. Things like Java software development, Ruby, Ruby on Rails, Macintosh software, Cocoa, Eclipse IDE, OOP, content management, XML technologies, CSS and XSLT document styling, artificial intelligence, standard document formats, and cool non-computing technologies.

My Photo
Name:
Location: Germantown, Maryland, United States

I like writing software, listening to music (mostly country and rock but a little of everything), walking around outside, reading (when I have the time), relaxing in front of my TV watching my TiVo, playing with my cat, and riding around in my hybrid gas/electric car.

Wednesday, August 17, 2005

Bruce Eckel's MindView, Inc: 10-22-04 Using Function Objects as Strategies

Bruce Eckel's MindView, Inc: 10-22-04 Using Function Objects as Strategies

Bruce Eckel wrote a nice implementation of the old C++ trick called "expression templates" in Java 1.5. It was a decent, and even type-safe, implementation of functional programming using the new generics feature of the language.



However, it took a whole page of source code.



On the comments page for his blog entry, I did a program that did the exact same thing as his example program in just three lines of Python!




>>> def compute(low_boundary, values):
... return reduce(lambda x,y: x*y, filter(lambda low_bound:low_bound>low_boundary, values))
...
>>> compute(4, [1, 2, 3, 4, 5, 6, 7])
210


Which would you rather type on - a page of code or 3 lines?

0 Comments:

<< Home

Related pages & news