Would Keyword.merge_all/1 be a good idea?

19 views
Skip to first unread message

Austin Ziegler

unread,
Apr 4, 2023, 4:53:23 PM4/4/23
to elixir-l...@googlegroups.com
I recently found myself wanting a variadic version of `Keyword.merge/2` similar to the way that Ruby’s `Hash#merge` works. Would `Keyword.merge_all/1` and `Map.merge_all/1` make sense?

You could go from this:

```elixir
  config
  |> Keyword.merge(config_host)
  |> Keyword.merge(config_user)
  |> Keyword.merge(config_port)
```

to this:

```elixir
Keyword.merge_all([
  config,
  config_host,
  config_user,
  config_port
])
```

I’d be happy to work on a PR to do this if there‘s interest.

-a
--

Allen Madsen

unread,
Apr 4, 2023, 4:58:13 PM4/4/23
to elixir-l...@googlegroups.com
One option that already exists:

[
  config,
  config_host,
  config_user,
  config_port
]
|> Enum.reduce(&Keyword.merge/2)



--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CAJ4ekQtoLaW%3D7wkYLcjLnUSwR2F97oTwCMUSkoBiiKLfhKn9ow%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages