如何在coffeescript中进行类似于python中的元组比较?

时间:2015-09-21 05:53:45

标签: coffeescript

这是我的功能

st3 = (x, y) ->
  console.log "#{x?}, #{y?}"
  if [x?, y?] is [true, true]   # <- this is the line
    'good'
  'bad'

这是输出

true, true
bad

我希望能够像在python中一样进行元组比较。

在python中,if可以大致写成

 if (x, y) == (True, False):
   return 'good'

coffescript if循环被翻译成javascript本身

if ([x != null, y != null] === [true, true]) {
  'good';
}

这就是为什么这不会被评估为真。

有没有其他方法可以在coffeescript中表达它?

1 个答案:

答案 0 :(得分:-1)

如果你想查看你的所有参数是否都不是None,那么我会这样做:

def check_all(*args):
    return all(arg is not None for arg in args)

如果您想检查他们是否全部True(如字面意义为True)那么您可以使用

def check_all(*args):
    return all(arg is True for arg in args)

如果您想要列入一个列表(而不是可变数量的参数,请删除星号。