Fxnk.Map (Fxnk v0.1.4) View Source
Fxnk.Map
are functions that work with maps.
Link to this section Summary
Functions
Curried assemble/2
Takes an initial map and a "builder" map where each value is a function. Builds a new map by setting the keys in the function map to the values returned by the function applied to the original map.
Takes a map and a function that accepts a map and returns a map. Runs the map against the function and merges the initial map into the result.
Takes a map and a function that accepts a map and returns a map. Runs the map against the function and merges the initial map into the result.
combine/2
but also accepts a combining function as the last arguments.
Curried has_prop?/2
Takes a map and a property, returns true
if the property has a value in the map, false
otherwise.
Merges two maps together deeply. If both maps have the same key, the value on the left will be used. If both keys are a map, the maps will be merged together recursively, preferring values on the left.
Merges two maps together deeply. If both maps have the same key, the value on the right will be used. If both keys are a map, the maps will be merged together recursively, preferring values on the right.
Merges two maps together, if both maps have the same key, the value on the left will be used.
Merges two maps together, if both maps have the same key, the value on the right will be used.
Return a specific element in a nested map. If the path does not exist, returns the orignal map.
Like path/2
, but returns the or_value
when the path is not found.
Accepts a list of args, returns a curried pick/2
.
Accepts a string key
and returns a function that takes a map
. Returns the map's value at key
or nil
.
Accepts a map and a key. Returns the map's value at key
or nil
Curried prop_equals/3
, takes a value, returns a function that accepts a map and a key.
Curried prop_equals/3
, takes a key and a value. Returns a function that accepts a map.
Accepts a map, key and value. Checks to see if the key on the map is equal to the value.any()
Accepts a list of keys and returns a function that takes a map. Returns a list of the values associated with the keys in the map.
Accepts a map and a list of keys and returns a list of the values associated with the keys in the map.
Rename a key in a map, takes the map, current key and replacement key. Returns the original map with the updated key.
Rename multiple keys in a map. Takes the original map and a map where the key is the original key and the value is the replacement key.
Link to this section Functions
Specs
Curried assemble/2
Examples
iex> map = %{red: "red", green: "green", blue: "blue" }
iex> fnmap = %{
...> red: Fxnk.Flow.compose([&String.upcase/1, Fxnk.Map.prop(:red)]),
...> blue: Fxnk.Flow.compose([&String.reverse/1, Fxnk.Map.prop(:blue)])
...> }
iex> assembler = Fxnk.Map.assemble(fnmap)
iex> assembler.(map)
%{red: "RED", blue: "eulb"}
Specs
Takes an initial map and a "builder" map where each value is a function. Builds a new map by setting the keys in the function map to the values returned by the function applied to the original map.
Examples
iex> map = %{red: "red", green: "green", blue: "blue" }
iex> fnmap = %{
...> red: Fxnk.Flow.compose([&String.upcase/1, Fxnk.Map.prop(:red)]),
...> blue: Fxnk.Flow.compose([&String.reverse/1, Fxnk.Map.prop(:blue)])
...> }
iex> Fxnk.Map.assemble(map, fnmap)
%{red: "RED", blue: "eulb"}
Specs
Takes a map and a function that accepts a map and returns a map. Runs the map against the function and merges the initial map into the result.
Examples
iex> map = %{red: "red", green: "green", blue: "blue"}
iex> colorCombiner = Fxnk.Map.combine(fn %{red: red, blue: blue} -> %{purple: red <> blue} end)
iex> colorCombiner.(map)
%{red: "red", green: "green", blue: "blue", purple: "redblue"}
Specs
Takes a map and a function that accepts a map and returns a map. Runs the map against the function and merges the initial map into the result.
Examples
iex> map = %{red: "red", green: "green", blue: "blue"}
iex> colorCombiner = Fxnk.Functions.always(%{purple: "purple"})
iex> Fxnk.Map.combine(map, colorCombiner)
%{red: "red", green: "green", blue: "blue", purple: "purple"}
Specs
combine/2
but also accepts a combining function as the last arguments.
Examples
iex> map = %{colors: %{red: "red", green: "green", blue: "blue"}}
iex> colorCombiner = Fxnk.Functions.always(%{colors: %{red: "fire red", purple: "purple"}})
iex> Fxnk.Map.combine_with(map, colorCombiner, &Fxnk.Map.merge_deep_right/2)
%{colors: %{red: "fire red", green: "green", blue: "blue", purple: "purple"}}
Specs
Curried has_prop?/2
Examples
iex> hasFoo = Fxnk.Map.has_prop?(:foo)
iex> hasFoo.(%{foo: 'foo'})
true
iex> hasFoo.(%{bar: 'bar'})
false
Specs
Takes a map and a property, returns true
if the property has a value in the map, false
otherwise.
Examples
iex> Fxnk.Map.has_prop?(%{foo: "foo"}, :foo)
true
iex> Fxnk.Map.has_prop?(%{foo: "foo"}, :bar)
false
Specs
Merges two maps together deeply. If both maps have the same key, the value on the left will be used. If both keys are a map, the maps will be merged together recursively, preferring values on the left.
Example
iex> map1 = %{red: "red", green: %{green: "green", yellowish: "greenish", with_blue: %{turqoise: "blueish green"}}, blue: "blue"}
iex> map2 = %{red: "orange", green: %{green: "blue and yellow", yellowish: "more yellow than green"}}
iex> Fxnk.Map.merge_deep_left(map1, map2)
%{red: "red", green: %{green: "green", yellowish: "greenish", with_blue: %{turqoise: "blueish green"}}, blue: "blue"}
Specs
Merges two maps together deeply. If both maps have the same key, the value on the right will be used. If both keys are a map, the maps will be merged together recursively, preferring values on the right.
Example
iex> map1 = %{red: "red", green: %{green: "green", yellowish: "greenish", with_blue: %{turqoise: "blueish green"}}, blue: "blue"}
iex> map2 = %{red: "orange", green: %{green: "blue and yellow", yellowish: "more yellow than green"}}
iex> Fxnk.Map.merge_deep_right(map1, map2)
%{red: "orange", green: %{green: "blue and yellow", yellowish: "more yellow than green", with_blue: %{turqoise: "blueish green"}}, blue: "blue"}
Specs
Merges two maps together, if both maps have the same key, the value on the left will be used.
Example
iex> Fxnk.Map.merge_left(%{red: "red", blue: "blue"}, %{red: "orange", green: "green"})
%{red: "red", blue: "blue", green: "green"}
Specs
Merges two maps together, if both maps have the same key, the value on the right will be used.
Example
iex> Fxnk.Map.merge_right(%{red: "red", blue: "blue"}, %{red: "orange", green: "green"})
%{red: "orange", blue: "blue", green: "green"}
Specs
Return a specific element in a nested map. If the path does not exist, returns the orignal map.
Examples
iex> map = %{one: %{two: %{three: "three" }}}
iex> Fxnk.Map.path(map, [:one, :two, :three])
"three"
iex> Fxnk.Map.path(map, [:one, :two])
%{three: "three"}
iex> Fxnk.Map.path(map, [:one, :four])
%{one: %{two: %{three: "three" }}}
Specs
Like path/2
, but returns the or_value
when the path is not found.
Examples
iex> map = %{one: %{two: %{three: "three" }}}
iex> Fxnk.Map.path_or(map, [:one, :two, :three], :foo)
"three"
iex> Fxnk.Map.path_or(map, [:one, :two], :foo)
%{three: "three"}
iex> Fxnk.Map.path_or(map, [:one, :four], :foo)
:foo
Specs
Accepts a list of args, returns a curried pick/2
.
Examples
iex> pickArgs = Fxnk.Map.pick([:red, :blue, :orange])
iex> pickArgs.(%{ red: "RED", green: "GREEN", blue: "BLUE", yellow: "YELLOW" })
%{red: "RED", blue: "BLUE"}
Specs
pick/2
takes a Map
and a List
of atoms, and returns a map of only the selected keys that exist. It will
return an empty map if passed an empty map or an empty list.
Examples
iex> Fxnk.Map.pick(%{ red: "RED", green: "GREEN", blue: "BLUE", yellow: "YELLOW" }, [:red, :blue, :orange])
%{red: "RED", blue: "BLUE"}
Specs
Accepts a string key
and returns a function that takes a map
. Returns the map's value at key
or nil
.
Examples
iex> getProp = Fxnk.Map.prop("foo")
iex> getProp.(%{"foo" => "foo", "bar" => "bar"})
"foo"
iex> getProp2 = Fxnk.Map.prop(:foo)
iex> getProp2.(%{foo: "foo", bar: "bar"})
"foo"
Specs
Accepts a map and a key. Returns the map's value at key
or nil
Examples
iex> Fxnk.Map.prop(%{"foo" => "foo", "bar" => "bar"}, "foo")
"foo"
iex> Fxnk.Map.prop(%{foo: "foo", bar: "bar"}, :foo)
"foo"
Specs
Curried prop_equals/3
, takes a value, returns a function that accepts a map and a key.
Examples
iex> isFoo = Fxnk.Map.prop_equals("foo")
iex> isFoo.(%{foo: "foo"}, :foo)
true
Specs
Curried prop_equals/3
, takes a key and a value. Returns a function that accepts a map.
Examples
iex> isKeyFoo = Fxnk.Map.prop_equals(:foo, "foo")
iex> isKeyFoo.(%{foo: "foo"})
true
Specs
Accepts a map, key and value. Checks to see if the key on the map is equal to the value.any()
Examples
iex> Fxnk.Map.prop_equals(%{foo: "foo"}, :foo, "foo")
true
iex> Fxnk.Map.prop_equals(%{foo: "bar"}, :foo, "foo")
false
Specs
Accepts a list of keys and returns a function that takes a map. Returns a list of the values associated with the keys in the map.
Examples
iex> getProps = Fxnk.Map.props(["foo", "bar"])
iex> getProps.(%{"foo" => "foo", "bar" => "bar", "baz" => "baz"})
["foo", "bar"]
iex> getProps2 = Fxnk.Map.props([:foo, :bar])
iex> getProps2.(%{foo: "foo", bar: "bar", baz: "baz"})
["foo", "bar"]
Specs
Accepts a map and a list of keys and returns a list of the values associated with the keys in the map.
Examples
iex> Fxnk.Map.props(%{"foo" => "foo", "bar" => "bar", "baz" => "baz"}, ["foo", "bar"])
["foo", "bar"]
iex> Fxnk.Map.props(%{foo: "foo", bar: "bar", baz: "baz"}, [:foo, :bar])
["foo", "bar"]
Specs
Rename a key in a map, takes the map, current key and replacement key. Returns the original map with the updated key.
Example
iex> Fxnk.Map.rename(%{id: "1234"}, :id, :user_id)
%{user_id: "1234"}
iex> Fxnk.Map.rename(%{hello: "world", foo: "foo" }, :foo, :bar)
%{hello: "world", bar: "foo"}
Specs
Rename multiple keys in a map. Takes the original map and a map where the key is the original key and the value is the replacement key.
Example
iex> Fxnk.Map.rename_all(%{user_id: "1234", foo: "foo", bar: "bar"}, %{user_id: :id, bar: :baz})
%{id: "1234", foo: "foo", baz: "bar"}