Created
September 19, 2019 17:46
-
-
Save jmn/b48a16b15df69397944eaed45043fe75 to your computer and use it in GitHub Desktop.
Minimal Elixir Concurrent Downloader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Dl do | |
def download(url) do | |
case HTTPoison.get(url) do | |
{:ok, response} -> | |
case response.status_code do | |
200 -> | |
{response.body} | |
end | |
end | |
end | |
def bd do | |
["https://slashdot.org", "https://news.ycombinator.com"] | |
|> Flow.from_enumerable | |
|> Flow.map(&download/1) | |
|> Flow.partition() | |
|> Flow.map(fn {body} -> | |
{:ok, file} = File.open("out_path", [:write]) | |
:ok = IO.binwrite(file, body) | |
:ok = File.close(file) | |
end) | |
|> Flow.run | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment