I need to break from foldl. Here is a dummy example how to break from fold when I count sum of values in a list and meet too big value (i.e. 10)
L = [1,2,3,4,10,5,6,7],
Res =
try
lists:foldl(
fun(I, Value) ->
if (I < 10) ->
Value + I;
true ->
throw({too_big_value, Value})
end
end,
0, L)
catch
throw:{too_big_value, Value} -> Value
end,
Res.
I know this example is artificial but are there any nice method to break out fold (I know that fold always scan the whole structure)?
Please note, that i need to retrieve correct data even if i break from fold. In this case i should get data from previous iteration (as it done in my example).