[erlang-questions] Breaking out of a foldl

Joe Armstrong <>
Mon Jun 1 09:26:15 CEST 2009


On Sat, May 30, 2009 at 6:19 PM, Mazen Harake
<> wrote:
> Because it is a hack?
>
> a "fold_until" would be much smoother.

Uuugh - smoothness?????????????

Why have two functions when one (the existing foldl) is perfectly adequate?

The use of throw to abnormally terminate a recursion is not a hack.
That is what
catch-throw was designed to do. exit/1 is for raising errors, throw/1 is for
abrupt termination of a computation.

> Consider this +1 to the set of people that wants this function in the lists
> module :)

Wirth (the blessed) said something like (paraphrased) every time we
add something
to a language we should ask *what can we remove*. If we just add stuff
to languages
(or libraries) - without removing stuff, we add additional complexity
so we violate
the principle of being as simple as possible.

I am often horrified by libraries that offer dozens of different ways
to do essentially the
same thing - this makes the documentation almost unreadable (since it is unclear
which the kernel methods are) and makes learning very difficult.

Libraries should provide a small set of primitive parts which can be
glued together
to solve a large number of problems. If the set of parts is large then
learning the library will be very difficult. If you don't know all the
functions in a library then
programming using the library will be painfully slow since you will
have to google
your way through large volumes of documentation.

If you really really want fold_until then I suggest you make your own
personal library
(mylib.erl) where you put fold_until (and every other additional
function that you think
is missing from the standard libraries) - then one day when you are
satisfied that
your library has proved useful you can publish it.

Exactly what should be in the standard libraries is an extremely
difficult problem-
I think they should contain small sets of orthogonal functions and err
on the side
of generality. If two function do more of less the same thing one of
them should be removed.

/Joe



>
> /M
>
> David Mercer wrote:
>>
>> Why not use throw and catch?  E.g.:
>>
>>  1> SumOr10 = fun(X, Y) when X + Y >= 10 -> throw(10); (X, Y) -> X + Y
>> end.
>>  #Fun<erl_eval.12.113037538>
>>  2> catch lists:foldl(SumOr10, 0, [1,2,3]).
>>  6
>>  3> catch lists:foldl(SumOr10, 0, [1,2,3,4]).
>>  10
>>  4> catch lists:foldl(SumOr10, 0, [1,2,3,4,5]).
>>  10
>>
>>
>>>
>>> -----Original Message-----
>>> From:  [mailto:] On
>>> Behalf Of Juan Jose Comellas
>>> Sent: Friday, May 29, 2009 11:16 AM
>>> To: Erlang Questions
>>> Subject: [erlang-questions] Breaking out of a foldl
>>>
>>> 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.
>>>
>>
>>
>> ________________________________________________________________
>> erlang-questions mailing list. See http://www.erlang.org/faq.html
>> erlang-questions (at) erlang.org
>>
>>
>
>
> ________________________________________________________________
> erlang-questions mailing list. See http://www.erlang.org/faq.html
> erlang-questions (at) erlang.org
>
>


More information about the erlang-questions mailing list