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.
To invoke an anonymous function, call it by the assigned name and add . between the name and arguments.
The pipe operator is the main operator for function composition in Elixir. It takes the result of the expression before it and passes it as the first argument of the following expression. Pipes replace nested function calls like foo(bar(baz)) with foo |> bar |> baz .
It will look bit weird but must work:
def boundary do :crypto.rand_bytes(8) |> Base.encode16 |> (&("--------FormDataBoundary" <> &1)).() end
Related: if the "anonymous" function has been assigned to a variable, you can pipe to it like this:
def boundary do add_marker = fn (s) -> "--------FormDataBoundary" <> s end :crypto.rand_bytes(8) |> Base.encode16 |> add_marker.() end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With