Shorthand Num >= Low Num 和 < High Num Clojure 的评估
Shorthand Evaluation of Num >= Low Num and < High Num Clojure
遇到这个 post 关于 shorthand 在 Clojure 中使用比较器操作的语句评估。
Does number fall in interval in Clojure?
所以,这对我来说很有意义:
(<= 4 7 7)
=>true
这是有道理的:
(< 4 7 7)
=>false
但是,如果中间数字 大于或等于 到 4,并且 小于 [=27,如果我需要语句评估为 true 怎么办? =] 7?有没有 shorthand 方法来做到这一点并避免做类似的事情:
(and (< 7 7) (>= 7 4))
没有内置的 shorthand,但是...
greater than or equal to 4, and less than 7
也可以写成(<= 4 x 6)
或(< 3 x 7)
。
半开间隔很常见。如果你需要经常使用它们,你可能想写一个函数:
(defn <=< [a b c]
(and (<= a b) (< b c)))
或者也许是一个宏:
(defmacro <=< [a b c]
`(let [b# ~b] (and (<= ~a b#) (< b# ~c))))
遇到这个 post 关于 shorthand 在 Clojure 中使用比较器操作的语句评估。
Does number fall in interval in Clojure?
所以,这对我来说很有意义:
(<= 4 7 7)
=>true
这是有道理的:
(< 4 7 7)
=>false
但是,如果中间数字 大于或等于 到 4,并且 小于 [=27,如果我需要语句评估为 true 怎么办? =] 7?有没有 shorthand 方法来做到这一点并避免做类似的事情:
(and (< 7 7) (>= 7 4))
没有内置的 shorthand,但是...
greater than or equal to 4, and less than 7
也可以写成(<= 4 x 6)
或(< 3 x 7)
。
半开间隔很常见。如果你需要经常使用它们,你可能想写一个函数:
(defn <=< [a b c]
(and (<= a b) (< b c)))
或者也许是一个宏:
(defmacro <=< [a b c]
`(let [b# ~b] (and (<= ~a b#) (< b# ~c))))