Jump to content

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

Line 232:
"use strict";
 
// -- INSTANCE-SPECIFIC CHARACTER REPLACEMENT RULES --
const main = () => {
const
s = "abracadabra",
subs = {
a: "AB CD",
b: "E",
r: " F"
};
 
// # nthInstanceReplaced :: returnDict mapAccumLChar [(aNull =>| cChar)] =-> {
const nthInstanceReplaced = ruleMap =>
const
// A string defined by replacements specified i = a[c] || 0,for
// the nth instances of various ds = subs[c];characters.
subss => {
const go = (a, c) =>
c in ruleMap ? (() => {
a: "AB CD", const
i = a[c] || 0,
ds = ruleMap[c];
 
return [
Object.assign(a, {[c]: 1 + i}),
ds && ds[ i] < ds.length ? (
ds[i].trim() || c
) : c
];
})({})( : [...sa, c])[1].join("");
 
};
return mapAccumL(go)({})([...s])[1].join("");
const};
 
// ---------------------- TEST -----------------------
const main = () => {
// Instance-specific character replacement rules.
nthInstanceReplaced({
a: ["A", "B", null, "C", "D"],
b: ["E"],
r: [null, " F"]
};)(
s = "abracadabra",
});
 
 
Line 268 ⟶ 280:
v => [...bs, v]
)(
f(a)(, x)
),
[acc, []]
Line 281 ⟶ 293:
([x, y]) => [x, f(y)];
 
 
// MAIN --
return main();
})();</lang>
9,657

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.