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
Brian Hicks
September 07, 2017
Technology
180
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
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
160
Make Snacks: Yet Another JavaScript Build System
brianhicks
0
150
State of Elm 2017
brianhicks
1
580
µKanren: A Minimal Functional Core for Relational Programming
brianhicks
0
480
Terraform All The Things!
brianhicks
2
440
Kubernetes for the Mesos User
brianhicks
1
140
ch-ch-ch-ch-changes in Elm 0.17.0
brianhicks
2
1.9k
State of Elm 2016
brianhicks
3
600
Mesos + Consul = Developer Happiness (JUG)
brianhicks
1
150
Other Decks in Technology
See All in Technology
気軽に使える"情報のハブ"としてのNotion活用 〜フロー情報の集積点 と、 Claude Code × Notion AI〜
syucream
1
200
元・セキュリティ学習経験0大学生による業務紹介 / An Introduction to the Job by a Former College Student with Zero Security Training Experience
nttcom
0
200
レガシーな広告配信システムでのAI駆動開発/運用の挑戦
i16fujimoto
0
120
2026-06-24_人とAIの責務分離に基づく開発プロセスの提案.pdf
takahiromatsui
0
180
Zenoh on Zephyr on LiteX
takasehideki
2
110
水を運ぶ人としてのリーダーシップ
izumii19
4
1k
AIが自律的に回る開発ループを設計してチーム開発に組み込む
nekorush14
0
130
現場のトークンマネジメント
dak2
1
190
Microsoft のサポートとフィードバック総まとめ
murachiakira
PRO
0
110
2026 AI Memory Architecture
nagatsu
0
320
「ビジネスがわかるエンジニア」とは何か?
ryooob
0
320
From Prompt Engineering to Loop Engineering
shibuiwilliam
1
240
Featured
See All Featured
Agile that works and the tools we love
rasmusluckow
331
22k
Paper Plane
katiecoart
PRO
1
52k
Joys of Absence: A Defence of Solitary Play
codingconduct
1
400
Imperfection Machines: The Place of Print at Facebook
scottboms
270
14k
How to Get Subject Matter Experts Bought In and Actively Contributing to SEO & PR Initiatives.
livdayseo
0
140
The SEO Collaboration Effect
kristinabergwall1
1
490
Building a Modern Day E-commerce SEO Strategy
aleyda
45
9.1k
The browser strikes back
jonoalderson
0
1.3k
Chasing Engaging Ingredients in Design
codingconduct
0
230
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
31
3.2k
Digital Ethics as a Driver of Design Innovation
axbom
PRO
1
330
Bash Introduction
62gerente
615
220k
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