def foo(m1, m2) do
m1
|> transform("some arg")
|> &other_func(m2, &1)
end
Which doesn’t work because |> only works with function calls, not functions.
If you convert it to a function call, it works! But the syntax is ugly.
def foo(m1, m2) do
m1
|> transform("some arg")
|> &(other_func(m2, &1)).()
end
Is there any reason to not make the first (prettier) form work? Basically, modify pipe operator to “if receive a function of arity 1, call the function passing in the arg”.
Or is there a better way to do what I’m trying to do? Thanks!
I feel you already have the solution there… If you find the .() too ugly just use a normal function wrapping your functionality. I think there is no better way, and the reason for not making the pipe is related to a discussion over lisp1 vs lisp2 function calls. Elixir just settles on one.
For instance, if you could have a variable contain an anonymous function in Elixir with zero arguments, it would be very difficult for Elixir to know when that function should be called or be used as a value, as the parenthesis may be omitted.
There are really two subject matters at work here.
foo
|> bar.()
just isn’t that bad. What’s ugly is
foo
|> &(bar(x, &1).()
```
but this latter problem simply stems from a wrong approach to piping in the first place.
As has been covered extensively in the relevant mailing list threads, the solution is to either:
```
thing =
foo
|> bar
|> baz
other_value
|> fun(thing)
|> etc
```
OR
```
def helpfully_named_fun(piped_value, other_value), do: fun(other_value, piped_value)
foo
|> bar
|> baz
|> helpfully_named_fun(other_value)
|> etc
```
Which is better depends on what produces the most readable code.