Passing the map to Map.get_lazy's function

44 views
Skip to first unread message

David Long

unread,
Mar 19, 2017, 10:44:19 PM3/19/17
to elixir-lang-core
I was working on some code that would simply test if one key was in a map and return it's value, and otherwise return a different key's value.

The map had a form like the following:

%{
  oncall: "dlong",
  overrideoncall: "jsmith",
  ...
}

If there was someone overriding on call then their name would be in the `overrideoncall` key, otherwise the key would not be in the map.

I think this could be a nice use for `Map.get_lazy`, if the function had the original map passed in, allowing me to write the code as follows:

Map.get_lazy(on_call_group, :overrideoncall, &(&1[:oncall]))

Before I wrote a pull request, I thought I'd see if this is a feature that makes sense to others?

José Valim

unread,
Mar 20, 2017, 12:44:28 AM3/20/17
to elixir-l...@googlegroups.com
The reason why get_lazy does not pass the map is because you already have the map, since you pass it as first argument.

--
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/719dc4fc-ae23-4437-b059-d91356e9b43b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--


José Valim
Skype: jv.ptec
Founder and Director of R&D

David Long

unread,
Mar 20, 2017, 6:48:46 AM3/20/17
to elixir-l...@googlegroups.com

That's not entirely accurate. In my case I was using a series of pipes so I did not have access to the map. My case might be very niche, though.


You received this message because you are subscribed to a topic in the Google Groups "elixir-lang-core" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/elixir-lang-core/JuAZnOXpo5Q/unsubscribe.
To unsubscribe from this group and all its topics, 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/CAGnRm4J7dZWROSBnBU7Ayn%3DhQGphp_ciFZTRf83XscDTQQ2z3w%40mail.gmail.com.

Ben Wilson

unread,
Mar 20, 2017, 7:49:06 AM3/20/17
to elixir-lang-core
Can you elaborate on the code you have in mind? This sounds like a general characteristic of pipes and not something specific to Map.get_lazy.

David Long

unread,
Mar 20, 2017, 7:53:23 AM3/20/17
to elixir-lang-core

Hi Ben,
My issue is specific to pipes. What I'm ultimately doing is taking a JSON object, parsing it with Poison.decode!, And then attempting to pull a single key out, either overrideoncall or oncall.


Michał Muskała

unread,
Mar 20, 2017, 9:32:30 AM3/20/17
to elixir-lang-core
You could define a helper function, for example:

defp map_get_any(map, keys), do: Enum.find_value(keys, &Map.get(map, &1))

That could be used like this:

data
|> Poison.decode!
|> map_get_any(["foo", "bar"])

Michał.

José Valim

unread,
Mar 20, 2017, 9:41:43 AM3/20/17
to elixir-l...@googlegroups.com

That's not entirely accurate. In my case I was using a series of pipes so I did not have access to the map. My case might be very niche, though.

All the capabilities all there. Not all functions in the standard library can be pipable on all cases exactly as you want.

Define a private functions and you should be golden.

David Long

unread,
Mar 20, 2017, 10:01:50 AM3/20/17
to elixir-lang-core, jose....@plataformatec.com.br
Than you all. I did implement a private method. I just thought I'd put out there, the suggestion of making the `Map.get_lazy` method a little more pipe friendly.
Reply all
Reply to author
Forward
0 new messages