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
The Filter Function
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Eloy Zuniga Jr.
May 15, 2018
Programming
89
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
The Filter Function
Basic understanding of the `filter` function.
Eloy Zuniga Jr.
May 15, 2018
More Decks by Eloy Zuniga Jr.
See All by Eloy Zuniga Jr.
The `max` function
eloy
0
69
The Command Line Interface
eloy
2
510
Python Decorators
eloy
8
640
Other Decks in Programming
See All in Programming
Haskell/Servantを通してWebミドルウェアを捉え直す
pizzacat83
1
600
型も通る、synthも通る、それでも危ない 〜AIのCDKの権限とコストを機械で検証する〜 / It Passes Type Checks, It Passes Synth Checks, but It’s Still Risky — Automatically Verifying Permissions and Costs in AI’s CDK —
seike460
PRO
1
390
광주소프트웨어마이스터고등학교 DevFest 특강 - 바이브 코딩 시대에서 주니어 개발자로 살아남는 방법
utilforever
1
150
継続モナドとリアクティブプログラミング
yukikurage
3
620
act1-costs.pdf
sumedhbala
0
240
生成AI導入の「期待外れ」を乗り越える ー 開発フロー改革が目指す、真の組織変革
starfish719
0
1.4k
地域 SRE コミュニティ最前線 - ホンマでっかSRE勉強会
tk3fftk
0
260
「正の参照」と 「負の導出」で組む ハーネスエンジニアリング
cottpan
1
150
どこまでゆるくて許されるのか
tk3fftk
0
520
Apache Hive: Toward a Cloud Native Lakehouse
okumin
0
150
言語を使う側から、作る側へ。 自作 Lisp で得た新たな気づき。
andpad
0
130
Hatena Engineer Seminar #37「言語モデルの活用に関する研究」
slashnephy
0
530
Featured
See All Featured
Exploring anti-patterns in Rails
aemeredith
3
450
The #1 spot is gone: here's how to win anyway
tamaranovitovic
3
1.1k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
2k
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
620
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
660
Crafting Experiences
bethany
1
230
Game over? The fight for quality and originality in the time of robots
wayneb77
1
220
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
How to Think Like a Performance Engineer
csswizardry
28
2.7k
Thoughts on Productivity
jonyablonski
76
5.3k
Become a Pro
speakerdeck
PRO
31
6k
Lightning talk: Run Django tests with GitHub Actions
sabderemane
0
220
Transcript
Handle Nonetype objects # Remove Nonetypes before using Max L
= [None, None, None] max([i for i in L if is not None], default=None) @eloy · python 3.6 · 2018 1/14
Using the filter function # Remove Nonetypes before using Max
max(filter(None, L), default=None) @eloy · python 3.6 · 2018 2/14
filter() Keep item if 'True'
Filter function # Takes a function and an iterable filter(<function>,
<iterable>) @eloy · python 3.6 · 2018 4/14
Let's try an example # Anything greater than 2 shall
pass def greater_than_2(item): """Assume item is integer.""" return item > 2 items = filter(greater_than_2, [1, 2, 3, 4]) @eloy · python 3.6 · 2018 5/14
Using the filter function # Remove Nonetypes before using Max
max(filter(None, L), default=None) @eloy · python 3.6 · 2018 6/14
@eloy · python 3.6 · 2018 7/14
Identity function # If NO function is passed, then we
use the "Identity function" filter(None, iterable) # Identity function = Is this Truthy or Falsey? truthy_items = [True, object, 'a', '0', 1, 2, -1] falsey_items = [False, None, '', "", 0] @eloy · python 3.6 · 2018 8/14
Testing identity function # Everything makes it through the filter
everything = filter(None, [True, object, 'a', 1]) # Nothing makes it through the filter nothing = filter(None, [False, None, '', "", 0]) @eloy · python 3.6 · 2018 9/14
Lets make a None function # Anything greater than 2
shall pass def fake_none(item): """Test truthiness.""" return bool(item) items = filter(fake_none, [1, 2, 3, 4]) @eloy · python 3.6 · 2018 10/14
Filter returns an iterable iterable = filter(None, truthy_items) @eloy ·
python 3.6 · 2018 11/14
Still an iterable iterable = list(filter(None, truthy_items)) @eloy · python
3.6 · 2018 12/14
Technically an iterator iterator = filter(None, truthy_items) @eloy · python
3.6 · 2018 13/14
Do you know the difference? 4 iterable 4 lazy iterable
4 iterator 4 range We'll talk about this next month :D @eloy · python 3.6 · 2018 14/14