Elixir Meetup #1 ▶ Hosted by Curiosum Come join our team at Enzai!

REnum 0.6.0 has been released!

In this release, we have expanded the convenience functions related to Map. Also, I changed the README. I’ve made a lot of examples, so I hope you’ll take a peek and think, “This is convenient!” https://hexdocs.pm/r_enum/readme.html

About RMap

RMap is Map extended with convenient functions inspired by Ruby and Rails ActiveSupport. All the functions are available defined in

dig/2

Returns the object in nested map that is specified by a given key and additional arguments.

iex> RMap.dig(%{a: %{b: %{c: 1}}}, [:a, :b, :c])
1

iex> RMap.dig(%{a: %{b: %{c: 1}}}, [:a, :c, :b])
nil

each_key/2

Calls the function with each key; returns :ok.

iex> RMap.each_key(%{a: 1, b: 2, c: 3}, &IO.inspect(&1))
# :a
# :b
# :c
:ok
# See also RMap.Ruby.each_value, RMap.Ruby.each_pair

except/2

Returns a map excluding entries for the given keys.

iex> RMap.except(%{a: 1, b: 2, c: 3}, [:a, :b])
%{c: 3}

invert/1

Returns a map object with the each key-value pair inverted.

iex> RMap.invert(%{"a" => 0, "b" => 100, "c" => 200, "d" => 300, "e" => 300})
%{0 => "a", 100 => "b", 200 => "c", 300 => "e"}

iex> RMap.invert(%{a: 1, b: 1, c: %{d: 2}})
%{1 => :b, %{d: 2} => :c}

values_at/2

Returns a list containing values for the given keys.

iex> RMap.values_at(%{a: 1, b: 2, c: 3}, [:a, :b, :d])
[1, 2, nil]

deep_atomlize_keys/1

Returns a list with all keys converted to atom. This includes the keys from the root map and from all nested maps and arrays.

iex> RMap.deep_atomlize_keys(%{"name" => "Rob", "years" => "28", "nested" => %{ "a" => 1 }})
%{name: "Rob", nested: %{a: 1}, years: "28"}

iex> RMap.deep_atomlize_keys(%{"a" => %{"b" => %{"c" => 1}, "d" => [%{"a" => 1, "b" => %{"c" => 2}}]}})
%{a: %{b: %{c: 1}, d: [%{a: 1, b: %{c: 2}}]}}
# See also RList.ActiveSupport.deep_symbolize_keys, RList.ActiveSupport.symbolize_keys, RList.ActiveSupport.deep_stringify_keys, RList.ActiveSupport.stringify_keys,

deep_transform_keys/2

Returns a list with all keys converted to atom. This includes the keys from the root map and from all nested maps and arrays.

iex> RMap.deep_transform_keys(%{a: %{b: %{c: 1}}}, &to_string(&1))
%{"a" => %{"b" => %{"c" => 1}}}

iex> RMap.deep_transform_keys(%{a: %{b: %{c: 1}, d: [%{a: 1, b: %{c: 2}}]}}, &inspect(&1))
%{":a" => %{":b" => %{":c" => 1}, ":d" => [%{":a" => 1, ":b" => %{":c" => 2}}]}}
# See also RList.ActiveSupport.deep_transform_values