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
Elixir Stream by Examples
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Wu Qing
March 18, 2018
Programming
0
54
Elixir Stream by Examples
Presented at ElixirCamp 3 (March 16–19, 2018) in Lord Somers Camp, Somers, VIC
Wu Qing
March 18, 2018
Tweet
Share
More Decks by Wu Qing
See All by Wu Qing
Building a Slack app with Phoenix
wiserfirst
0
61
Journey of Upgrading an App to Phoenix 1.6
wiserfirst
0
72
Bitwise Fun with Elixir
wiserfirst
0
190
Tower of Hanoi with Phoenix LiveView
wiserfirst
0
630
What is telemetry all about
wiserfirst
0
77
What's New In Ecto 3
wiserfirst
1
290
Practical tips about with (in Elixir)
wiserfirst
0
42
Gentle Introduction to Elixir Macros
wiserfirst
0
41
Fight flaky specs with RSpec
wiserfirst
0
69
Other Decks in Programming
See All in Programming
Rubyと楽しいをつくる / Creating joy with Ruby
chobishiba
0
200
DevinとClaude Code、SREの現場で使い倒してみた件
karia
1
840
エージェント開発初心者の僕がエージェントを作った話と今後やりたいこと
thasu0123
0
220
The Ralph Wiggum Loop: First Principles of Autonomous Development
sembayui
0
3.7k
Python’s True Superpower
hynek
0
190
TROCCOで実現するkintone+BigQueryによるオペレーション改善
ssxota
0
120
DSPy入門 Pythonで実現する自動プロンプト最適化 〜人手によるプロンプト調整からの卒業〜
seaturt1e
1
430
Ruby x Terminal
a_matsuda
5
550
CSC307 Lecture 13
javiergs
PRO
0
310
grapheme_strrev関数が採択されました(あと雑感)
youkidearitai
PRO
1
190
要求定義・仕様記述・設計・検証の手引き - 理論から学ぶ明確で統一された成果物定義
orgachem
PRO
1
510
今更考える「単一責任原則」 / Thinking about the Single Responsibility Principle
tooppoo
3
1.2k
Featured
See All Featured
Conquering PDFs: document understanding beyond plain text
inesmontani
PRO
4
2.4k
Automating Front-end Workflow
addyosmani
1370
200k
Everyday Curiosity
cassininazir
0
150
Designing for Timeless Needs
cassininazir
0
150
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
From π to Pie charts
rasagy
0
140
New Earth Scene 8
popppiees
1
1.7k
Unsuck your backbone
ammeep
672
58k
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
93
Mobile First: as difficult as doing things right
swwweet
225
10k
Skip the Path - Find Your Career Trail
mkilby
1
71
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
91
Transcript
Elixir Stream by Examples Qing Wu
None
Service S Integration
Data source: API endpoint • millions of records involved •
pagination that maps to id ranges
Data source @spec fetch_data(integer) :: [Map.t] def fetch_data(page_number) do ...
end
Sending to Service S • Generate CSV files • Upload
to an FTP server
Sending data @spec handle_data([Map.t]) :: :ok | {:error, String.t} def
handle_data(data) do ... end
Solution page_range = 0..999 Enum.each(page_range, fn(page_number) -> page_number |> fetch_data
|> handle_data end)
None
Enum Module • Provide functions to work with collections that
can be enumerated over • Functions in Enum always produce immediate results
Enum examples iex(1)> result1 = 1..10 |> Enum.map(&(&1 + 20))
[21, 22, 23, 24, 25, 26, 27, 28, 29, 30] iex(2)> result2 = result1 |> Enum.filter(&(rem(&1, 2) == 0)) [22, 24, 26, 28, 30] iex(3)> result2 |> Enum.reduce(0, &+/2) 130
Solution (Naive) page_range = 0..999 Enum.each(page_range, fn(page_number) -> page_number |>
fetch_data |> handle_data end)
None
Solution (Naive) page_range = 0..999 Enum.each(page_range, fn(page_number) -> page_number |>
fetch_data |> handle_data end)
Enum Solution page_range |> Enum.map(&fetch_data/1) |> Enum.concat |> Enum.chunk_every(10_000) |>
Enum.each(&handle_data/1)
Enum Solution page_range |> Enum.map(&fetch_data/1) |> Enum.concat |> Enum.chunk_every(10_000) |>
Enum.each(&handle_data/1)
Enum Solution page_range |> Enum.map(&fetch_data/1) |> Enum.concat |> Enum.chunk_every(10_000) |>
Enum.each(&handle_data/1)
Enum Solution page_range |> Enum.map(&fetch_data/1) |> Enum.concat |> Enum.chunk_every(10_000) |>
Enum.each(&handle_data/1)
None
Stream module iex(1)> result1 = 1..10 |> Stream.map(&(&1 + 20))
#Stream<[enum: 1..10, funs: [#Function<46.51599720/1 in Stream.map/2>]]> iex(2)> result2 = result1 |> Stream.filter(&(rem(&1, 2) == 0)) #Stream<[enum: 1..10, funs: [#Function<46.51599720/1 in Stream.map/2>, #Function<39.51599720/1 in Stream.filter/2>]]> iex(3)> result2 |> Enum.reduce(0, &+/2) 130
Stream Solution page_range |> Stream.map(&fetch_data/1) |> Stream.concat |> Stream.chunk_every(10_000) |>
Enum.each(&handle_data/1)
Summary • Stream module is very handy for lazy evaluation
• saves unnecessary iterations and memory • determine when to trigger evaluation
Further reading http://elixir-lang.org/getting-started/ enumerables-and-streams.html Offical Docs https://hexdocs.pm/elixir/Stream.html