ben-pr-p

ben-pr-p

Style: cleanest way to pipe to an Enum.map with an anonymous function

In general I’ve been sticking to this community style guide GitHub - christopheradams/elixir_style_guide: A community driven style guide for Elixir · GitHub, but there’s one thing that really hurts my eyes when I see it in my code.

When I pipe a list to Enum.map or another Enum function:

[value] =
  logins
  |> Enum.filter_map(
      fn [un | _] -> un == username end,
      fn [_ | [pwd | _]] -> pwd end
    )
  |> # go on

It looks even worse if it’s a multiline function:

  "candidates"
  |> Cosmic.get_type()
  |> Enum.filter(fn
      %{"metadata" => %{"callable" => "Callable"}} -> true
      _ -> false
    end)
  |> Enum.map(fn %{"slug" => slug, "title" => name} -> %{slug: slug, name: name} end)
  |> Poison.encode()

This feels wrong! What do other people do?

Most Liked

peerreynders

peerreynders

I find that inline anonymous function definitions are a bit of a bad habit in the JavaScript world.

Using lambda-style is fine when the function is mercilessly short. Otherwise just define a module function and use the capture syntax. And if you need to use a closure define a module function that returns an anonymous function with a closure.

defp is_user(username),
  do: fn [un | _] -> un == username end

defp extract_pwd([_ | [pwd | _]]),
  do: pwd

...

[value] =
  logins
  |> (Enum.filter_map (is_user username), &extract_pwd/1)
  |> # go on

.

defp is_callable(%{"metadata" => %{"callable" => "Callable"}}), do: true
defp is_callable(_), do: false

defp extract_slug_name(%{"slug" => slug, "title" => name}),
  do: %{slug: slug, name: name}

...

"candidates"
  |> Cosmic.get_type()
  |> Enum.filter(&is_callable/1)
  |> Enum.map(&extract_slug_name/1)
  |> Poison.encode()
NobbZ

NobbZ

Why does it feel wrong?

I do it exactly that way, except that I lineup end) and |> in the same column:

list
|> Enum.map(fn a ->
  a
end)
|> …

But to be honest, I prefer to simply give that function a name and def(p) it:

list
|> Enum.map(&id/1)
|> …

Or using const (as an example with extra arguments:

list
|> Enum.map(&const(&1, 5))
def const(_, b), do: b
def id(a), do: a
ben-pr-p

ben-pr-p

Ah yeah, I am a recovering Node.js programmer. Then I learned Lisp and tried to program Javascript for fun for a while with only expressions, which lead to lots of immediately invoked function expressions and .filter .map chains.

I’ll define private functions. Thanks everyone!

Where Next?

Popular in Discussions Top

cvkmohan
The upcoming Phoenix 1.6 release looks very interesting. Became a habit to watch the commits - and - what they are bringing in. phx.gen...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53690 245
New
sashaafm
Piggy backing a bit on @dvcrn topic BEAM optimization for functions with static return type?, I’ve been trying to understand in a deeper ...
New
pillaiindu
I want to convert a Phoenix LiveView CRUD website to a CRUD mobile app. What do you think is the easiest way to do so?
New
lorenzo
Hey everone! I created a prototype for my app using Nodejs for the api. But the framework I chose wasnt great (in general theresnt any g...
New
AlexMcConnell
The reason that Rails is as popular as it is is because it’s very easy for relatively inexperienced developers to get a lot of work done....
588 19568 166
New
Ankhers
Just a little information upfront. Generally speaking, if I feel like I need to either break a pipe chain or use an anonymous function in...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31142 143
New
restack_oslo
Hello, Please pardon me for any faux paux. I am 46 and this is my first time on a forum of any kind. I wanted to to get answers from tho...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
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
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
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
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
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
hariharasudhan94
I would like to know what is the best IDE for elixir development?
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

Latest on Elixir Forum

We're in Beta

About us Mission Statement