Функция number?
звучит так, как вам нужно.Может быть, тест (and (number? arg1) (number? arg2))
.
Некоторое время назад Брайан Карпер предложил макрос и серию функций для использования при проверке различных типов числовых аргументов:
;; Suggested by Brian Carper at:
;;/1196647/dolzhen-li-ya-ispolzovat-funktsiy-ili-makros-dlya-proverki-argumentov-v-clojure
(defmacro assert* [val test]
`(let [result# ~test]
(when (not result#)
(throw (IllegalArgumentException.
(str "Test failed: " (quote ~test)
" for " (quote ~val) " = " ~val))))))
(defmulti validate* (fn [val test] test))
(defmethod validate* :prob [x _]
(assert* x (and (number? x) (pos? x) (<= x 1.0))))
(defmethod validate* :posint [x _]
(assert* x (and (integer? x) (pos? x))))
(defmethod validate* :non-negint [x _]
(assert* x (and (integer? x) (not (neg? x)))))
(defmethod validate* :posnum [x _]
(assert* x (and (number? x) (pos? x))))
(defmethod validate* :percentage [x _]
(assert* x (and (number? x) (pos? x) (<= x 100))))
(defmethod validate* :numseq [x _]
(assert* x (and (not (empty? x)) (seq? x) (every? number? x))))
(defmethod validate* :nonzero-numseq [x _]
(assert* x (and (not (empty? x)) (seq? x) (every? #(and (number? %) (not (zero? %))) x))))
(defmethod validate* :posint-seq [x _]
(assert* x (and (not (empty? x)) (seq? x) (every? #(and (integer? %) (pos? %)) x))))
(defmethod validate* :prob-seq [x _]
(assert* x (and (not (empty? x)) (seq? x) (every? #(and (number? %) (pos? %) (<= % 1.0)) x))))
(defmethod validate* :default [x _]
(throw (IllegalArgumentException.
(str "Unrecognized validation type"))))
(defn validate [& tests]
(doseq [test tests] (apply validate* test)))
гибкий в моем опыте.Как видите, мультитемод легко распространить на новые тесты.
Использование будет выглядеть примерно так:
(defn f [arg1 arg2]
"arg1 must be a positive integer, arg2 must be a positive number"
(validate [arg1 :posint] [arg2 :posnum])
...
)