air

air

Map.take is not a good name

Hi all! Just want to know if there is someone else thinking Map.take is not a very good name for function that returns subset of a map. It’s not an issue or something but I feel uncomfortable with that name. In ruby, for example, method with same functionality called slice. When I type Map.take([:a, :b]) I expect a list of values mapped to these keys. What do you think?

First 10 of 26 Posts Switch mode

lucaong

lucaong

You mean you would expect it to do something like this?

Enum.map(keys, fn key ->
   case Map.fetch(map, key) do
     {:ok, value} -> {key, value}
     _ -> nil
   end
 end)

Perhaps, but I think that the name take is not obviously indicating one or the other case, so I personally don’t think it’s a bad name. Maybe it could have been called Map.pick, just to differentiate it from Enum.take which takes a number of elements, but renaming it now would have much bigger disadvantages than advantages in my opinion.

air

air OP

I expect it like this:

take = fn map, keys ->
  Enum.map keys, & Map.get(map, &1)
end

a = %{a: 1, b: 2, c: 3}
take.(a, [:b, :c])
> [2, 3]

Well, I don’t think it has to be renamed. Just want to know what other people think about it.

lpil

lpil

Creator of Gleam

I find it quite understandable. I’ve always found slice hard to remember, even after many years of Ruby

air

air OP

My point is that you take values from map. I think, slice is not ideal either. But in my opinion, it is better that take.

lpil

lpil

Creator of Gleam

You also take a subset. Language is tricky :slight_smile:

gregvaughn

gregvaughn

The name take seems very natural to me. What you describe there could be done with %{a: 1, b: 2, c: 3} |> Map.take([:b, :c]) |> Map.values() (provided ordering is not important).

However, I have wanted a Map.values/2 that has an extra parameter to name which keys’ values you want.

air

air OP

Yes, I can, but I also can do it with several other ways. But oneliner is much prettier that 2 pipes.

That’s a very good idea!
Since Elixir already has Map.values/1 it could be a little effort to make that function.

gregvaughn

gregvaughn

You want a one-liner? You asked the right person :grin:

def values(%{} = map, keys) when is_list(keys), do: for {k, v} <- map, k in keys, do: v
air

air OP

:+1:Like a charm

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

FYI, I’m 90% sure that map |> Map.take(keys) |> Map.values is gonna win from a performance perspective because those are both BIFs.

Where Next?

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 91561 914
New
byu
@chrismccord : I just saw the Extract AGENTS.md from Phoenix.new into phx.new generator commit to the phoenix project. My initial shotgu...
New
arcanemachine
I was working on an Ecto migration and I needed a timestamp. So, for the nth time, I looked up the different data types for timestamps, a...
New
AstonJ
Just a general thread to post chat/news/info relating to AI/ML stuff that may be relevant for Nx now or in the future. Got anything to sh...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
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
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
bjorng
We want to introduce a new native datatype to Erlang: native records. Although replacing all tuple records with native records is not our...
New

We're in Beta

About us Mission Statement