Unique characters in each string: Difference between revisions
m
→{{header|JavaScript}}: Slight reduction in terms of a foldr (curried .reduceRight)
m (→{{header|JavaScript}}: Slight reduction in terms of a foldr (curried .reduceRight)) |
|||
Line 293:
<lang javascript>(() => {
"use strict";
// --- CHARACTERS SEEN EXACTLY ONCE IN EACH STRING ---
// onceInEach :: [String] -> String
const onceInEach = ws =>
// Characters which occur exactly once
// in each word in
0 < ws.length ? (() => {
const
wordCount = ws.length,▼
charFreqs = charCounts(ws.join("")),
(a, x) => intersect(a)(new Set([...x])),▼
new Set([...h])▼
);▼
return sort([
)(
charSet(ws[0])
).join("");
})() : "";
Line 336 ⟶ 338:
}
), {}
);
// compose (<<<) :: (b -> c) -> (a -> b) -> a -> c
const compose = (...fs) =>
// A function defined by the right-to-left
// composition of all the functions in fs.
fs.reduce(
(f, g) => x => f(g(x)),
x => x
);
// foldr :: (a -> b -> b) -> b -> [a] -> b
const foldr = f =>
// Note that that the signature of foldr differs
// from that of foldl - the positions of
// current value and accumulator in f are reversed
acc => xs => [...xs].reduceRight(
acc
);
Line 351 ⟶ 374:
(a, b) => a < b ? -1 : (a > b ? 1 : 0)
);
// MAIN ---
|