If you want to check if a string contains a character, you can use a regular expression:
(re-find #"\." astr)
Or:
(some #(= \. %) astr)
Or:
(contains? (into #{} astr) \.)
Or can you use includes? from clojure.contrib.seq-utils , which does this too.
Clojure already has a reader who knows how to distinguish between ints and double, so if you are sure that your string contains only numbers, you can use it. (Be careful though it reads anything, not just numbers. This is potentially dangerous. Do not use this if it is possible that your line has something other than a number.)
Note. Clojure also handles the case where an integer is too large to fit into a native int without overflow. If you want to parse integers, you might need the bigint function, not parseInt .
user> (class (read-string "2.5")) java.lang.Double user> (class (read-string "2")) java.lang.Integer user> (class (read-string "2000000000000")) java.math.BigInteger
If your function is a predicate, is it commonly used in Clojure to denote its decimal? , not is-decimal . Your function is actually more than a number parser, so personally I would call it parse-number or string-to-number .
Brian carper
source share