[REBOL] Optimizations anyone?
From: kpeters::otaksoft::com at: 6-Feb-2008 12:34
Hi all ~
I just wrote the function below to validate Canadian Social Insurance numbers (SIN)
and it strikes me as, well, rather awkward.
SINs are 9 digits wide and the rightmost digit is the check digit.
Any Rebolish ideas anyone?
TIA
Kai
valid-sin: function [ sin [string!] ] [ digits8 ] [
if any [
9 <> length? trim sin
none = to-integer sin
] [ return false ]
; from left to right all even positions
evens: copy ""
sin: skip sin 1
forskip sin 2 [ evens: append evens form ( 2 * to-integer to-string first sin )]
;
evens-sum: 0
foreach digit evens [ evens-sum: evens-sum + ( to-integer to-string digit )]
;
; from left to right all odd positions
odds-sum: 0
digits8: copy/part head sin 8
forskip digits8 2 [ odds-sum: odds-sum + ( to-integer to-string first digits8 )]
;
total-sum: evens-sum + odds-sum
next-ten: 10 * (1 + to-integer to-string first to-string total-sum )
check-digit: next-ten - total-sum
;
sin: head sin
check-digit = ( to-integer to-string sin/9 )
]
; 2 valid SINs
valid-sin "130692544"
valid-sin "193456787"