2013-12-11 17 views
4

我有一系列的值。我想知道他們中的任何一個是否評估爲真。如何在Clojure中應用'或'?

(def input [nil nil nil 1 nil 1]) 

我想要做這樣的事情:

(apply or input) 

但我不能因爲oris a macro不是一個函數。同樣,這將不起作用

(reduce or input) 

我寫我自己的

(def orfn #(if %1 %1 %2)) 

現在這個工作

(reduce orfn input) 

一樣這種方法稍有不同,雖然它只是試驗nil

(not (every? nil? input)) 

apply or或同等產品的「正確」方式是什麼?

+0

在一般情況下[這可以幫助](http://stackoverflow.com/questions/9273333/in-clojure-how-to-apply-a-macro-to-a-list) – soulcheck

+0

非常感謝@soulcheck 。我認爲,我遇到的問題的最佳解決方案是「一些」。 – Joe

回答

9

您可以使用someidentity做到這一點:

(some identity [nil nil nil 1 nil 1]) 
=> 1 

some返回對一個函數序列中的第truthy值,所以這個工作,因爲1是truthy值。

4

(some identity [nil nil nil nil 1 nil 1])