Brian Hicks
September 07, 2017
84

# Snake Snacks: Function Composition, The Dumb Way

## Brian Hicks

September 07, 2017

## Transcript

2. ### WHAT WE WANT def inc(n): return n + 1 def

double(n): return n * 2 # in the REPL >>> (inc >> double)(2) 6 >>> (double >> inc)(2) 5

8. ### DUNDER METHODS, THE NICE WAY class Boss(object): def __init__(self, name):

self.name = name def __str__(self): return self.name def __repr__(self): return '<%s: %s>' % (self.__class__.__name__, self) def introduce(self): return "Please meet my boss, %s" % self
9. ### DUNDER METHODS, THE NICE WAY >>> b = Boss("Michael Scott")

>>> b <Boss: Michael Scott> >>> str(b) 'Michael Scott' >>> b.introduce() 'Please meet my boss, Michael Scott'

11. ### DECORATORS, THE NICE WAY def memoize(fn): cache = {} def

inner(*args, **kwargs): key = str((args, kwargs)) if key not in cache: cache[key] = fn(*args, **kwargs) return cache[key] return inner
12. ### DECORATORS, THE NICE WAY @memoize def hype(stuff): return "OH YEAH,

IT'S %s" % stuff.upper() # is the same as... hype = memoize(lambda stuff: "OH YEAH, IT'S %s" % stuff.upper())
13. ### DECORATORS, THE NICE WAY >>> hype("cheese") "OH YEAH, IT'S CHEESE"

>>> hype("a moose") "OH YEAH, IT'S A MOOSE"
14. ### BUT… OBJECTS! class memoize(object): def __init__(self, fn): self.cache = {}

self.fn = fn def __call__(self, *args, **kwargs): key = str((args, kwargs)) if key not in self.cache: self.cache[key] = self.fn(*args, **kwargs) return self.cache[key]
15. ### BUT… OBJECTS! >>> hype("cheese") "OH YEAH, IT'S CHEESE" >>> hype("a

moose") "OH YEAH, IT'S A MOOSE" >>> hype.cache {"(('a moose',), {})": "OH YEAH, IT'S A MOOSE", "(('cheese',), {})": "OH YEAH, IT'S CHEESE"}
16. ### __gt__ class ChessPlayer(object): def __init__(self, name, elo): self.name = name

self.elo = elo def __gt__(self, other): return self.elo > other.elo
17. ### __gt__ >>> carlsen = ChessPlayer("Carlsen, Magnus", 2827) >>> almasi =

ChessPlayer("Almasi, Zoltan", 2707) >>> carlsen > almasi True >>> carlsen < almasi False
18. ### OUR FIRST TRY! class composable(object): def __init__(self, fn): self.fn =

fn def __call__(self, *args, **kwargs): return self.fn(*args, **kwargs) def __gt__(self, other): return self.__class__( lambda *args, **kwargs: other(self.fn(*args, **kwargs)) )
19. ### OUR FIRST TRY! @composable def inc(n): return n + 1

@composable def double(n): return n * 2 @composable def triple(n): return n * 3
20. ### OUR FIRST TRY! >>> (inc > inc)(0) 2 >>> (inc

> inc > inc)(0) 2
21. ### OUR FIRST TRY! ! 1 > 2 > 3 #

is equivalent to 1 > 2 and 2 > 3 # NOT (1 > 2) > 3
22. ### __rshift__ TO THE RESCUE! class composable(object): def __init__(self, fn): self.fn

= fn def __call__(self, *args, **kwargs): return self.fn(*args, **kwargs) def __rshift__(self, other): return self.__class__( lambda *args, **kwargs: other(self.fn(*args, **kwargs)) )
23. ### __rshift__ TO THE RESCUE! >>> (inc >> inc)(0) 2 >>>

(inc >> inc >> inc)(0) 3
24. ### __rshift__ TO THE RESCUE! >>> (inc >> double)(2) 6 >>>

(double >> inc)(2) 5 >>> (inc >> double >> triple)(2) 18