[erlang-questions] Asserting exact maps
zxq9
zxq9@REDACTED
Wed Oct 11 11:25:22 CEST 2017
On 2017年10月11日 水曜日 18:20:20 zxq9 wrote:
> On 2017年10月11日 水曜日 08:47:40 Technion wrote:
> > Hi,
> >
> >
> > I'm wondering if there is a simple process I can use to verify a map
> > only contains valid variables.
>
> But I prefer the list version that operates over lists, personally.
... which I totally failed to provide any version of whatsoever.
A super simple one:
1> Checkmap =
1> fun(Map, Keys) ->
1> lists:sort(Keys) == lists:sort(maps:keys(Map))
1> end.
#Fun<erl_eval.12.87737649>
2> Checkmap(#{"one" => 1, "two" => 2}, ["one", "two"]).
true
3> Checkmap(#{"one" => 1, "two" => 2}, ["two", "one"]).
true
4> Checkmap(#{"one" => 1, "two" => 2}, ["two", "one", "three"]).
false
5> Checkmap(#{"one" => 1, "two" => 2}, ["two"]).
false
Of course, here I am returning a boolean, but I prefer that for assertions
because I can do:
true = checkmap(Map, Keys)
and move on -- and reuse that function somewhere else in a non-crashing
way if I need to.
-Craig
More information about the erlang-questions
mailing list