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.

Revisions

  1. Ch4s3 created this gist Sep 22, 2020.
    32 changes: 32 additions & 0 deletions piping.ex
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    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"}
    """