How to determine if a character is within range in Clojure?

I am trying to write a function that checks if a character is in a specific hexadecimal range.

I am trying the code shown below:

(def current \s)
(and (>= current (char 0x20)) (<= current (char 0xD7FF)))

I get the following error:

java.lang.ClassCastException: java.lang.Character cannot be cast to 
java.lang.Number (NO_SOURCE_FILE:0)

I assume that the operator> = expects a number, it is trying to enter cast it.In regular java, I could just do:

(current >= 0x20) && (current <= 0xD7FF)
+5
source share
2 answers

explicitly convert it to int first

(<= 0x20 (int current) 0xD7FF)
+8
source

Balls are not numbers in Clojure, although it is easy to distinguish them into characters using a function int.

(number? \a)       => false
(number? 42)       => true
(number? (int \a)) => true

To cast to primitive types, you can use a function with the name of the type you want (see (find-doc #"Coerce")).

(int (char 0x20))
(float ...)
(map double [1 2 3 4])
 ....
+3
source

All Articles