Easy way to break foldl

会有一股神秘感。 提交于 2019-12-12 08:51:52

问题


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).


回答1:


You're doing it right, using a throw with try/catch for nonlocal return. If the function looked at the return value from the fun to decide whether or not to continue, it wouldn't be foldl anymore.




回答2:


Just curious, what is the point of using foldl here? If you need to break out, use recursion, foldl is not designed for it.

main([]) ->
  L = [1,2,3,4,5,10,6,7],

   io:format("[~w]", [s(L, 0)]).

s([], S) ->
  S;

s([H|T], S) ->
  if (H < 10) ->
    s(T, S + H);
  true ->
    S
  end.

Update:

Another options is to use takewhile:

lists:foldl(fun(E, A) -> A + E end, 0, lists:takewhile(fun(E) -> E < 10 end, L))


来源:https://stackoverflow.com/questions/8412446/easy-way-to-break-foldl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!