具有简写相等逻辑的编程语言,用于将一个变量与多个变量进行比较
我很想知道是否有编程语言具有“速记相等结构”(我刚刚提出了这个术语,但不确定如何描述它)。
所以而不是正常的:
如果(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道该语法的任何内容,但它与集合成员资格的测试密切相关,许多语言都包含集合成员资格,无论是作为语言构造还是在库中。
例如,考虑一下 Python 的这一点:
它适用于 Python 2.7+,其中有设置文字的语法。早期版本的 Python 会将集合构造为:
除了语法差异之外,上述基于集合的方法的不同之处还在于,所有可能的值都会被急切地求值。例如,您不能这样做:
除以零时会出现错误。
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:
That works in Python 2.7+, where there is syntax for set literals. Earlier versions of Python would construct the set as:
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:
You'd get an error for dividing by zero.
伯恩外壳:
Bourne shell: