Boolean operations offers more than its simple ability to test for truth
1 2 3 | ## example
unique = lambda x: reduce(lambda y,z: (not (z in y) and y.append(z) ) or y, x, [])
unique([3, 4, 5, 6, 5, 3, 7])
|
There are 2 properties in boolean operations that makes them useful: * Boolean operations offers lazy evaluation, and * Values that are not expecific false are considered true, and this could be used to inline a return statement (check "Truth Value Testing" in python doc) This properties may help to improbe the functional capabilities of python Also, this could be used for those smalltalk' programmers wich think that a return default value should be "self"
Faster unique function. This is not really related to your recipe, but rather to your unique-function. FYI, the fastest (most efficient) approach I'm aware of to return a list of unique values is to use a dictionary:
Faster unique function. I was tempted to use the dict version wich is like:
unique = lambda x: reduce(lambda y,z: y.setdefault(z, None) or y, x, {}).keys()
unique([3, 4, 5, 6, 5, 3, 7])
I prefer no to do so because it doesn't keeps the sequence's order, but I agree it's faster (and notably faster on old python versions)
even shorter... Shorter:
Or even better:
Well, that's a little trivial, maybe just:
unique = sets.Set: +1. I'm not sure why that makes me want to smile, Ian; but it does. How succinct!
Since you still need a separate line to import 'sets', you could also use the one-liner:
bollean operations, the kidss' friend. last night I was doing some stuff at my toilet... whatever! I was concerned about making code a little bit more maintable. so, given that I readed this recipe some time ago, inspiration came to me and :
this is super dooper, since I could get B as base class in the future without crashing anything. oh yeah! , thanks mr burroni for giving your knowledge to the people
amen/
sorry there is a typo at class B definition
you got the point ;)