views:

109

answers:

2

I'm trying to write a function that checks if a character is within a certain hexadecimal range.

I'm 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 because the >= operator expects a number, it tries to type cast it.In regular java, I could just do:

(current >= 0x20) && (current <= 0xD7FF)
+6  A: 

explicitly convert it to an int first

(<= 0x20 (int current) 0xD7FF)
cobbal
+3  A: 

Chars aren't numbers in Clojure though it's easy to cast them into characters with the int function.

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

For casts to primitive types you can use the function with the name of the type you want (see (find-doc #"Coerce")).

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