Skip to content

Instantly share code, notes, and snippets.

@KamilLelonek
Created May 17, 2020 10:56
Show Gist options
  • Save KamilLelonek/77fcdfeebd8f1b68f1e0cab570c3bd89 to your computer and use it in GitHub Desktop.
Save KamilLelonek/77fcdfeebd8f1b68f1e0cab570c3bd89 to your computer and use it in GitHub Desktop.
defmodule Download do
use Pipe
def start(api_key) do
api_key
~> download()
~> parse()
end
defp download(123), do: {:ok, "valid result"}
defp download(_), do: {:error, "invalid API key"}
defp parse({:ok, message}), do: %{message: message, downloaded_at: Time.utc_now()}
end
Download.start(123)
#=> %{downloaded_at: ~T[10:53:31.643047], message: "valid result"}
Download.start("invalid") |> IO.inspect()
#=> {:error, "invalid API key"}
defmodule Pipe do
defmacro __using__(_) do
quote do
require unquote(__MODULE__)
import unquote(__MODULE__)
end
end
defmacro left ~> right do
quote do
case unquote(left) do
{:error, error} ->
{:error, error}
result ->
result |> unquote(right)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment