Selectively replace multiple instances of a character within a string: Difference between revisions
Content added Content deleted
m (→{{header|ALGOL 68}}: typo) |
|||
Line 192: | Line 192: | ||
nthCharsReplaced :: M.Map Char [Maybe Char] -> String -> String |
nthCharsReplaced :: M.Map Char [Maybe Char] -> String -> String |
||
nthCharsReplaced ruleMap = snd . mapAccumL go |
nthCharsReplaced ruleMap = snd . mapAccumL go ruleMap |
||
where |
where |
||
go a c = |
go a c = |
||
case M.lookup c |
case M.lookup c a of |
||
Nothing -> (a, c) |
Nothing -> (a, c) |
||
Just ds -> |
Just ds -> |
||
if null ds |
|||
then (a, c) |
|||
else |
|||
( M.adjust tail c a, |
|||
fromMaybe c (head ds) |
|||
) |
) |
||