Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Elixir Stream by Examples
Search
Wu Qing
March 18, 2018
Programming
60
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Elixir Stream by Examples
Presented at ElixirCamp 3 (March 16–19, 2018) in Lord Somers Camp, Somers, VIC
Wu Qing
March 18, 2018
Transcript
23 slides
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
More Decks by Wu Qing
See All by Wu Qing
Building a Slack app with Phoenix
wiserfirst
0
82
Journey of Upgrading an App to Phoenix 1.6
wiserfirst
0
90
Bitwise Fun with Elixir
wiserfirst
0
220
Tower of Hanoi with Phoenix LiveView
wiserfirst
0
660
What is telemetry all about
wiserfirst
0
95
What's New In Ecto 3
wiserfirst
1
320
Practical tips about with (in Elixir)
wiserfirst
0
52
Gentle Introduction to Elixir Macros
wiserfirst
0
60
Fight flaky specs with RSpec
wiserfirst
0
82
Other Decks in Programming
See All in Programming
止まらない比較を止める — 余帰納法で無限ストリームを比べる
tarao
0
150
Materialが消えた日 〜真っ白なFlutterキャンバスにRenderObjectで絵を描く〜
umigishiaoi
0
160
コード生成時代のAPI定義を支えるRequest Spec移行
pvcresin
0
120
Apple Intelligenceとオンデバイス時代の到来
yahir0
0
240
flutter_test のその先へ — Flutter アプリの LLM 機能に「評価」という品質保証を
kosukesaigusa
0
200
React Native経験者が Flutterを30⽇間触って驚いたこと
3saito
0
180
ローカルLLMの基本と近況 2611 / Local LLM basics and recent situation 2611
kishida
2
410
Claude Code ハーネスDeep Dive – 君はどこまで制御する? -
takateeen_knj
6
5.8k
SODA - FACT BOOK(EN)
sodainc
1
240
動画プレイヤーはなぜカクつくのか 〜Flutterのレンダリングとネイティブの境界を解剖する〜
naruogram
0
340
Getting Started with Vibe Coding
miccheng
0
220
Kawaiicon: Kubernetes, it's no longer cool, but let's secure it anyway!
barnbarn
0
130
Featured
See All Featured
Designing for Timeless Needs
cassininazir
1
560
Max Prin - Stacking Signals: How International SEO Comes Together (And Falls Apart)
techseoconnect
PRO
0
530
Raft: Consensus for Rubyists
vanstee
143
7.9k
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
530
Faster Mobile Websites
deanohume
310
32k
Exploring anti-patterns in Rails
aemeredith
4
570
Documentation Writing (for coders)
carmenintech
77
5.6k
WCS-LA-2024
lcolladotor
0
1k
Leveraging LLMs for student feedback in introductory data science courses - posit::conf(2025)
minecr
1
480
Utilizing Notion as your number one productivity tool
mfonobong
4
640
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
520
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
20k