Created
September 22, 2020 21:34
-
-
Save Ch4s3/ed487f82b792aa93e2acf3d273ceb97d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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