RMap.Ruby (REnum v0.8.0)
Summarized all of Ruby's Hash functions. Functions corresponding to the following patterns are not implemented
- When a function with the same name already exists in Elixir.
- When a method name includes
!
. - <, <=, ==, >, >=, [], []=, default_*
Link to this section Summary
Functions
Returns a 2-element tuple containing a given key and its value.
Returns %{}.
Returns the object in nested map that is specified by a given key and additional arguments.
Calls the function with each key; returns :ok.
Calls the function with each value; returns :ok.
Returns a map excluding entries for the given keys.
Returns a list containing the values associated with the given keys.
When a function is given, calls the function with each missing key, treating the block's return value as the value for that key.
Returns a list whose entries are those for which the function returns a truthy value.
Returns a flatten list.
Returns a map object with the each key-value pair inverted.
See Enum.count/1
.
Returns a 2-element tuple consisting of the key and value of the first-found entry having a given value.
Returns a list whose entries are all those from self for which the function returns false or nil.
Removes the first map entry; returns a 2-element tuple. First element is {key, value}. Second element is a map without first pair.
See Enum.count/1
.
Returns given map.
Returns a map with modified keys.
Returns a map with modified values.
Returns true if value is a value in list, otherwise false.
Returns a list containing values for the given keys.
Link to this section Functions
assoc(map, key)
Specs
Returns a 2-element tuple containing a given key and its value.
Examples
iex> RMap.assoc(%{a: 1, b: 2, c: 3}, :a)
{:a, 1}
iex> RMap.assoc(%{a: 1, b: 2, c: 3}, :d)
nil
iex> RMap.assoc(%{a: %{b: %{c: 1}}}, :a)
{:a, %{b: %{c: 1}}}
clear(_)
Specs
clear(map()) :: %{}
Returns %{}.
Examples
iex> RMap.clear(%{a: 1, b: 2, c: 3})
%{}
delete_if(map, func)
See RMap.Ruby.reject/2
.
dig(result, keys)
Specs
Returns the object in nested map that is specified by a given key and additional arguments.
Examples
iex> RMap.dig(%{a: %{b: %{c: 1}}}, [:a, :b, :c])
1
iex> RMap.dig(%{a: %{b: %{c: 1}}}, [:a, :c, :b])
nil
each_key(map, func)
Specs
Calls the function with each key; returns :ok.
Examples
iex> RMap.each_key(%{a: 1, b: 2, c: 3}, &IO.inspect(&1))
# :a
# :b
# :c
:ok
each_pair(map, func)
See Enum.each/2
.
each_value(map, func)
Specs
Calls the function with each value; returns :ok.
Examples
iex> RMap.each_value(%{a: 1, b: 2, c: 3}, &IO.inspect(&1))
# 1
# 2
# 3
:ok
eql?(map1, map2)
See Map.equal?/2
.
except(map, keys)
Specs
Returns a map excluding entries for the given keys.
Examples
iex> RMap.except(%{a: 1, b: 2, c: 3}, [:a, :b])
%{c: 3}
fetch_values(map, keys)
Specs
Returns a list containing the values associated with the given keys.
Examples
iex> RMap.fetch_values(%{ "cat" => "feline", "dog" => "canine", "cow" => "bovine" }, ["cow", "cat"])
["bovine", "feline"]
iex> RMap.fetch_values(%{ "cat" => "feline", "dog" => "canine", "cow" => "bovine" }, ["cow", "bird"])
** (MapKeyError) key not found: bird
fetch_values(map, keys, func)
Specs
When a function is given, calls the function with each missing key, treating the block's return value as the value for that key.
Examples
iex> RMap.fetch_values(%{ "cat" => "feline", "dog" => "canine", "cow" => "bovine" }, ["cow", "bird"], &(String.upcase(&1)))
["bovine", "BIRD"]
filter(map, func)
Specs
Returns a list whose entries are those for which the function returns a truthy value.
Examples
iex> RMap.filter(%{a: 1, b: 2, c: 3}, fn {_, v} -> v > 1 end)
%{b: 2, c: 3}
flatten(map)
Specs
Returns a flatten list.
Examples
iex> RMap.flatten(%{1=> "one", 2 => [2,"two"], 3 => "three"})
[1, "one", 2, 2, "two", 3, "three"]
iex> RMap.flatten(%{1 => "one", 2 => %{a: 1, b: %{c: 3}}})
[1, "one", 2, :a, 1, :b, :c, 3]
has_value?(map, value)
See RMap.Ruby.value?/2
.
inspect(map)
See Kernel.inspect/1
.
invert(map)
Specs
Returns a map object with the each key-value pair inverted.
Examples
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}
keep_if(map, func)
See RMap.Ruby.filter/2
.
key(map, key, default \\ nil)
See Map.get/3
.
key?(map, key)
See Map.has_key?/2
.
length(map)
See Enum.count/1
.
rassoc(map, value)
Specs
Returns a 2-element tuple consisting of the key and value of the first-found entry having a given value.
Examples
iex> RMap.rassoc(%{a: 1, b: 2, c: 3}, 1)
{:a, 1}
iex> RMap.rassoc(%{a: 1, b: 2, c: 3}, 4)
nil
iex> RMap.rassoc(%{a: %{b: %{c: 1}}}, %{b: %{c: 1}})
{:a, %{b: %{c: 1}}}
reject(map, func)
Specs
Returns a list whose entries are all those from self for which the function returns false or nil.
Examples
iex> RMap.reject(%{a: 1, b: 2, c: 3}, fn {_, v} -> v > 1 end)
%{a: 1}
select(map, func)
See RMap.Ruby.filter/2
.
shift(map)
Specs
Removes the first map entry; returns a 2-element tuple. First element is {key, value}. Second element is a map without first pair.
Examples
iex> RMap.shift(%{a: 1, b: 2, c: 3})
{{:a, 1}, %{b: 2, c: 3}}
iex> RMap.shift(%{})
{nil, %{}}
size(map)
See Enum.count/1
.
store(map, key, value)
See Map.put/3
.
to_hash(map)
Specs
Returns given map.
Examples
iex> RMap.to_hash(%{a: 1, b: 2, c: 3})
%{a: 1, b: 2, c: 3}
to_s(map)
See Kernel.inspect/1
.
transform_keys(map, func)
Specs
Returns a map with modified keys.
Examples
iex> RMap.transform_keys(%{a: 1, b: 2, c: 3}, &to_string(&1))
%{"a" => 1, "b" => 2, "c" => 3}
iex> RMap.transform_keys(%{a: %{b: %{c: 1}}}, &to_string(&1))
%{"a" => %{b: %{c: 1}}}
transform_values(map, func)
Specs
Returns a map with modified values.
Examples
iex> RMap.transform_values(%{a: 1, b: 2, c: 3}, &inspect(&1))
%{a: "1", b: "2", c: "3"}
iex> RMap.transform_values(%{a: %{b: %{c: 1}}}, &inspect(&1))
%{a: "%{b: %{c: 1}}"}
value?(map, value)
Specs
Returns true if value is a value in list, otherwise false.
Examples
iex> RMap.value?(%{a: 1, b: 2, c: 3}, 3)
true
iex> RMap.value?(%{a: 1, b: 2, c: 3}, 4)
false
values_at(map, keys)
Specs
Returns a list containing values for the given keys.
Examples
iex> RMap.values_at(%{a: 1, b: 2, c: 3}, [:a, :b, :d])
[1, 2, nil]