siyu798 <siyu...@gmail.com> writes: Hi!
> Is there an idiomatic/built-in way to parse double with a default > value if there's exception? Not that I know of. > Currently we use a generic with-default macro to ignore exception and > return default value as follow: > > (with-default 0.0 (Double. my-value)) Why a macro instead of a function? --8<---------------cut here---------------start------------->8--- (defn parse-double [s & [d]] (try (Double/valueOf s) (catch Exception _ d))) --8<---------------cut here---------------end--------------->8--- That way, you can use it directly with higher-order functions: ;; Without default value (map parse-double ["0" "1.5" "3.8e-1" "broken" "0x1.bP2" "Infinity" "NaN"]) ==> (0.0 1.5 0.38 nil 6.75 Infinity NaN) ;; With default value 42.0 (map #(parse-double % 42.0) ["0" "1.5" "3.8e-1" "broken" "0x1.bP2" "Infinity" "NaN"]) ==> (0.0 1.5 0.38 42.0 6.75 Infinity NaN) Bye, Tassilo -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en