具有简写相等逻辑的编程语言,用于将一个变量与多个变量进行比较

发布于 2024-12-01 03:49:38 字数 614 浏览 1 评论 0原文

我很想知道是否有编程语言具有“速记相等结构”(我刚刚提出了这个术语,但不确定如何描述它)。

所以而不是正常的:

如果(X == 1 || X == 2)

一种速记相等检查,如下所示: ?

如果(X == 1 || 2)

我知道有很多 for &反对这种结构。我可以创建函数来执行类似的操作,但我很感兴趣是否有语言可以让您开箱即用。


编辑

感谢 Michael 帮助我澄清事情,我喜欢 Python 的方式。

我会尝试更好地解释,因为看看我上面的问题并不能很好地解释。

而不是检查集合中的某些内容,或者在后台创建集合。

我想知道是否有编程语言只定义一次左手变量,它就会自动为您创建左手变量和右手变量。

所以写这个:

if (X == 1 || 2 || 3)

实际上会创建

if (X == 1 || X == 2 || X == 3)

我意识到这个伪 synatx 并不是很有用,创建集合是一种很好的方法。但想知道它是否存在于任何地方。

I'd be interested to know whether there are programming languages which have "shorthand equality constructs" (I just made that term up, but not sure how to describe it).

So rather than the normal:

if (X == 1 || X == 2)

A kind-of shorthand equality check, like this: ?

if (X == 1 || 2)

I understand that there are many for & againsts for this sort of construct. And that I can create functions to do something similar, but I'd be interested whether there are languages that enable you to do out of the box.


EDIT

Thanks Michael for helping me clarify things, I like the way Python does it.

I'll try and explain better, as looking at my question above it doesn't explain very well.

Rather than checking for something within a collection, or creates a collection in the background.

I'm wondering if there are programming languages that when only defining the left hand variable once, it will automatically create the left and right for you.

So writing this:

if (X == 1 || 2 || 3)

Would actually create

if (X == 1 || X == 2 || X == 3)

I realise this pseudo synatx isn't hugely useful and that creating the collection is a fine way of doing it. But wondered if it exists anywhere.

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

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

发布评论

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

评论(2

自在安然 2024-12-08 03:49:38

我不知道该语法的任何内容,但它与集合成员资格的测试密切相关,许多语言都包含集合成员资格,无论是作为语言构造还是在库中。

例如,考虑一下 Python 的这一点:

x = 1
if x in {1, 2}:
    #do something

它适用于 Python 2.7+,其中有设置文字的语法。早期版本的 Python 会将集合构造为:

x = 1
if x in set([1, 2]):
    #do something

除了语法差异之外,上述基于集合的方法的不同之处还在于,所有可能的值都会被急切地求值。例如,您不能这样做:

x = 1
if x in {1, 2/0}:
    #do something

除以零时会出现错误。

I'm not aware of anything with that syntax, but it is closely related to testing for set membership, which many languages do include, either as a language construct or in a library.

For example, consider this bit of Python:

x = 1
if x in {1, 2}:
    #do something

That works in Python 2.7+, where there is syntax for set literals. Earlier versions of Python would construct the set as:

x = 1
if x in set([1, 2]):
    #do something

Apart from the syntactic difference, the above set-based approach also differs in that all the possible values are eagerly evaluated. You couldn't, for example, do this:

x = 1
if x in {1, 2/0}:
    #do something

You'd get an error for dividing by zero.

蓝天 2024-12-08 03:49:38

伯恩外壳:

case $x in one | 2 | thr33 | f0re) echo less than six;; esac  # spot logic error

Bourne shell:

case $x in one | 2 | thr33 | f0re) echo less than six;; esac  # spot logic error
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文