Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Snake Snacks: Function Composition, The Dumb Way
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Brian Hicks
September 07, 2017
Technology
170
0
Share
Snake Snacks: Function Composition, The Dumb Way
Brian Hicks
September 07, 2017
More Decks by Brian Hicks
See All by Brian Hicks
Esperanto
brianhicks
0
150
Make Snacks: Yet Another JavaScript Build System
brianhicks
0
130
State of Elm 2017
brianhicks
1
570
µKanren: A Minimal Functional Core for Relational Programming
brianhicks
0
470
Terraform All The Things!
brianhicks
2
420
Kubernetes for the Mesos User
brianhicks
1
120
ch-ch-ch-ch-changes in Elm 0.17.0
brianhicks
2
1.8k
State of Elm 2016
brianhicks
3
580
Mesos + Consul = Developer Happiness (JUG)
brianhicks
1
150
Other Decks in Technology
See All in Technology
AI時代における技術的負債への取り組み
codenote
1
1.5k
Introduction to Sansan, inc / Sansan Global Development Center, Inc.
sansan33
PRO
0
3.1k
Data Hubグループ 紹介資料
sansan33
PRO
0
2.9k
AgentCore×VPCでの設計パターンn選と勘所
har1101
3
280
AI駆動1on1〜AIに自分を育ててもらう〜
yoshiakiyasuda
0
120
The Journey of Box Building
tagomoris
4
2.7k
名刺メーカーDevグループ 紹介資料
sansan33
PRO
0
1.1k
実践ハーネスエンジニアリング:TAKTで実現するAIエージェント制御 / Practical Harness Engineering: AI Agent Control Enabled by TAKT
nrslib
11
4.6k
Standards et agents IA : un tour d’horizon de MCP, A2A, ADK et plus encore
glaforge
0
170
Chasing Real-Time Observability for CRuby
whitegreen
0
120
AIでAIをテストする - 音声AIエージェントの品質保証戦略
morix1500
1
120
最初の一歩を踏み出せなかった私が、誰かの背中を押したいと思うようになるまで / give someone a push
mii3king
0
160
Featured
See All Featured
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
520
More Than Pixels: Becoming A User Experience Designer
marktimemedia
3
380
What’s in a name? Adding method to the madness
productmarketing
PRO
24
4k
The Art of Programming - Codeland 2020
erikaheidi
57
14k
Art, The Web, and Tiny UX
lynnandtonic
304
21k
Exploring the relationship between traditional SERPs and Gen AI search
raygrieselhuber
PRO
2
3.8k
The Anti-SEO Checklist Checklist. Pubcon Cyber Week
ryanjones
0
120
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
200
End of SEO as We Know It (SMX Advanced Version)
ipullrank
3
4.1k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
130k
What does AI have to do with Human Rights?
axbom
PRO
1
2.1k
Sam Torres - BigQuery for SEOs
techseoconnect
PRO
0
250
Transcript
Snake Snacks FUNCTION COMPOSITION, THE DUMB WAY
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
HOW ARE WE GONNA GET THERE? The Worst Way Possible™
DOUBLE UNDERSCORE METHODS
DOUBLE UNDERSCORE METHODS
DUNDER METHODS
DUNDER MIFFLIN
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
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'
! DECORATORS "
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
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())
DECORATORS, THE NICE WAY >>> hype("cheese") "OH YEAH, IT'S CHEESE"
>>> hype("a moose") "OH YEAH, IT'S A MOOSE"
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]
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"}
__gt__ class ChessPlayer(object): def __init__(self, name, elo): self.name = name
self.elo = elo def __gt__(self, other): return self.elo > other.elo
__gt__ >>> carlsen = ChessPlayer("Carlsen, Magnus", 2827) >>> almasi =
ChessPlayer("Almasi, Zoltan", 2707) >>> carlsen > almasi True >>> carlsen < almasi False
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)) )
OUR FIRST TRY! @composable def inc(n): return n + 1
@composable def double(n): return n * 2 @composable def triple(n): return n * 3
OUR FIRST TRY! >>> (inc > inc)(0) 2 >>> (inc
> inc > inc)(0) 2
OUR FIRST TRY! ! 1 > 2 > 3 #
is equivalent to 1 > 2 and 2 > 3 # NOT (1 > 2) > 3
__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)) )
__rshift__ TO THE RESCUE! >>> (inc >> inc)(0) 2 >>>
(inc >> inc >> inc)(0) 3
__rshift__ TO THE RESCUE! >>> (inc >> double)(2) 6 >>>
(double >> inc)(2) 5 >>> (inc >> double >> triple)(2) 18
THANK YOU I'M SORRY