i have ETS table that have key values scheme like this:
users = :ets.tab2list(:users)
IO.inspect users # printing
[
{"3eadd68495d",
%UserType{
name: "John",
id: "3eadd68495d",
status: "Free"
}},
{"234a34495d",
%UserType{
name: "Steve",
id: "234a34495d",
status: "Free"
}},
{"9add68495d",
%UserType{
name: "Mike",
id: "9add68495d",
status: "Busy"
}}
]
And I want to get one id of any element, that have status "Free".
I'm already tried to get value using loop
users = :ets.tab2list(:users)
for user <- users do
userValue = user |> elem(1)
if userValue.status === "Free" do
userValue.id
end
end
but it returns multiple id values (3eadd68495d234a34495d), not one
i need smth like "break" after if userValue.status === "Free" do userValue.id
but i have no idea how to use this in Elixir.
1条答案
按热度按时间xkrw2x1b1#
For the single value, one should use
Enum.reduce_while/3
.or as pointed out by @sabiwara in comments, use
Enum.find_value/2
.I would not recommend this, but one might do
try/catch
to “break” thefor
comprehension as shown below.:ets.select/2
as described in https://write.as/yuriploc/ets-dets-match-patterns-and-specificationsSomewhat alongside the following lines should do