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 thea given list.
0 < ws.length ? (() => {
const
wordCount = ws.length,
charFreqs = charCounts(ws.join("")),
[h,charSet = s => new Set([...ts] = ws),
sharedwordCount = tws.reduce(length;
(a, x) => intersect(a)(new Set([...x])),
new Set([...h])
);
 
return sort([
[ ...shared].filterfoldr(
c => wordCount === charFreqs[c]compose(intersect, charSet)
)(
charSet(ws[0])
new Set)([ws...h]slice(1))
);]
.filter(c => wordCount === ws.length,charFreqs[c])
).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(
(a, x) => intersectf(ax)(new Set([...x])a),
acc
);
 
Line 351 ⟶ 374:
(a, b) => a < b ? -1 : (a > b ? 1 : 0)
);
 
 
// MAIN ---
9,655

edits