Hello,
I think you're looking for `.get_lazy/3` functions.
Best regards,
Jean
Le mardi 26 janvier 2021 à 00:47 -0800, Paweł Urbanek a écrit :
> Looks like get does not evaluate the fallback function:
>
> Keyword.get([{:a, 1}], :b, fn () -> 2 + 2 end) =>
> #Function<45.97283095/0 in :erl_eval.expr/5>
>
> Maybe fetch!/3 could work like that?
>
> Keyword.fetch!([{:a, 1}], :b, fn () -> 2 + 2 end) => 4
> On Tuesday, January 26, 2021 at 9:40:13 AM UTC+1 [email protected]
> wrote:
> > Hello,
> >
> > There is Map.get/3 already, how would this differ?
> >
> > Best
> >
> > Adam
> >
> > On Tue, 26 Jan 2021 at 08:38, Paweł Urbanek
> > <[email protected]> wrote:
> > >
> > > I'd like to propose extending the API for Keyword and Map, fetch!
> > > method:
> > >
> > > Map.fetch!(%{a: 1}, :b, 2) => 2
> > > Keyword.fetch!([a: 1], :b, 2) => 2
> > >
> > > Third argument represents value which will be returned in case
> > > the requested key is missing. Optionally maybe it could also
> > > accept the function which is executed only in case the key is
> > > missing and it's return value is used.
> > >
> > > It would allow a simple way to fallback to default option values.
> >
--
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/7a87bcdad172833a37cb4e465696864b360e568b.camel%40gmail.com.