【发布时间】:2017-06-20 08:15:08
【问题描述】:
我正在创建一个简单的服务,它接收电子邮件地址并从用户列表中找到用户。
这是一个包含用户列表的简化版本。我想根据用户的电子邮件地址提取用户。
def endpoint do
[%{email: "foo@example.org", account_type: "full"},
%{email: "bar@earxample.org", account_type: "standard"},
%{email: "baz@example.org", account_type: "full"}]
end
def get_by_email(user, email) do
user |> Map.get(:email)
end
def dev_endpoint(email) do
endpoint
|> Enum.map(&get_by_email(email)/1)
end
def show(conn, %{"id" => email}) do
response = dev_endpoint(email)
json(conn, %{"email" => response})
end
所以本质上:
dev_endpoint("foo@example.org")
应该返回:
%{email: "foo@example.org", account_type: "full"}
我知道我的捕获语法有问题,但我尝试了各种不同的迭代,但没有成功。
【问题讨论】:
-
也许我不能完全理解你想要什么但
endpoint |> Enum.find(&(email== Map.get(&1,:email)))不够吗?