hawkyre

hawkyre

Dot (.) operator with pipe operator

Is there any way to leverage the pipe operator to access properties of an object after a sequence of operations?

I’m trying to do a sequence of operations followed by accessing a property in the final map but I can’t get it to work while making it look great

Here’s what I have:

hd(hd(ctg).tier_group.tiers_in_tier_group).tier.tier_pills
  |> Enum.at(pill_ix)
  |> &(&1.pill_id)

ElixirLS autocorrects this to

hd(hd(ctg).tier_group.tiers_in_tier_group).tier.tier_pills
  |> Enum.at(pill_ix)
  |> (& &1.pill_id)

and that does not work, it throws the following error:

(ArgumentError) cannot pipe Enum.at(
  hd(hd(ctg).tier_group.tiers_in_tier_group).tier.tier_pills,
  pill_ix
) into & &1.pill_id, can only pipe into local calls foo(), remote calls Foo.bar() or anonymous function calls foo.()

This makes me think it’s not possible, but there’s no harm in asking because it still feels like something with a trivial solution. The alternative would be doing this:

(hd(hd(ctg).tier_group.tiers_in_tier_group).tier.tier_pills
  |> Enum.at(pill_ix)).pill_id

but this way makes me have to add an extra parenthesis to the front, which is a bit ugly in my opinion because in the case I have to do it several times it will accumulate a lot of parenthesis in the front, plus I’ll have to go back and forth adding them.

Any insights into this? Thanks!

Marked As Solved

Nicd

Nicd

If you wanted to do it like this, you would need to write

  |> Enum.at(pill_ix)
  |> (&(&1.pill_id)).() # You need the parentheses to make it a function call

but in the latest Elixir you can also use

  |> Enum.at(pill_ix)
  |> then(& &1.pill_id)

which you may prefer. Or you might use foo = <your_pipe> and then foo.pill_id.

Also Liked

moogle19

moogle19

Why not use Map.fetch!/2?

hd(hd(ctg).tier_group.tiers_in_tier_group).tier.tier_pills
  |> Enum.at(pill_ix)
  |> Map.fetch!(:pill_id)
al2o3cr

al2o3cr

The leading parenthesis is the only thing you have a problem with in that expression? :stuck_out_tongue:

The short answer for your question is then, as noted by others. But I’d recommend you consider identifying and naming these complex operations better. For instance:

hd(hd(ctg).tier_group.tiers_in_tier_group).tier.tier_pills
  |> Enum.at(pill_ix)

could be:

def pill_at(x, idx) do
  Enum.at(hd(x.tier_group.tiers_in_tier_group).tier.tier_pills, idx)
end

and then your pipe is shorter / gone:

pill_at(hd(ctg), pill_ix).pill_id

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
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
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
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
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
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
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
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
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

We're in Beta

About us Mission Statement