Skip to content
Browse files

Access protocol for all lists of tuples

  • Loading branch information...
1 parent e2e1747 commit 055f47feaf35e59877215c09954888ed53029f9b @devinus committed
Showing with 10 additions and 2 deletions.
  1. +7 −2 lib/elixir/lib/access.ex
  2. +3 −0 lib/elixir/test/elixir/access_test.exs
View
9 lib/elixir/lib/access.ex
@@ -31,8 +31,13 @@ defimpl Access, for: List do
"""
- def access(list, atom) when is_atom(atom) do
- Keyword.get(list, atom)
+ def access([], _key), do: nil
+
+ def access(list, key) do
+ case :lists.keyfind(key, 1, list) do
+ { ^key, value } -> value
+ false -> nil
+ end
end
end
View
3 lib/elixir/test/elixir/access_test.exs
@@ -7,6 +7,9 @@ defmodule AccessTest do
assert [foo: :bar][:foo] == :bar
assert [foo: [bar: :baz]][:foo][:bar] == :baz
assert [foo: [bar: :baz]][:fuu][:bar] == nil
+ assert [{"foo", :bar}]["foo"] == :bar
+ assert [{"foo", [{"bar", :baz}]}]["foo"]["bar"] == :baz
+ assert [{"foo", [{"bar", :baz}]}]["fuu"]["bar"] == nil
end
test :nil do

0 comments on commit 055f47f

Please sign in to comment.
Something went wrong with that request. Please try again.