をすべての-predを適用したいと思います。 A related function that may be handyはjuxt
です。これは、単一の引数に多くの機能が適用されます。every-pred
とは異なり、juxt
機能がない短絡を行い、常にリストの各機能を評価し、
((juxt a b c) x) => [(a x) (b x) (c x)]
注意。例:
(ns tst.clj.core
(:use clj.core clojure.test tupelo.test)
(:require [tupelo.core :as t]))
(defn f2 [x] (zero? (mod x 2)))
(defn f3 [x] (zero? (mod x 3)))
(defn f5 [x] (zero? (mod x 5)))
(def f235 (apply juxt [f2 f3 f5]))
これは私たちに結果を与える:
truthy?
機能が
boolean
に似て
(f235 2) => [true false false]
(f235 3) => [false true false]
(f235 5) => [false false true]
(f235 6) => [true true false]
(f235 10) => [true false true]
(f235 15) => [false true true]
(f235 30) => [true true true]
(every? truthy? (f235 15)) => false
(every? truthy? (f235 30)) => true
:
(defn truthy?
"Returns true if arg is logical true (neither nil nor false); otherwise returns false."
[arg]
(if arg true false))
P.S.オリジナルの例では(and (f1 x) (f2 x))
は表示されず、代わりに(and f1 f2) => f1
と記載されています。だからあなたがタイプするとき
(def fs [f1 f2 f3 f4])
((and (first fs) (second fs)) value)
=> ((and f1 f2) value)
=> (f1 value)
これはあなたが求めていた結果を与えません。
'do'ブロックに入れてみましたか? – jmargolisvt
いいえ - それをどうやって行うのか、なぜそうしたいのですか - 副作用はありません –
私は自分の答えを編集しました。私はかなりevery-predがあなたが望むものを正確に行うことを確信しています。 – RedDeckWins