只计数集合中的真值 [英] counting only truthy values in a collection

查看:112
本文介绍了只计数集合中的真值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:






我有一个集合,它具有假的和真实的值。我想只计算真实的价值,有办法吗?


(count(1 2 3 nil nil)) => 5



解决方案

如果你想保持真实的值只需要使用 identity function:

  nil false true)))


Possible Duplicate:
how to return only truthy values as the result of a map operation

I have a collection which has falsy and truthy values. I would like to only count the truthy values, is there a way to do that ?

(count (1 2 3 nil nil)) => 5

解决方案

If you want to keep the truthy values just need to use the identity function:

   (count (filter identity '(1 2 3 nil nil false true))) 

这篇关于只计数集合中的真值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆