View Source Adept.Map (adept v0.1.0)

Link to this section Summary

Functions

Put a value into a map, but only if that value is not nil

Stringify the keys in a map

Link to this section Functions

Link to this function

put_set(map, key, value)

View Source
@spec put_set(map :: map(), key :: any(), value :: any()) :: map()

Put a value into a map, but only if that value is not nil

This useful when building a new map from a list of attributes where you are not sure if they are set in the source and want to skip them if they are not

attr = %{ abc: 123, ghi: 789 }
new_map =
  %{}
  |> Adept.Map.put_set( :abc, Map.get(attr, :abc) )
  |> Adept.Map.put_set( :def, Map.get(attr, :def) )
  |> Adept.Map.put_set( :ghi, Map.get(attr, :ghi) )

examples

Examples

iex> Adept.Map.put_set( %{}, :abc, "a string" )
%{ abc: => "a string" }

iex> Adept.Map.put_set( %{}, :abc, nil )
%{}
@spec stringify_keys(map :: map()) :: map()

Stringify the keys in a map

example

Example

iex> Adept.Map.stringify_keys( %{:a => 1 , "b" => 2, 3 => 3 } )
%{"a" => 1 , "b" => 2, "3" => 3 }