Selectively replace multiple instances of a character within a string: Difference between revisions
Content added Content deleted
m (→Python) |
|||
Line 153: | Line 153: | ||
import Data.Maybe (fromMaybe) |
import Data.Maybe (fromMaybe) |
||
---------- POSITIONAL CHARACTER |
---------- POSITIONAL CHARACTER-REPLACEMENT RULES -------- |
||
nthCharsReplaced :: M.Map Char [Maybe Char] -> String -> String |
nthCharsReplaced :: M.Map Char [Maybe Char] -> String -> String |
||
Line 159: | Line 159: | ||
where |
where |
||
go a c = |
go a c = |
||
if M.member c ruleMap |
|||
then |
|||
let i = fromMaybe 0 (M.lookup c a) |
|||
) |
in ( M.insert c (succ i) a, |
||
otherChar i c (fromMaybe [] (M.lookup c ruleMap)) |
|||
) |
|||
else (a, c) |
|||
otherChar :: Int -> Char -> [Maybe Char] -> Char |
otherChar :: Int -> Char -> [Maybe Char] -> Char |
||
Line 168: | Line 171: | ||
| i < length deltas = fromMaybe c (deltas !! i) |
| i < length deltas = fromMaybe c (deltas !! i) |
||
| otherwise = c |
| otherwise = c |
||
--------------------------- TEST ------------------------- |
--------------------------- TEST ------------------------- |