News for hackers. Stuff that matters.
Re:Adventures of an imperative programmer in the land of fp by joshhart on Friday August 27, 2010 @02:28PM (#1639655)
This is built into the Scala API - foo.getOrElse(bar, "beebop")

Stuff like this should be built into the standard language.

Re:Adventures of an imperative programmer in the land of fp by koenigdavidmj on Friday August 27, 2010 @02:50PM (#1639756)
Python has it too:

* foo.get(bar) will return None if the element is not there * foo.get(bar, "beebop") does what your Scala example does * foo.setdefault works just like foo.get. However, if foo did not have key bar when it was called, it will fill the key in with the default value before returning. * collections.defaultdict is also worth a look.