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?

1 Like