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

Last Post!

hawkyre

hawkyre

That’s another way, thanks!

Yeah it most definitely needs some refactoring but I was just trying to get something to work since I more often than not have to end up changing what I wrote hahaha

Where Next?

Popular in Questions Top

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
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
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
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
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

Other popular topics Top

joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
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
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
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
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New

We're in Beta

About us Mission Statement