[erlang-questions] Breaking out of a foldl

Richard O'Keefe <>
Tue Jun 2 04:32:20 CEST 2009


One thing that occurs to me is that the sample code we were shown
is so special-purpose that I don't think I'd want to use it.

Another thing that occurs to me is that the code was so simple that
the point of putting it in the library would have to be the readability
advantage of having one shared name that we could all look up in the
same place, rather than anything else.

Let's see how I would do it.
A function is given an accumulator and a new value,
and either returns
	{continue,Acc'}
or	{stop,Acc'}
So

early_termination_foldl(_, Acc, []) ->
     Acc;
early_termination_foldl(Fun, Acc0, [X|Xs]) ->
     case Fun(Acc0, X)
       of {continue,Acc1} -> early_termination_foldl(Fun, Acc1, Xs)
        ; {stop,Final} -> Final
     end.

Here we already have a disagreement with the original posting:
if the Fun wants to stop with the current accumulator value,
it can just return {stop,Acc0}.  Why does there need to be a
'break' option just to handle that case?

Or of course we could turn the whole thing inside out.

xfoldl(_, Acc, []) -> Acc;
xfoldl(Fun, Acc0, [X|Xs]) ->
     Fun(Acc0, X, fun (Acc1) -> xfoldl(Fun, Acc1, Xs) end).

Here the Fun "returns {continue,Acc1}" by invoking
Continuation(Acc1), and "returns {stop,Final}" by
returning Final.  [Could this be called bringing a trick
out of RABBIT?]

So now we have three different versions of the same "early
termination foldl", and that's not counting anything using
catch/throw (which, pace the friends of Java, comes from the
Lisp world with the meaning of "long-distance return", not
with the meaning "exception").

I think it's premature to put something in the library before
we have a good idea what that something should be.  We DON'T have
to have agreement about what the BEST thing is, just something
that enough people regard as useful and straightforward.  (If you
got the rather weak joke about RABBIT, you probably think that
xfoldl is straightforward...)




More information about the erlang-questions mailing list