怎么写“a==b”? X:Y”换句话说,在 Erlang 中如何编写 C 风格的三元运算符?
Is there a good way to write code like this in Erlang?
A == B ? X : Y
below is ruby-style code. This is also known as a ternary operator.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
解释
三元运算符
_ ? 的原因_ : _
存在于许多语言中是因为它们有两个语法类:语句和表达式。由于 if-then-else 结构通常属于语句类,因此当您输入表达式时,无法使其发挥作用。因此您添加_ ? _ : _
运算符到表达式类。正如另一篇文章所述,您可以采用
a == b 吗? true : false
并只写a == b
,但这并不能解释我们可能有a == b 的一般情况? X : Y
用于任意表达式X
和Y
。另请注意,a == b
在 Erlang 中始终为false
,因此您可能会认为真正要做的是将整个表达式替换为false
代码>.幸运的是,Erlang 与大多数函数式语言一样,只有一种语法类:表达式。因此,您可以使用 X -> case a == b ...; Y-> ... end 在函数中的任何位置,包括其他表达式。换句话说,三元
_ ? _ : _
运算符在 Erlang 中是多余的,因为case
已经可以工作了。举个例子:
假设我们要返回一个简单的 proplist,并且需要进行一些计算,
但是由于
case
构造是一个表达式,因此我们可以将其内联:并完成该操作。
为什么我不提倡使用 IF
Erlang 中的
if .. end
结构通常不是你想要的。在这种情况下,您想要仔细检查值a == b
,它可能会产生两个输出true
或false
之一。在这种情况下,case
表达式更为直接。如果您必须检查多个不同的测试并选择第一个匹配,则更好地使用if
,而我们在这里只需要进行一个测试。Explanation
The reason the ternary operator
_ ? _ : _
exists in many languages is due to the fact that they have two syntactic classes: Statements and Expressions. Since if-then-else constructions usually belong the the statement-class, there is no way to get that working for when you are entering an expression. Hence you add the_ ? _ : _
operator to the expression class.As another post states, you can take
a == b ? true : false
and just writea == b
, but that does not explain the general case where we may havea == b ? X : Y
for arbitrary expressionsX
andY
. Also note thata == b
is alwaysfalse
in Erlang, so you could argue that the real thing to do is to replace the whole expression withfalse
.Luckily, Erlang, as is the case for most functional languages, have one syntactic class only, expressions. Hence you can use
case a == b of X -> ...; Y -> ... end
in any place in a function, other expressions included. In other words, the ternary_ ? _ : _
operator is redundant in Erlang since thecase
already works.An example:
Suppose we are to return a simple proplist and we have some computation we need to do
But since the
case
construction is an expression, we can just inline it:and be done with the thing.
Why I am not advocating the use of IF
the
if .. end
construction in Erlang is usually not what you want. You want to scrutinize a valuea == b
in this case and it can yield one of two outputstrue
orfalse
. In that case thecase
-expression is more direct. Theif
is better used if you have to check for multiple different tests and pick the first matching, whereas we only have a single test to make here.我们使用这样的宏:
然后在您的代码中编写:
We use macro like this:
Then in your code you write:
如果您问如何编写类似
A == B 的内容? X : Y
作为一个if
表达式,它也可以写成一个
case
表达式:或者
If you're asking how to write something like
A == B ? X : Y
as anif
expression, it'sYou can also write it as a
case
expression:or
由于a == b ? true : false 映射到
a == b
,您也可以在 Erlang 中使用a == b
。Since
a == b ? true : false
maps toa == b
, you can usea == b
in Erlang also.您可以使用“if”,就像
特殊的 ?: 形式似乎没有必要。您当然可以使用 true/false 作为返回值,但我认为您的意思是更通用的形式,因为那种形式是无用的( A == B 做同样的工作)。
You can use 'if' like that
special ?: form seems to be not necessary. You can of course use true/false as return value but I think you meant more general form, as that one would be useless (A == B does the same job).
@Gabe 的答案是最简洁且(据我所知)惯用的。 C 表达式
((A==B) ? X : Y)
直接映射到 Erlang 表达式,但是,这比 C 版本的代码多得多。你可能应该将它包装成一个方便的函数 - 有人应该告诉我这是否已经存在于 Erlang 标准库中!
那么你的 C 表达式就变成了简单的
iff(A == B, X, Y)
。 但是,请注意!就像 C 一样,Erlang 急切地计算函数参数。如果X
或Y
有副作用或评估成本昂贵,则iff
将不等同于内联case< /代码> 表达式。
@Gabe's answer is the most concise, and (as far as I can tell) idiomatic. The C expression
((A==B) ? X : Y)
maps directly to the Erlang expressionHowever, this is vastly more code than the C version. You should probably wrap it up into a convenience function — and someone should tell me if this already exists in the Erlang standard libraries!
Then your C expression becomes simply
iff(A == B, X, Y)
. However, beware! that just like C, Erlang eagerly evaluates function arguments. IfX
orY
have side effects or are expensive to evaluate, theniff
will not be equivalent to an in-linecase
expression.