philipbohun.com

Glojure by Example: Number Parsing

parse-long and parse-double are Clojure functions. They return the number, or nil when the text is not a number.

(println (parse-long "42"))
(println (parse-long "nope"))
(println (parse-double "3.5"))

The Go parsers return a vector of the number and an error. Atoi parses an integer. ParseFloat takes the bit size as well.

(let [[n err] (strconv.Atoi "256")]
  (println n (nil? err)))
(let [[n err] (strconv.ParseFloat "1.25" 64)]
  (println n (nil? err)))

A failed parse-long is nil, not an exception.

$ glj number-parsing.glj
42
nil
3.5
256 true
1.25 true

Next example: URL Parsing.