Erlang:打破列表:foreach“循环”

发布于 2024-08-12 21:15:44 字数 137 浏览 5 评论 0原文

我在 Erlang 中有一个元素列表,我使用lists:foreach 来遍历列表中的元素。有没有办法在遍历过程中摆脱这个“foreach 循环”。例如:假设如果我在列表 [2, 4, 5, 1, 2, 5] 中遇到“1”,我想停止进一步遍历列表。我该怎么做?

I have a list of elements in Erlang, and I'm using lists:foreach to traverse through the elements in the list. Is there a way to break out of this "foreach loop" in the middle of the traversal. For eg: suppose I want to stop traversing the list further if I encounter a '1' in the list [2, 4, 5, 1, 2, 5]. How do I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

李白 2024-08-19 21:15:44

另一种方法是使用 throwcatch

catch lists:foreach(
        fun(1) ->
                throw(found_one);
           (X) ->
                io:format("~p~n", [X])
        end,
        [2, 4, 5, 1, 2, 5]).

在 shell 中运行时,输出:

2
4
5
found_one

编辑:根据大众的需求,一个更精确版本,仅捕获您想要捕获的内容:

try lists:foreach(
        fun(1) ->
                throw(found_one);
           (X) ->
                io:format("~p~n", [X])
        end,
        [2, 4, 5, 1, 2, 5])
catch
    throw:found_one ->
        found_one
end.

Another way to do it is to use throw and catch:

catch lists:foreach(
        fun(1) ->
                throw(found_one);
           (X) ->
                io:format("~p~n", [X])
        end,
        [2, 4, 5, 1, 2, 5]).

When run in the shell, this outputs:

2
4
5
found_one

EDIT: By popular demand, a more precise version that catches only what you want to catch:

try lists:foreach(
        fun(1) ->
                throw(found_one);
           (X) ->
                io:format("~p~n", [X])
        end,
        [2, 4, 5, 1, 2, 5])
catch
    throw:found_one ->
        found_one
end.
﹏半生如梦愿梦如真 2024-08-19 21:15:44
traverse(Liste) ->
 traverse(Liste, []).

traverse([], Acc) ->
 Acc;    

traverse([1|_], Acc) ->
 Acc;

traverse([H|T], Acc) ->
 % do something useful here maybe?
 traverse(T, Acc).

当然,这是非常粗略的例子。

traverse(Liste) ->
 traverse(Liste, []).

traverse([], Acc) ->
 Acc;    

traverse([1|_], Acc) ->
 Acc;

traverse([H|T], Acc) ->
 % do something useful here maybe?
 traverse(T, Acc).

Of course this is very rough example.

绅刃 2024-08-19 21:15:44

lists 模块中有很多不错的功能:

lists:foreach(fun(E) -> do_something(E) end,
    lists:takewhile(fun(E) -> E =/= 1 end, List)).

或者更有效但不太好

lists:takewhile(fun(1) -> false;
                   (E) -> do_something(E), true
                end, List)

There are many nice functions in lists module:

lists:foreach(fun(E) -> do_something(E) end,
    lists:takewhile(fun(E) -> E =/= 1 end, List)).

or more effective but less nice

lists:takewhile(fun(1) -> false;
                   (E) -> do_something(E), true
                end, List)
余生一个溪 2024-08-19 21:15:44

我遇到了同样的问题并通过以下方式解决:

-module(foreach_until).
-export([foreach_until/3]).

foreach_until(Action, L, Judge)     ->
    lists:reverse(foreach_until(Action, L, Judge, []))
    .

foreach_until(_, [], _, Result) ->
    Result
    ;

foreach_until(Action, [H | T], Judge, Result)   ->
    case Judge(H) of 
        true    -> Result;
        false   -> foreach_until(Action, T, Judge, [Action(H) | Result])
    end
    .

下面是一个示例解释如何使用:

60> foreach_until:foreach_until(fun(X) -> X*X end, [1,2,3,4], fun(X)-> X >= 3 end).
[1,4]

I meet the same problem and solve it by this way:

-module(foreach_until).
-export([foreach_until/3]).

foreach_until(Action, L, Judge)     ->
    lists:reverse(foreach_until(Action, L, Judge, []))
    .

foreach_until(_, [], _, Result) ->
    Result
    ;

foreach_until(Action, [H | T], Judge, Result)   ->
    case Judge(H) of 
        true    -> Result;
        false   -> foreach_until(Action, T, Judge, [Action(H) | Result])
    end
    .

Below is an example explained how to use:

60> foreach_until:foreach_until(fun(X) -> X*X end, [1,2,3,4], fun(X)-> X >= 3 end).
[1,4]
箹锭⒈辈孓 2024-08-19 21:15:44

列表:全部?

do_something(A) ->
   case A of 
      1 ->
         false;
      _ ->
         true
   end.

IsCompleted = lists:all(do_something(A), [2, 4, 5, 1, 2, 5]),

每当 do_something 返回 false 时就会中断并在 IsCompleted 中返回结果。

lists:all?

do_something(A) ->
   case A of 
      1 ->
         false;
      _ ->
         true
   end.

IsCompleted = lists:all(do_something(A), [2, 4, 5, 1, 2, 5]),

Will break out whenever do_something returns false and return the results in IsCompleted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文