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
Wu Qing
March 18, 2018
Programming
54
0
Share
Elixir Stream by Examples
Presented at ElixirCamp 3 (March 16–19, 2018) in Lord Somers Camp, Somers, VIC
Wu Qing
March 18, 2018
More Decks by Wu Qing
See All by Wu Qing
Building a Slack app with Phoenix
wiserfirst
0
65
Journey of Upgrading an App to Phoenix 1.6
wiserfirst
0
81
Bitwise Fun with Elixir
wiserfirst
0
200
Tower of Hanoi with Phoenix LiveView
wiserfirst
0
640
What is telemetry all about
wiserfirst
0
81
What's New In Ecto 3
wiserfirst
1
300
Practical tips about with (in Elixir)
wiserfirst
0
45
Gentle Introduction to Elixir Macros
wiserfirst
0
44
Fight flaky specs with RSpec
wiserfirst
0
72
Other Decks in Programming
See All in Programming
自動レビューエンジンの実装と運用 ~レビューのない世界へ~
kurukuru1999
2
310
プロパティの順序で型推論が壊れる!? TypeScript6.0の修正からContext-Sensitivityの仕組みを追う
bicstone
2
1.3k
IBM Bobを活用したレガシーアプリの最新化
oniak3ibm
PRO
0
140
権限チェックの一貫性を型で守る TypeScript による多層防御
mnch
4
1.1k
TypeSpec で繋ぐ複数プロダクトの型安全
maroon8021
1
320
代数的データ型って何が嬉しいの? #frontend_phpcon_do
kajitack
8
3k
ふつうのFeature Flag実践入門
irof
7
3.5k
Composerを使ったサプライチェーン攻撃の様子を眺めてみる #phpstudy
o0h
PRO
2
210
作って学ぶ、 JSX (TSX) ランタイムの基本
syumai
7
1.5k
tsserverとは何だったのか、これからどうなるのか
nowaki28
1
430
ReactとSvelteのその先、Ripple-TS / Beyond React and Svelte: Ripple-TS
ssssota
3
1.9k
次世代リンターで探る、tsgo 時代における型認識カスタムルールの現実解
ytakahashii
3
1.4k
Featured
See All Featured
The Impact of AI in SEO - AI Overviews June 2024 Edition
aleyda
5
1.1k
How to build a perfect <img>
jonoalderson
1
5.5k
KATA
mclloyd
PRO
35
15k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
10k
jQuery: Nuts, Bolts and Bling
dougneiner
66
8.5k
How to make the Groovebox
asonas
2
2.2k
Writing Fast Ruby
sferik
630
63k
Art, The Web, and Tiny UX
lynnandtonic
304
22k
Rebuilding a faster, lazier Slack
samanthasiow
85
9.5k
WCS-LA-2024
lcolladotor
0
610
HTML-Aware ERB: The Path to Reactive Rendering @ RubyCon 2026, Rimini, Italy
marcoroth
1
130
More Than Pixels: Becoming A User Experience Designer
marktimemedia
3
430
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