[erlang-questions] lists:keyfind as an alternative to lists:keysearch

Geoff Cant nem@REDACTED
Wed Jan 21 14:23:09 CET 2009


Christian <chsu79@REDACTED> writes:

> On Wed, Jan 21, 2009 at 12:12, Bengt Kleberg <bengt.kleberg@REDACTED> wrote:
>> Greetings,
>>
>> Rickard has my support. I, too, find mixing boolean and none boolean
>> return values surprising.
>>
>> Perhaps {} (empty tuple) is an appropriate return value if nothing is
>> found?
>
> The functions proplists:get_value/2,3 are the less-generalized
> versions of this keyfind-function. So for consistency chose
> 'undefined'. (and provide a Default-argument keyfind function as
> well?)

I heartily second this suggestion - being able to choose the
not_found/undefined/false/error value is a big win.

You can choose a value that makes sense in context:
lists:keyfind(interface, 3, NetConfig, no_interfaces).

Another bonus is that if you are going to replace the not_found result
with some default, being able to supply the default saves you a four
line case statement to do the replacement yourself.

It saves you from writing:
case lists:keyfind(interface, 3, NetConfig) of
     false -> no_interfaces;
     Value -> Value
end

It would be easy enough to supply a lists:keyfind/4 BIF and a
lists:keyfind/3 function that wraps keyfind/4 with the 'false' argument.

Cheers,
--Geoff Cant




More information about the erlang-questions mailing list