Selectively replace multiple instances of a character within a string: Difference between revisions

Content added Content deleted
Line 158: Line 158:
nthCharsReplaced ruleMap = snd . mapAccumL go M.empty
nthCharsReplaced ruleMap = snd . mapAccumL go M.empty
where
where
go a c =
go a c
if M.member c ruleMap
| M.member c ruleMap =
then
let i = fromMaybe 0 (M.lookup c a)
let i = fromMaybe 0 (M.lookup c a)
in ( M.insert c (succ i) a,
in ( M.insert c (succ i) a,
otherChar i c (fromMaybe [] (M.lookup c ruleMap))
otherChar i c (fromMaybe [] (M.lookup c ruleMap))
)
)
else (a, c)
| otherwise = (a, c)


otherChar :: Int -> Char -> [Maybe Char] -> Char
otherChar :: Int -> Char -> [Maybe Char] -> Char