rm-rf-etc

rm-rf-etc

Nicer way to pipe?

This is basically the same question I’ve asked before, I’m just wondering if anything has changed since I last asked, or if there’s any other ways to do this thing that I am doing.

I know there’s been proposals for alternate ways to pipe some input into an argument slot other than the first position, and I understand why those got shot down, so I’m not really asking about that.

Here’s an example of a function I wrote recently, and shows a style I’ve been doing kind of frequently.

  def get_meter(num, total) do
    Decimal.div(num, total)
    |> Decimal.mult(10)
    |> Decimal.round(0)
    |> Decimal.to_integer()
    |> (fn level ->
      Enum.map(0..10, fn ^level -> "|"; _ -> "-" end)
      |> Enum.join()
    end).()
  end

And this is another way to do the same thing, which I kind of like because we don’t have to add the extra parentheses, but we do have to add an extra level of indentation, which I find annoying.

  def get_meter(num, total) do
    Decimal.div(num, total)
    |> Decimal.mult(10)
    |> Decimal.round(0)
    |> Decimal.to_integer()
    |> case do
        level ->
          Enum.map(0..10, fn ^level -> "|"; _ -> "-" end)
          |> Enum.join()
    end
  end

Using case is really nice when we truly have different conditions to handle in different ways, and I love that we can then pipe the result from the case statement/operator/function/macro/whatever-the-correct-term-is into whatever else comes after, but in the example shown, I don’t like it much.

The best alternative I can find to make this a little cleaner would be this:

  def do_func(arg, func), do: func.(arg)

  def get_meter(num, total) do
    Decimal.div(num, total)
    |> Decimal.mult(10)
    |> Decimal.round(0)
    |> Decimal.to_integer()
    |> do_func(fn level ->
      Enum.map(0..10, fn ^level -> "|"; _ -> "-" end)
      |> Enum.join()
    end)
  end

Would be nice if Elixir had a built-in Kernel.do_func/2.

Most Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Exactly. It is not obvious what the intent of (&Enum.map(0..10, fn ^&1 -> "|"; _ -> "-" end)).() is at all. However if you name it, then it’s a lot clearer.

RudManusachi

RudManusachi

Another approach could be to use capture operator & to create anonymous function. Plus after it we could take Enum.join/1 out to the “main pipeline”

  def get_meter(num, total) do
    Decimal.div(num, total)
    |> Decimal.mult(10)
    |> Decimal.round(0)
    |> Decimal.to_integer()
    |> (&Enum.map(0..10, fn ^&1 -> "|"; _ -> "-" end)).()
    |> Enum.join()
  end

However, I think overall this pattern of wrapping code into anonymous function just to fit into pipeline is considered as bad a bad practice and it’s more preferred to break it into separate functions or intermediate values
(see https://github.com/lexmag/elixir-style-guide#anonymous-pipeline

rm-rf-etc

rm-rf-etc

'Aight, good feedback. You all were right, I moved some things around and the code got better. Thanks everyone.

rm-rf-etc

rm-rf-etc

Oh hey! This is not bad! Just realized I could make this small change.

  def do_func(arg, func), do: func.(arg)

  def get_meter(num, total) do
    Decimal.div(num, total)
    |> Decimal.mult(10)
    |> Decimal.round(0)
    |> Decimal.to_integer()
    |> do_func(&Enum.map(0..10, fn ^&1 -> "|"; _ -> "-" end))
    |> Enum.join()
  end

Where Next?

Popular in Questions Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
Tee
can someone please explain to me how Enum.reduce works with maps
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
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 record...
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
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
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
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
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
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New

We're in Beta

About us Mission Statement