Created
August 27, 2011 18:11
-
-
Save alandipert/1175686 to your computer and use it in GitHub Desktop.
ISBN-13 validation
This file contains 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
(defn isbn13? [isbn13] | |
(let [nums (map #(Integer/parseInt %) (re-seq #"\d" isbn13)) | |
first-nums (butlast nums) | |
sum (->> first-nums | |
(map vector (cycle [1 3])) | |
(map (partial apply *)) | |
(reduce +)) | |
last-num (last nums)] | |
(= last-num | |
(- 10 (mod sum 10))))) | |
(comment | |
(map isbn13? ["978-0-306-40615-7" "978-0-306-40615-2"]) | |
;; (true false) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I like the let idiom. Makes me want a "where" clause like Haskell has. :) And ->> reminds me of |> in F# (the pipeline operator). Thanks for the implementation.