Skip to content

Instantly share code, notes, and snippets.

@werand
Created February 2, 2016 20:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save werand/01cd188205f1774a00de to your computer and use it in GitHub Desktop.
Save werand/01cd188205f1774a00de to your computer and use it in GitHub Desktop.
(ns hex.core)
;;
;; Decode hex numbers - kata
;;
(defn decimal-value [n]
(or
(get {\0 0 \1 1 \2 2 \3 3 \4 4 \5 5 \6 6 \7 7 \8 8 \9 9 \a 10 \b 11 \c 12 \d 13 \e 14 \f 15} n)
(throw (Exception. (str "Invalid number: " n)))))
(defn power-of [n]
(iterate #(* n %) 1M))
(defn to-decimal [hex]
(->> (seq hex)
(map decimal-value)
reverse
(map #(* %1 %2) (power-of 16))
(reduce +)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment