Myles McDonnell
Myles McDonnell

Reputation: 13335

Determine if record with field of certain value exists in list

I need to determine if a record with a given value exists in a list, what is the most efficient way to do this?

Upvotes: 0

Views: 138

Answers (1)

Dmitry Zagorulkin
Dmitry Zagorulkin

Reputation: 8548

i think like this:

[ L || L = #record{state=determined} <- List ].

And the most efficient way is:

lists:any(fun(#record{state=deter}) -> true; (_) -> false end, List).

The first aproach is applicable if your list contains few records with determined field in the list and you'll get it all. The second aproach is the most efficient because we are using standart library and if we'll get nedeed record we'll will stop iteration over the list.

Upvotes: 1

Related Questions