[erlang-questions] How to return first {Key, Value} from Map where Predicate is true

Fred Hebert mononcqc@REDACTED
Thu Jan 8 19:02:30 CET 2015

On 01/08, Imants Cekusins wrote:
> An iterator function with a break option would be very useful, too.
> Even the current maps:fold function could be modified to allow break:
> fold(Fun, Init, Map) -> Acc
> Types:
> Fun = fun((K, V, AccIn) -> AccOut)
> modify to:
> Fun = fun((K, V, AccIn) -> AccOut | {break, AccOut})
> ?

        maps:fold(fun(K, V, Map) -> ..., throw({found, {K,V}) end, Init, Map0),
        {found, Val} -> Val

This gives you a similar result without anything else required.
Alternatively you could just go 'catch maps:fold(...)', but in case of
exceptions that aren't throws, you may yield unexpected results (a
stacktrace) whereas the 'try ... catch' would just have crashed.

More information about the erlang-questions mailing list