Skip to content

Instantly share code, notes, and snippets.

View noelworden's full-sized avatar

Noel Worden noelworden

View GitHub Profile
iex> Map.replace!(example_map, :delta, "five")
** (KeyError) key :delta not found in: %{alpha: "one", bravo: "two", charlie: "three"}
(stdlib 3.12.1) :maps.update(:delta, "five", %{alpha: "one", bravo: "two", charlie: "three"})
iex> Map.update!(example_map, :charlie, fn charlie -> charlie <> " and four" end)
%{alpha: "one", bravo: "two", charlie: "three and four"}
iex> Map.update!(example_map, :delta, fn delta -> delta <> " and four" end)
** (KeyError) key :delta not found in: %{alpha: "one", bravo: "two", charlie: "three"}
(stdlib 3.12.1) :maps.get(:delta, %{alpha: "one", bravo: "two", charlie: "three"})
(elixir 1.10.3) lib/map.ex:272: Map.update!/3
iex> Map.update(example_map, :charlie, "three", fn charlie -> charlie <> " and four" end)
%{alpha: "one", bravo: "two", charlie: "three and four"}
iex> Map.update(example_map, :delta, "four", fn delta -> delta <> " and five" end)
%{alpha: "one", bravo: "two", charlie: "three", delta: "four"}
iex> example_map |> Map.put(:alpha, "eight") |> Map.put(:bravo, "nine")
%{alpha: "eight", bravo: "nine", charlie: "three"}
iex> example_map |> Map.replace!(:alpha, "eight") |> Map.replace!(:bravo, "nine")
%{alpha: "eight", bravo: "nine", charlie: "three"}
iex> %{example_map | alpha: "eight", bravo: "nine"}
%{alpha: "eight", bravo: "nine", charlie: "three"}
[kind: :less_than_or_equal_to, number: 0, validation: :number]
@noelworden
noelworden / week_june22_map.ex
Last active July 17, 2020 23:39
#blog_snippets
iex> Map.new([kind: :less_than_or_equal_to, number: 0, validation: :number])
%{kind: :less_than_or_equal_to, number: 0, validation: :number}