Does number fall in interval in Clojure?

人盡茶涼 提交于 2019-12-22 01:28:10

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/17393451/does-number-fall-in-interval-in-clojure

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!