diff options
author | José Valim <jose.valim@plataformatec.com.br> | 2015-09-25 14:38:24 +0200 |
---|---|---|
committer | José Valim <jose.valim@plataformatec.com.br> | 2015-09-25 14:38:24 +0200 |
commit | deba783731754c7d23a10c8d45a47f3fd7f4447a (patch) | |
tree | d596df25a16178bf0a047025098c4e5fd655bb45 | |
parent | 511cb5d3a58c4ee9900c26f010154200c313a4e5 (diff) | |
download | elixir-deba783731754c7d23a10c8d45a47f3fd7f4447a.tar.gz |
Bring Access.Map temporarily back
-rw-r--r-- | lib/elixir/lib/access.ex | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/lib/elixir/lib/access.ex b/lib/elixir/lib/access.ex index 91f74eca8..b646f6ee9 100644 --- a/lib/elixir/lib/access.ex +++ b/lib/elixir/lib/access.ex @@ -112,3 +112,41 @@ defmodule Access do "could not put/update key #{inspect key} on a nil value" end end + +# Code compiled with Elixir v1.0 will need Access.Map at +# runtime when using *_in function with the map syntax. +# We can remove this later on, but we need to articulate +# with Hex and other archives. +# TODO: Remove me on 1.2 +defmodule Access.Map do + @moduledoc false + + def update!(%{} = map, key, fun) do + case :maps.find(key, map) do + {:ok, value} -> + :maps.put(key, fun.(value), map) + :error -> + raise KeyError, key: key, term: map + end + end + + def update!(other, key, _fun) do + raise ArgumentError, + "could not put/update key #{inspect key}. Expected map/struct, got: #{inspect other}" + end + + def get_and_update!(%{} = map, key, fun) do + case :maps.find(key, map) do + {:ok, value} -> + {get, update} = fun.(value) + {get, :maps.put(key, update, map)} + :error -> + raise KeyError, key: key, term: map + end + end + + def get_and_update!(other, key, _fun) do + raise ArgumentError, + "could not put/update key #{inspect key}. Expected map/struct, got: #{inspect other}" + end +end |