Fxnk.Map (Fxnk v0.1.4) View Source

Fxnk.Map are functions that work with maps.

Link to this section Summary

Functions

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.

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.

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.

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

assemble(%{required(any()) => function()}) :: (map() -> map())

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

assemble(map(), %{required(any()) => function()}) :: any()

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

combine((map() -> map())) :: (map() -> 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.

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

combine(map(), (map() -> map())) :: 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.

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"}
Link to this function

combine_with(map, function, combining_function)

View Source

Specs

combine_with(map(), (map() -> map()), (map(), map() -> map())) :: map()

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

has_prop?(atom() | String.t()) :: (map() -> boolean())

Curried has_prop?/2

Examples

iex> hasFoo = Fxnk.Map.has_prop?(:foo)
iex> hasFoo.(%{foo: 'foo'})
true
iex> hasFoo.(%{bar: 'bar'})
false
Link to this function

has_prop?(map, property)

View Source

Specs

has_prop?(map(), atom() | String.t()) :: boolean()

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
Link to this function

merge_deep_left(map1, map2)

View Source

Specs

merge_deep_left(map(), map()) :: map()

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"}
Link to this function

merge_deep_right(map1, map2)

View Source

Specs

merge_deep_right(map(), map()) :: map()

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

merge_left(map(), map()) :: map()

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

merge_right(map(), map()) :: map()

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

path(map(), [binary() | atom()]) :: map() | any()

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" }}}
Link to this function

path_or(map, path_array, or_value)

View Source

Specs

path_or(map(), [binary() | atom()], any()) :: map() | any()

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

pick([atom(), ...]) :: (map() -> map())

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(map(), [atom(), ...]) :: map()

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

prop(atom() | binary()) :: (map() -> any())

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

prop(map(), atom() | binary()) :: any()

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

prop_equals(any()) :: (map(), atom() | String.t() -> boolean())

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

prop_equals(atom() | binary(), any()) :: (map() -> boolean())

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
Link to this function

prop_equals(map, key, value)

View Source

Specs

prop_equals(map(), atom() | binary(), any()) :: boolean()

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

props([atom() | binary(), ...]) :: (map() -> [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.

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

props(map(), [atom() | binary(), ...]) :: [any(), ...]

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"]
Link to this function

rename(map, key, new_key)

View Source

Specs

rename(map(), String.t() | atom(), String.t() | atom()) :: map()

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"}
Link to this function

rename_all(map, renames)

View Source

Specs

rename_all(map(), map()) :: map()

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"}