且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

数字在Clojure中落在间隔内吗?

更新时间:2022-10-19 13:30:36


有比这更好的方法了吗?以下内容:


  

它可以与任意数量的参数一起使用,并检查它们是否有序。有3个参数,这就是您要寻找的。​​ p>

Is there a better way than the following:

(defn in-interval?
  "Returns a predicate that tests if its argument falls in
  the inclusive interval [a, b]."
  [a b]
  (fn [x] (and (>= x a) (<= x b))))

In use:

((in-interval? 5 8) 5.5) ; true
((in-interval? 5 8) 9)   ; false

I don't want to use range, for example, because that constructs a lazy sequence.

Is there a better way than the following:

Yes.

(<= 5 8 8.5)

It works with any number of arguments and check if they are ordered. With 3 arguments, it's what you are looking for.