Skip to content

Commit

Permalink
Add get! method to dict behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
Devin Torres committed Oct 29, 2012
1 parent edf34ed commit beac96c
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 7 deletions.
7 changes: 7 additions & 0 deletions lib/elixir/lib/binary/dict.ex
Expand Up @@ -38,6 +38,13 @@ defmodule Binary.Dict do
end
end

def get!(dict(data), key) do
case :orddict.find(to_binary(key), data) do
{:ok, value} -> value
:error -> raise(KeyError, key: key)
end
end

def put(dict(data), key, value) do
dict(:orddict.store to_binary(key), value, data)
end
Expand Down
15 changes: 15 additions & 0 deletions lib/elixir/lib/dict.ex
Expand Up @@ -104,6 +104,21 @@ defmodule Dict do
elem(dict, 0).get(dict, key, default)
end

@doc """
Returns the value associated with `key` in `dict`. If `dict` does not
contain `key`, it raises `KeyError`.
## Examples
d = new [a: 1]
Dict.get d, :a #=> 1
Dict.get d, :b #=> raises KeyError[key: :b]
"""
def get!(dict, key) do
elem(dict, 0).get!(dict, key)
end

@doc """
Stores the given `value` under `key` in `dict`.
If `dict` already has `key`, the stored value is replaced by the new one.
Expand Down
2 changes: 1 addition & 1 deletion lib/elixir/lib/exception.ex
Expand Up @@ -217,7 +217,7 @@ defexception ErlangError, [original: nil] do
end
end

defexception Keyword.KeyError, key: nil do
defexception KeyError, key: nil do
def message(exception) do
"key not found: #{inspect exception.key}"
end
Expand Down
8 changes: 8 additions & 0 deletions lib/elixir/lib/hash_dict.ex
Expand Up @@ -46,6 +46,14 @@ defmodule HashDict do
end
end

@doc false
def get!(dict(data), key) do
case :dict.find(key, data) do
{:ok, value} -> value
:error -> raise(KeyError, key: key)
end
end

@doc false
def put(dict(data), key, value) do
dict(:dict.store key, value, data)
Expand Down
8 changes: 4 additions & 4 deletions lib/elixir/lib/keyword.ex
Expand Up @@ -103,7 +103,7 @@ defmodule Keyword do
def get!(keywords, key) when is_atom(key) do
case :lists.keyfind(key, 1, keywords) do
{ ^key, value } -> value
false -> raise(Keyword.KeyError, key: key)
false -> raise(KeyError, key: key)
end
end

Expand Down Expand Up @@ -246,14 +246,14 @@ defmodule Keyword do

@doc """
Updates the key with the given function. If the key does
not exist, raises `Keyword.KeyError`.
not exist, raises `KeyError`.
## Examples
Keyword.update([a: 1], :a, &1 * 2)
#=> [a: 2]
Keyword.update([a: 1], :b, &1 * 2)
#=> Keyword.KeyError
#=> KeyError
"""
def update([{key, value}|keywords], key, fun) do
Expand All @@ -265,7 +265,7 @@ defmodule Keyword do
end

def update([], key, _fun) when is_atom(key) do
raise(Keyword.KeyError, key: key)
raise(KeyError, key: key)
end

@doc """
Expand Down
8 changes: 8 additions & 0 deletions lib/elixir/lib/ord_dict.ex
Expand Up @@ -44,6 +44,14 @@ defmodule OrdDict do
end
end

@doc false
def get!(dict(data), key) do
case :orddict.find(key, data) do
{:ok, value} -> value
:error -> raise(KeyError, key: key)
end
end

@doc false
def put(dict(data), key, value) do
dict(:orddict.store key, value, data)
Expand Down
8 changes: 8 additions & 0 deletions lib/elixir/test/elixir/dict_test.exs
Expand Up @@ -34,6 +34,14 @@ defmodule DictTest.Common do
assert "default" == Dict.get(empty_dict, "first_key", "default")
end

test :get! do
assert 1 == Dict.get!(new_dict, "first_key")
assert 2 == Dict.get!(new_dict, "second_key")
assert_raise KeyError, fn ->
Dict.get!(new_dict, "other_key")
end
end

test :put do
dict = Dict.put(new_dict, "first_key", {1})
assert {1} == Dict.get dict, "first_key"
Expand Down
4 changes: 2 additions & 2 deletions lib/elixir/test/elixir/keyword_test.exs
Expand Up @@ -50,7 +50,7 @@ defmodule KeywordTest do
test :get! do
assert Keyword.get!(create_keywords, :first_key) == 1

error = assert_raise Keyword.KeyError, fn ->
error = assert_raise KeyError, fn ->
Keyword.get!(create_keywords, :unknown)
end

Expand Down Expand Up @@ -99,7 +99,7 @@ defmodule KeywordTest do

test :update do
assert Keyword.update([a: 1], :a, &1 * 2) == [a: 2]
assert_raise Keyword.KeyError, fn ->
assert_raise KeyError, fn ->
assert Keyword.update([a: 1], :b, &1 * 2)
end
end
Expand Down

0 comments on commit beac96c

Please sign in to comment.