ActiveState Code

Recipe 440490: Boolean operations beyond the limit


Boolean operations offers more than its simple ability to test for truth

Python
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])

Discussion

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"

Comments

  1. 1. At 12:22 p.m. on 30 aug 2005, Graham Fawcett said:

    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:

    def unique(sequence):
        d = {}
        for item in sequence:
            d[item] = None
        return d.keys()
    
  2. 2. At 1:28 p.m. on 30 aug 2005, Javier Burroni (the author) said:

    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)

  3. 3. At 4:28 p.m. on 30 aug 2005, Ian Bicking said:

    even shorter... Shorter:

    def unique(seq):
        return dict.fromkeys(seq).keys()
    

    Or even better:

    def unique(seq):
        return sets.Set(seq)
    

    Well, that's a little trivial, maybe just:

    unique = sets.Set
    
  4. 4. At 6:18 a.m. on 31 aug 2005, Graham Fawcett said:

    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:

    from sets import Set as unique
    
  5. 5. At 12:50 p.m. on 6 sep 2005, Adrian Manrique said:

    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 :

    class A:
        def method( self ):
            print 'foo'
            return 1
    
    class B:
        def method(self):
            return len(B.__bases__) and B.__bases__[0].method(self) or self
    

    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/

  6. 6. At 1:03 p.m. on 6 sep 2005, Adrian Manrique said:

    sorry there is a typo at class B definition

    class A:
        def method( self ):
            print 'foo'
            return 1
    
    class B(A):
        def method(self):
            return len(B.__bases__) and B.__bases__[0].method(self) or self
    
  7. 7. At 1:01 p.m. on 19 sep 2005, Javier Burroni (the author) said:

    you got the point ;)

Sign in to comment