Skip to content

Instantly share code, notes, and snippets.

@Ch4s3
Created September 22, 2020 21:34
Show Gist options
  • Save Ch4s3/ed487f82b792aa93e2acf3d273ceb97d to your computer and use it in GitHub Desktop.
Save Ch4s3/ed487f82b792aa93e2acf3d273ceb97d to your computer and use it in GitHub Desktop.
defmodule Testing do
"""
You can pipe straight into a case
Kernel.<<>>, the special form for binary pattern
matching can be used in a case match to
split up incoming binaries, like this
imaginary |~ delimited protocol.
"fiexd_size_header|~some message"
You can also pipe into an anonymous function
using |> ().()
Kernel.& begins the capture and &1 captures
the piped arg
"""
def pipes(input) do
input
|> case do
<<_header::binary-size(17), "|~", rest::binary>> ->
rest
_ ->
raise "Invalid"
end
|> (&{:ok, &1}).()
end
end
"""
iex > Testing.pipes("fiexd_size_header|~some message")
{:ok, "some message"}
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment