Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does number fall in interval in Clojure?

Tags:

clojure

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.

like image 651
David J. Avatar asked Jun 30 '13 18:06

David J.


1 Answers

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.

like image 102
Blacksad Avatar answered Sep 26 '22 00:09

Blacksad