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.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

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

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New

Other popular topics Top

lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement