Created
February 2, 2016 20:32
-
-
Save werand/01cd188205f1774a00de to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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