Ah, yes, that's true, and this was perhaps a too quickly written example. I would still prefer state = opts |> Keyword.take!([:foo, :bar])|> Map.new() But this is probably not a good enough reason to add this to the standard library.
I will try to pay attention to more convincing examples in my own usage. In general it often has to do with redirecting options to other functions. On Sunday, January 20, 2019 at 8:02:50 PM UTC+1, José Valim wrote: > > Those are not equivalent though, as one is passing a map and the other a > keyword list. You could call Map.new but it is unlikely you have a map made > only of external arguments... so I am thinking you can't really escape from > buiilding the map by hand. > > > *José Valim* > www.plataformatec.com.br > Skype: jv.ptec > Founder and Director of R&D > > > On Sun, Jan 20, 2019 at 7:58 PM Valter Sundström <[email protected] > <javascript:>> wrote: > >> Sorry I messed up the title forgetting to add `[proposal]`, but since you >> answered here, let's go with this thread. >> >> The GenServer case I am talking about is essentially this: >> def start_link(opts) do >> state = %{ >> foo: Keyword.fetch!(opts, :foo), >> bar: Keyword.fetch!(opts, :bar) >> } >> >> GenServer.start_link(__MODULE__, state, opts) >> end >> >> >> I believe I am not alone in doing similar things, but possibly I am. The >> reasoning being to fail quickly at initialization. >> This could instead be written as: >> def start_link(opts) do >> GenServer.start_link(__MODULE__, Keyword.take!(opts, [:foo, :bar]), >> opts) >> end >> >> This, I think is clearer. >> >> >> >> >> On Sunday, January 20, 2019 at 7:47:53 PM UTC+1, José Valim wrote: >>> >>> Just for completeness, if we add those functions, we also need to add >>> Map.drop! and Keyword.drop!. Btw, your description here makes more sense, >>> as I can see this feature being useful for keywords, but not really for >>> maps. Do you have some code snippets you could show us too? >>> >>> Thanks Valter! >>> >>> *José Valim* >>> www.plataformatec.com.br >>> Skype: jv.ptec >>> Founder and Director of R&D >>> >>> >>> On Sun, Jan 20, 2019 at 7:36 PM Valter Sundström <[email protected]> >>> wrote: >>> >>>> I somewhat often find myself wanting to take subsets of maps and >>>> keywords lists, verifying that all keys I take exist. >>>> An initial PR for this is available here >>>> <https://github.com/elixir-lang/elixir/pull/8657>, but as josé pointed >>>> out, a discussion needs to be had regarding the need for it. >>>> One of my main usages of this is for example when initializing the >>>> state of a `GenServer`, or if extracting some options from a keyword list >>>> to pass along to some other module. >>>> Please chime in if you have any opinions! >>>> >>>> // Valter >>>> >>>> -- >>>> 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 [email protected]. >>>> To view this discussion on the web visit >>>> https://groups.google.com/d/msgid/elixir-lang-core/5990bdcd-83d2-4925-8af6-79bbaa488667%40googlegroups.com >>>> >>>> <https://groups.google.com/d/msgid/elixir-lang-core/5990bdcd-83d2-4925-8af6-79bbaa488667%40googlegroups.com?utm_medium=email&utm_source=footer> >>>> . >>>> For more options, visit https://groups.google.com/d/optout. >>>> >>> -- >> 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 [email protected] <javascript:>. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/elixir-lang-core/bf4a83a9-c925-40fe-a1f6-fbbb7159b5cb%40googlegroups.com >> >> <https://groups.google.com/d/msgid/elixir-lang-core/bf4a83a9-c925-40fe-a1f6-fbbb7159b5cb%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> For more options, visit https://groups.google.com/d/optout. >> > -- 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 [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/8117e4de-718f-4a8b-a6a1-374a9084c3b6%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
