kwhite

kwhite

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

Most Liked

zachdaniel

zachdaniel

Creator of Ash

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

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

kokolegorille

kokolegorille

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

Where Next?

Popular in Questions Top

hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 130286 1222
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement