Skip to content

Instantly share code, notes, and snippets.

@bryanhunter
Last active November 12, 2017 00:57
Show Gist options
  • Save bryanhunter/8efbbd34dbf84e044b53d239b050ac92 to your computer and use it in GitHub Desktop.
Save bryanhunter/8efbbd34dbf84e044b53d239b050ac92 to your computer and use it in GitHub Desktop.
BangPipe - a really silly Elixir macro
Interactive Elixir (1.5.1) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> defmodule Bangpipe do
...(1)> defmacro left <|> right do
...(1)> quote do
...(1)> unquote(left)
...(1)> |> case do
...(1)> {:ok, val} -> val
...(1)> val -> val
...(1)> end
...(1)> |> unquote(right)
...(1)> end
...(1)> end
...(1)> end
{:module, Bangpipe,
<<70, 79, 82, 49, 0, 0, 4, 128, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 94,
0, 0, 0, 9, 15, 69, 108, 105, 120, 105, 114, 46, 66, 97, 110, 103, 112, 105,
112, 101, 8, 95, 95, 105, 110, 102, 111, ...>>, {:<|>, 2}}
iex(2)> import Bangpipe
Bangpipe
iex(3)> {:ok, [1,2,3,4]} <|> Enum.sum()
10
iex(4)>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment