rm-rf-etc

rm-rf-etc

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.

Showing Posts 1 to 6

rm-rf-etc

rm-rf-etc OP

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
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 GitHub - lexmag/elixir-style-guide: An opinionated Elixir style guide · GitHub

RudManusachi

RudManusachi

There ya go! =)

rm-rf-etc

rm-rf-etc OP

Sorry to have cut in front of you @RudManusachi.

I hear what you’re saying, but this seems it’s pretty small to break out into a separate function. Mind showing me what you think would be the best practice for this?

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.

rm-rf-etc

rm-rf-etc OP

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

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews