How to pass an anonymous function to the pipe in Elixir

ruisin picture ruisin · Jul 6, 2014 · Viewed 8.1k times · Source

I'd like to write the code like this:

def boundary do
  :crypto.rand_bytes(8)
  |> Base.encode16
  |> &("--------FormDataBoundary" <> &1)
end

But it doesn't work.

Answer

couchemar picture couchemar · Jul 7, 2014

It will look bit weird but must work:

def boundary do
  :crypto.rand_bytes(8)
  |> Base.encode16
  |> (&("--------FormDataBoundary" <> &1)).()
end