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

mikl
I wanted to capitalize a string, and tried using String.capitalize(). That generally works well, until you try to capitalize a word like...
New
mmmrrr
Just saw that dhh announced https://hotwire.dev/ Is it just me or is this essentially live view? :smiley: Although I like the “iFrame-e...
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
tmbb
This is a post to discuss the new Phoenix LiveView functionality. From Chris’s talk, it appears that they generate all HTML on the serve...
342 18146 126
New
fireproofsocks
I’ve been working on an Elixir project that has required a lot of scripting. I usually reach for Elixir because I like it more (and in th...
New
PragTob
Hey everyone, this has been brewing in my head some time and it came up again while reading Adopting Elixir. GenServers, supervisors et...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
jer
I’ve been using umbrellas for a while, and generally started off (on greenfield projects at least) by isolating subapps based on clearly ...
New
AstonJ
If so I (and hopefully others!) might have some tips for you :slight_smile: But first, please say which area you’re finding most challen...
New
paulanthonywilson
I like Umbrella projects and pretty much always use them for personal Elixir stuff, especially Nerves things. But I don’t think this is ...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
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
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
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
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
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
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement