Efficient way to filter a Map by value in Elixir

后端 未结 4 848
情深已故
情深已故 2021-01-04 14:20

In Elixir, what would be an efficient way to filter a Map by its values.

Right now I have the following solution

%{foo: \"bar\", biz: ni         


        
4条回答
  •  被撕碎了的回忆
    2021-01-04 15:06

    In this case comprehension would be a good idea, because it also does not create an intermediate list and returns you a map:

    map = %{baz: 4, biz: nil, foo: "bar"}
    for {key, value} <- map, !is_nil(value), into: %{}, do: {key, value}
    

提交回复
热议问题