Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (def-module luhn)
- (use (Strings [reverse split parse])
- (Funcs [map reduce_indexed])
- (Funcs/Results [expect]))
- (pub def-fn luhn (:: string bool) [bin]
- (-<>> bin
- (|> reverse)
- (|> split <> "")
- (|> map <> parse<uint32>)
- (|> map <> expect "luhn: BIN (Bank Identification Number) expected")
- (|> reduce_indexed <> luhn-elt 0)
- (|> % <> 10)
- (|> = 0)))
- (def-fn luhn-elt (:: int int int int) [acu elt idx]
- (if (= 0 (% idx 2))
- (+ acu elt)
- (+ acu (let [n (* 2 elt)]
- (if (> n 9) (- n 9) n)))))
- ; vim: ft=lisp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement