Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.55 KB | None | 0 0
  1. (def-module luhn)
  2.  
  3. (use (Strings [reverse split parse])
  4. (Funcs [map reduce_indexed])
  5. (Funcs/Results [expect]))
  6.  
  7. (pub def-fn luhn (:: string bool) [bin]
  8. (-<>> bin
  9. (|> reverse)
  10. (|> split <> "")
  11. (|> map <> parse<uint32>)
  12. (|> map <> expect "luhn: BIN (Bank Identification Number) expected")
  13. (|> reduce_indexed <> luhn-elt 0)
  14. (|> % <> 10)
  15. (|> = 0)))
  16.  
  17. (def-fn luhn-elt (:: int int int int) [acu elt idx]
  18. (if (= 0 (% idx 2))
  19. (+ acu elt)
  20. (+ acu (let [n (* 2 elt)]
  21. (if (> n 9) (- n 9) n)))))
  22.  
  23. ; vim: ft=lisp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement