ocl

How to implement the Luhn alorithm in OCL


I am trying to find the Luhn algorithm in OCL to check the validity of ISIN. Can anyone provide a code example would it be great!


Solution

  • In the German Wikipedia article about the Luhns algorithm (https://de.wikipedia.org/wiki/Luhn-Algorithmus) you can find an example to calculate the value for an ident of 446-667-651. The algorithm below calculates the correct value of 40.

    let list = '446667651'.ToCharArray.strToInt in 
    Sequence{1..list->size}
    ->Collect(i|
        if (list->size-i).Mod(2)=0 then 
            list.at(i) 
        else 
            (list.at(i)*2).Mod(9) 
        endif)
    ->Sum
    

    Maybe you need some adaptions for calculating the value for ISINs.