Selectively replace multiple instances of a character within a string: Difference between revisions
Content added Content deleted
m (→Python) |
|||
Line 272: | Line 272: | ||
// the nth instances of various characters. |
// the nth instances of various characters. |
||
s => mapAccumL( |
s => mapAccumL( |
||
(a, c) => c in |
(a, c) => c in a ? (() => { |
||
const |
const ds = a[c]; |
||
i = a[c] || 0, |
|||
ds = ruleMap[c]; |
|||
return [ |
return 0 < ds.length ? [ |
||
Object.assign(a, {[c]: 1 |
Object.assign(a, {[c]: ds.slice(1)}), |
||
ds[0] || c |
|||
] : [a, c]; |
|||
) : c |
|||
]; |
|||
})() : [a, c] |
})() : [a, c] |
||
)( |
)(ruleMap)([...s])[1].join(""); |
||