Validate International Securities Identification Number: Difference between revisions
Content added Content deleted
No edit summary |
|||
Line 1,486: | Line 1,486: | ||
Or, making alternative choices from the standard libraries: |
Or, making alternative choices from the standard libraries: |
||
<lang haskell>import |
<lang haskell>import Control.Monad ((<=<)) |
||
import Data.Bifunctor (first) |
|||
import Data.List (foldl') -- ' |
|||
import qualified Data.Map as M |
import qualified Data.Map as M |
||
import Data.Maybe (fromMaybe) |
import Data.Maybe (fromMaybe) |
||
Line 1,494: | Line 1,496: | ||
validISIN :: String -> Bool |
validISIN :: String -> Bool |
||
validISIN = |
validISIN = |
||
(&&) . isinPattern |
((&&) . isinPattern) |
||
<*> (luhn . (show =< |
<*> (luhn . (show <=< stringInts)) |
||
isinPattern :: String -> Bool |
isinPattern :: String -> Bool |
||
Line 1,501: | Line 1,503: | ||
12 == length s |
12 == length s |
||
&& all (`elem` capitals) l |
&& all (`elem` capitals) l |
||
&& all (`elem` (capitals |
&& all (`elem` (capitals <> digits)) m |
||
&& head r `elem` digits |
&& head r `elem` digits |
||
where |
where |
||
[l, m, r] = bites [2, 9, 1] |
[l, m, r] = bites s [2, 9, 1] |
||
luhn :: String -> Bool |
luhn :: String -> Bool |
||
Line 1,513: | Line 1,515: | ||
stream f = |
stream f = |
||
concat $ |
concat $ |
||
zipWith |
zipWith ($) (cycle f) (stringInts $ reverse x) |
||
($) |
|||
(cycle f) |
|||
(stringInts $ reverse x) |
|||
s1 = sum (stream odds) |
s1 = sum (stream odds) |
||
s2 = |
s2 = |
||
sum $ |
sum $ |
||
sum . stringInts . show |
sum . stringInts . show . (2 *) <$> stream evens |
||
. (2 *) <$> stream evens |
|||
charMap :: M.Map Char Int |
charMap :: M.Map Char Int |
||
Line 1,529: | Line 1,527: | ||
stringInts = fromMaybe [] . traverse (`M.lookup` charMap) |
stringInts = fromMaybe [] . traverse (`M.lookup` charMap) |
||
bites :: [ |
bites :: [a] -> [Int] -> [[a]] |
||
bites |
bites xs = |
||
(reverse . fst) |
(reverse . fst) |
||
. foldl' -- ' |
|||
(\ |
(\(a, r) x -> first (: a) (splitAt x r)) |
||
([], xs) |
([], xs) |
||
(reverse ns) |
|||
capitals, digits :: String |
capitals, digits :: String |