Piping results of single-line function

I just ran into this unexpected behavior. I had a short function that is piping its result into other functions. When I converted the short function to a one-liner, the result changed.

I expected the EOL to terminate the if/do/else and not for the else only to be piped to the next line. (If you add parentheses around the if/do/else line, it works the same as the multi-line function.)

What terminates a one-line function? Where else might this happen?

defmodule F do
  def fn1() do
    if true do
      1
    else
      0
    end
    |> Kernel.+(10)
  end

  def fn2() do
    if true, do: 1, else: 0
    |> Kernel.+(10)
  end
end

F.fn1()  # returns 11
F.fn2()  # returns 1
1 Like

|> is an operator, just like + (although with lower precedence). This guide has some relevant info: Operators — Elixir v1.15.7

If you consider this example:

    if true, do: 1, else: 0
    + 10

+ is left associative, so it goes “leftwards” until it gets to the end of its scope has a complete expression for its left side. To visualize the “end of its scope” bit, you can remove the syntactic sugar given by keyword lists.

It ends up being equivalent to something like this.

if true, [{:do, 1}, {:else, 0 + 10}]

EOL in Elixir aren’t semantic. I feel like there might be a counter example to this? Not sure. I know spaces matter in some cases, i.e %{foo:10} is not valid. I can’t come up with one off the top of my head.

So with that in mind you can, your original syntax is parsed like so:

if true, do: 1, else: 0
    |> Kernel.+(10)

if true, do: 1, else: 0 |> Kernel.+(10)

if true, [do: 1, else: 0 |> Kernel.+(10)]

and so on.

EDIT:

thought of an example. newlines are relevant in multiline strings, i.e

"""
foo
"""

The first """ has to have an EOL immediately after it, and the last one has to have an EOL immediately before it IIRC

2 Likes

It’s important to use () when using functions in pipes

You could do it like this too

iex> if(true, do: 1, else: 0) |> Kernel.+(10)
11
1 Like