我想计算一个GS1 check digit并且已经拿出了下面的代码。用于计算校验位的算法是:用Clojure计算GS1校验位的更习惯的方法
- 反向条形码
- 删除最后一个数字(计算校验位)
- 与第一,第三,第五,e.t.c.一起添加数字数字乘以3个,甚至数字乘以1
- 从最近十
这听起来相等或更高倍数减去总和简单的打了出来,但我想出了解决方案显得有点不雅。它确实有效,但我想知道是否有更优雅的书写方式。
(defn abs "(abs n) is the absolute value of n" [n]
(cond
(not (number? n)) (throw (IllegalArgumentException.
"abs requires a number"))
(neg? n) (- n)
:else n))
(defn sum-seq "adds (first number times 3) with (second number)"
[coll]
(+
(* (first coll) 3)
(second coll)))
(defn sum-digit
[s]
(reduce +
(map sum-seq
(partition 2 2 '(0)
(map #(Integer/parseInt %)
(drop 2 (clojure.string/split (clojure.string/reverse s) #"")))))))
(defn mod-higher10 "Subtracts the sum from nearest equal or higher multiple of ten"
[i]
(if (zero? (rem i 10))
0
(- 10(rem i 10))))
(defn check-digit "calculates a GS1 check digit"
[s]
(mod-higher10
(sum-digit s)))
(= (check-digit "7311518182472") 2)
(= (check-digit "7311518152284") 4)
(= (check-digit "7311518225261") 1)
(= (check-digit "7311518241452") 2)
(= (check-digit "7311518034399") 9)
(= (check-digit "7311518005955") 5)
(= (check-digit "7311518263393") 3)
(= (check-digit "7311518240943") 3)
(= (check-digit "00000") 7)
(= (check-digit "") 0)
你很乐意接受答案吗? (我是初学者,努力工作!) –