Groovy没有收藏(Groovy not in collection)
查看列表中是否有东西的常规方式是使用“in”
if('b' in ['a','b','c'])然而,你如何很好地看到某件物品是否不在收藏品中?
if(!('g' in ['a','b','c']))似乎凌乱和“!” 隐藏在随便的一瞥中。 有没有更习惯性的常规方式来做到这一点?
谢谢!
The groovy way to see if something is in a list is to use "in"
if('b' in ['a','b','c'])However how do you nicely see if something is not in a collection?
if(!('g' in ['a','b','c']))Seems messy and the "!" is hidden to the casual glance. Is there a more idiomatic groovy way to do this?
Thanks!
最满意答案
不幸的是,我认为没有很好的语法。 但是你可以使用一个辅助变量来使其更具可读性:
def isMagicChar = ch in ['a', 'b', 'c'] if (!isMagicChar) ...或者,在这种情况下,你可以使用正则表达式:)
if (ch !=~ /[abc]/) ...I think there is no not in pretty syntax, unfortunately. But you can use a helper variable to make it more readable:
def isMagicChar = ch in ['a', 'b', 'c'] if (!isMagicChar) ...Or, in this case, you may use a regex :)
if (ch !=~ /[abc]/) ...更多推荐
发布评论