Erlang:从函数返回

发布于 2024-08-13 16:24:34 字数 202 浏览 4 评论 0原文

我有一个函数,其中有一系列个案陈述。

case ... of
     ...
end,

case ... of
     ...
end,

...

我想当某个 case 语句中出现特定 case 条件时立即从函数返回 - 这样就不会检查下一个 case 语句,并且函数只是退出/返回 我该怎么做?

I have a function in which I have a series of individual case statements.

case ... of
     ...
end,

case ... of
     ...
end,

...

etc.

I want to return from the function immediately when a particular case condition occurs in one of the case statements - so that the next case statement is not checked, and the function just exits/returns. How do I do that?

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

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

发布评论

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

评论(6

丑丑阿 2024-08-20 16:24:34

我建议您进行重构,以充分利用 Erlang 及其模式匹配功能。

没有 return 运算符。另外,一个鲜为人知的事实是您可以执行以下操作:

Return=case ... of

case 语句可以具有“返回”值。

I would suggest you refactor to harness the full power of Erlang and its pattern matching abilities.

There isn't a return operator. Also, a little known fact is you can do something like:

Return=case ... of

a case statement can have a "return" value.

穿透光 2024-08-20 16:24:34

模式匹配是重构 case 语句的好方法 - 您可以执行类似的操作

testcase(1, X, Y) -> .... passed1;
testcase(2, X, Y) -> .... passed2;
testcase(N, X, Y) when N > 2 -> .... passedlarge;
testcase(_, X, Y) -> defaultcase.

,然后您的 case 语句简单地概括为:(

X = testcase(Number, Param1, Param2).

在这个人为的示例中,X 将是 Passed1、Passed2、PassedLarge 或 DefaultCase)

Pattern matching is a good way to refactor a case statement - you can do something like this

testcase(1, X, Y) -> .... passed1;
testcase(2, X, Y) -> .... passed2;
testcase(N, X, Y) when N > 2 -> .... passedlarge;
testcase(_, X, Y) -> defaultcase.

and then your case statement simply wraps up to:

X = testcase(Number, Param1, Param2).

(X will be either passed1, passed2, passedlarge or defaultcase in this contrived example)

独留℉清风醉 2024-08-20 16:24:34

Erlang 没有return 运算符。您需要将代码重构为更小的函数。

您的原始代码有两个与逗号运算符链接的 case 表达式。我认为您在要保留的第一个 case 表达式中存在一些副作用。下面,我使用一个虚构的 return 运算符:

case ... of
  P1 -> return E1;
  P2 -> E2;
end,

case ... of
  ...
end

像这样的表达式可以使用小函数和与类似这样的模式匹配转换为真正的 Erlang 代码:

case1(P1, ...) -> E1;
case1(P2, ...) -> E2, case2(...).
case2(...) -> ...

免责声明:自从我使用它以来已经过去了 10 年。编写了 Erlang 代码,所以我的语法可能不正确。

Erlang does not have a return operator. You will need to refactor your code into smaller functions.

Your original code has two case expressions chained with the comma operator. I presume you have some side effects in the first case expression that you want to preserve. Below, I'm using an imaginary return operator:

case ... of
  P1 -> return E1;
  P2 -> E2;
end,

case ... of
  ...
end

An expression like this can be converted to real Erlang code using small functions and pattern matching with something resembling this:

case1(P1, ...) -> E1;
case1(P2, ...) -> E2, case2(...).
case2(...) -> ...

Disclaimer: It's been 10 years since I've written Erlang code, so my syntax may be off.

伴我心暖 2024-08-20 16:24:34

在 Erlang 中,您只需使用模式匹配来触发适当的函数。如果您有太多的子句需要涵盖和处理,我还建议稍微重构一下代码。

In Erlang you just use the pattern matching to trigger the appropriate function. If you have too many clauses to cover and deal with I would also suggest to refactor the code a little bit.

空心↖ 2024-08-20 16:24:34

一种方法是将 case 语句级联:

my_fun(X) ->
  case cond1(X) of
    true -> ret1;
    _ ->
      case cond2(X) of
        true -> ret2;
        _ ->
          ...
      end
  end.

另一种方法是将 case 语句分成子句:

my_fun(X) ->
  my_fun(cond1, X).

my_fun(cond1, X) ->
  case cond1(X) of
    true -> ret1;
    _    -> my_fun(cond2, X)
  end;

my_fun(cond2, X) ->
  case cond2(X) of
    true -> ret2;
    _    -> my_fun(cond3, X)
  end;

...

One way is to cascade your case statements:

my_fun(X) ->
  case cond1(X) of
    true -> ret1;
    _ ->
      case cond2(X) of
        true -> ret2;
        _ ->
          ...
      end
  end.

Another one is to separate your case statements into clauses:

my_fun(X) ->
  my_fun(cond1, X).

my_fun(cond1, X) ->
  case cond1(X) of
    true -> ret1;
    _    -> my_fun(cond2, X)
  end;

my_fun(cond2, X) ->
  case cond2(X) of
    true -> ret2;
    _    -> my_fun(cond3, X)
  end;

...
毁虫ゝ 2024-08-20 16:24:34

使用 catch/throw

调用者说:

X = (catch foo(A, B)).

then write

foo(A, B) ->
    case ... of
     ...throw(X) ..
    end,

    case ... of
     ... throw (Y)
    end,
    ...

这通常被认为是不好的编程实践 - 因为程序有多个
出口点,很难摸索

use catch/throw

The caller says:

X = (catch foo(A, B)).

then write

foo(A, B) ->
    case ... of
     ...throw(X) ..
    end,

    case ... of
     ... throw (Y)
    end,
    ...

This is generally considered poor programming practice - since the program has multiple
exit points and is difficult to grock

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