Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add get! method to dict behaviour

  • Loading branch information...
commit beac96cd551b59601548ded556b223117002bdfb 1 parent edf34ed
@devinus authored
View
7 lib/elixir/lib/binary/dict.ex
@@ -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
View
15 lib/elixir/lib/dict.ex
@@ -105,6 +105,21 @@ defmodule Dict do
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.
View
2  lib/elixir/lib/exception.ex
@@ -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
View
8 lib/elixir/lib/hash_dict.ex
@@ -47,6 +47,14 @@ defmodule HashDict do
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)
end
View
8 lib/elixir/lib/keyword.ex
@@ -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
@@ -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
@@ -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 """
View
8 lib/elixir/lib/ord_dict.ex
@@ -45,6 +45,14 @@ defmodule OrdDict do
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)
end
View
8 lib/elixir/test/elixir/dict_test.exs
@@ -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"
View
4 lib/elixir/test/elixir/keyword_test.exs
@@ -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
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.