Skip to content

Instantly share code, notes, and snippets.

@alandipert
Created August 27, 2011 18:11
Show Gist options
  • Save alandipert/1175686 to your computer and use it in GitHub Desktop.
Save alandipert/1175686 to your computer and use it in GitHub Desktop.
ISBN-13 validation
(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)
)
@jjthrash
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment