Breaking out of a foldl

Juan Jose Comellas juanjo@REDACTED
Fri May 29 18:16:14 CEST 2009


Several times I've come across the need of a variant of foldl/foldr where I
can break out of the iteration before going over allthe elements of a list.
Has anybody thought of adding a function like the one below to the lists
module?

bfoldl(Fun, Acc0, [Head | Tail]) ->
    case Fun(Head, Acc0) of
        {ok, Acc} ->
            bfoldl(Fun, Acc, Tail);
        {break, _Acc} = Result ->
            Result;
        break ->
            {break, Acc0}
    end;
bfoldl(_Fun, Acc0, []) ->
    {ok, Acc0}.

Where a function can return {ok, Acc} to continue iterating and break /
{break, Acc} to interrupt it.


More information about the erlang-questions mailing list