Skip to content

Instantly share code, notes, and snippets.

@jmn
Created September 19, 2019 17:46
Show Gist options
  • Save jmn/b48a16b15df69397944eaed45043fe75 to your computer and use it in GitHub Desktop.
Save jmn/b48a16b15df69397944eaed45043fe75 to your computer and use it in GitHub Desktop.
Minimal Elixir Concurrent Downloader
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