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
Python's Functional Tools
Search
Julian Gindi
December 03, 2013
Programming
1
820
Python's Functional Tools
Advanced Python Magic
Julian Gindi
December 03, 2013
Tweet
Share
Other Decks in Programming
See All in Programming
Cursorハンズオン実践!
eltociear
2
1.2k
SODA - FACT BOOK(JP)
sodainc
1
8.9k
バッチ処理を「状態の記録」から「事実の記録」へ
panda728
PRO
0
200
フロントエンド開発のためのブラウザ組み込みAI入門
masashi
7
3.6k
他言語経験者が Golangci-lint を最初のコーディングメンターにした話 / How Golangci-lint Became My First Coding Mentor: A Story from a Polyglot Programmer
uma31
0
470
3年ぶりにコードを書いた元CTOが Claude Codeと30分でMVPを作った話
maikokojima
0
700
Webサーバーサイド言語としてのRustについて
kouyuume
1
5k
マイベストのシンプルなデータ基盤の話 - Googleスイートとのつき合い方 / mybest-simple-data-architecture-google-nized
snhryt
0
100
Blazing Fast UI Development with Compose Hot Reload (droidcon London 2025)
zsmb
0
400
CSC509 Lecture 08
javiergs
PRO
0
270
Leading Effective Engineering Teams in the AI Era
addyosmani
7
670
なんでRustの環境構築してないのにRust製のツールが動くの? / Why Do Rust-Based Tools Run Without a Rust Environment?
ssssota
14
47k
Featured
See All Featured
4 Signs Your Business is Dying
shpigford
186
22k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
9
940
Gamification - CAS2011
davidbonilla
81
5.5k
Writing Fast Ruby
sferik
630
62k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
31
2.9k
[RailsConf 2023] Rails as a piece of cake
palkan
57
5.9k
Fashionably flexible responsive web design (full day workshop)
malarkey
407
66k
Raft: Consensus for Rubyists
vanstee
140
7.2k
GitHub's CSS Performance
jonrohan
1032
470k
What's in a price? How to price your products and services
michaelherold
246
12k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
230
22k
A designer walks into a library…
pauljervisheath
209
24k
Transcript
Functional Python Tools Advanced Python Magic
I’m Julian Gindi • Python developer at iStrategylabs (here) •
(small-time) Python core contributor • github.com/juliangindi • @JulianGindi • JulianGindi.com • lebowski.juliangindi.com
Python @isl • Django (content-heavy websites) • General scripting and
internal tools • Fabric (executing commands on our servers) • Salt (provisioning our servers)
Goals • Basic understanding of functional programming • Explain how
it can help you craft simple, yet powerful code • Show you a couple practical applications of the functional paradigm
Golden Circle
Functional Programming (What) • First class functions. Higher-order functions •
Pure functions • State • Immutable Data • Verbs instead of nouns
Python? • Not a functional language in a “pure” sense
• Multi paradigm language • Doesn't really matter - tools are still useful and awesome • Practical
Why? • Downright awesome • Programs are easier to understand
and debug • Often faster • Superior ability to deal with concurrency
How? • Time for some CODE!
>>> my_tuple = ('A', 'B', ‘C’) >>> len(my_tuple) 3 >>>
my_tuple.append('D') Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'tuple' object has no attribute 'append'
Practical Features (Core) • Closures • Lambda’s • Iterators, generators
• Partials • List Comprehension • Tuples • Built in functions (map, reduce, apply)
# List Comprehensions def square_list(items): new_list = [] new_list.append([x
* x for x in items]) return new_list ! print square_list([1,2,3]) ! >> [1, 4, 9]
# Closures ! def average_closure(a, b): def sum(): return a
+ b return sum() / 2 ! print average_closure(4, 4) ! >> 4
import operator ! # Lambdas ! def weird_average(func, data) :
return reduce(operator.add, map(func, data)) / len(data) ! print weird_average(lambda x: x * x , [1,2,3]) ! >> 4.66
# Generators ! def fibonacci() : prev, current = 0,1
while True : yield current prev, current = current, prev + current ! # Generator instantiation gen = fibonacci() ! # Generator usage for i in range(10) : print gen.next() ! >> 1 1 2 3 5 8 13 21 34 55
# PARTIALS ! from functools import partial ! def power(x,y):
return x ** y ! square = partial(power, y = 2) cube = partial(power, y = 3) ! assert (square(5), cube(7)) == (25, 343) ! ! !
How to get started • ‘import functools’ • Create a
simple program in a functional style • http://www.defmacro.org/ ramblings/fp.html - Functional programming for the rest of us • Execution in the Kingdom of Nouns