Skip to content

Instantly share code, notes, and snippets.

@alco
Last active December 4, 2020 05:39
Show Gist options
  • Save alco/9956215 to your computer and use it in GitHub Desktop.
Save alco/9956215 to your computer and use it in GitHub Desktop.
iex> use PipeInspect
nil
iex> "hello" |> String.reverse |> String.upcase |> String.downcase
"olleh"
"OLLEH"
"olleh"
defmodule PipeInspect do
defmacro __using__(_) do
quote do
import Kernel, except: [{:|>, 2}]
import unquote(__MODULE__), only: [{:|>, 2}]
end
end
defmacro x |> f do
inspect = quote do: IO.inspect
stages = Enum.intersperse(Macro.unpipe(f), inspect)
# uncomment this line to also inspect the final value
# stages = stages ++ [inspect]
quote do
unquote(x) |> unquote(rebuild_pipe(stages))
end
end
defp rebuild_pipe([h]) do
h
end
defp rebuild_pipe([h|t]) do
{:|>, [], [h, rebuild_pipe(t)]}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment