Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Access protocol for all lists of tuples

  • Loading branch information...
commit 055f47feaf35e59877215c09954888ed53029f9b 1 parent e2e1747
@devinus authored
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
Please sign in to comment.
Something went wrong with that request. Please try again.