Jump to content

Brace expansion using ranges: Difference between revisions

→‎{{header|JavaScript}}: Updated primitives
(Updated and fixed Python in line with new task example.)
(→‎{{header|JavaScript}}: Updated primitives)
Line 527:
 
return 0 < expansions.length ? (() => {
const [parsed, residue] = Array.from(expansions[0];
expansions[0]
);
 
return suffixAdd(
Line 584 ⟶ 582:
];
 
return tests.map(s => {
s => {const
constexpanded = braceExpandWithRange(s)
expanded = braceExpandWithRange.join(s"\n\t");
.join("\n\t");
 
return `${s} -> \n\t${expanded}`;
})
)
.join("\n\n");
};
Line 773 ⟶ 769:
p => Parser(
s => parse(pf)(s).flatMap(
vr([v, r]) => parse(
fmapP(vr[0]v)(p)
)(vr[1]r)
)
);
Line 804 ⟶ 800:
f => Parser(
s => parse(p)(s).flatMap(
tpl([x, r]) => parse(f(tpl[0]x))(tpl[1]r)
)
);
Line 847 ⟶ 843:
p => Parser(
s => parse(p)(s).flatMap(
vr => Tuplefirst(f(vr[0]))(vr[1])
)
);
Line 855 ⟶ 851:
const item = () =>
// A single character.
Parser(s => {
sconst =>[h, 0...t] <= s.length ? [;
 
Tuple(s[0])(
return Boolean(h) ? s.slice(1)[
Tuple(h)(t)
] : [];
});
 
 
Line 922 ⟶ 918:
// Any character for which the
// given predicate returns true.
Parser(s => {
sconst =>[h, 0...t] <= s.length ? (;
 
test(s[0]) ? [
return Boolean(h) ? Tuple(s[0])(s.slice(1))
expansionstest(h) ? [0]
Tuple(s[0]h)(t)
] : []
) : [];
});
 
// sequenceP :: [Parser a] -> Parser [a]
Line 937 ⟶ 935:
s => ps.reduce(
(a, q) => a.flatMap(
vr([v, r]) => parse(q)(snd(vr)r).flatMap(
first(xs => fst(vr)v.concat(xs))
)
),
Line 980 ⟶ 978:
"0": a,
"1": b,
length: 2,
*[Symbol.iterator]() {
for (const k in this) {
if (!isNaN(k)) {
.join("\n\t")yield this[k];
test(s[0]) ? [ }
[]}
);}
});
 
 
// abs :: Num -> Num
Line 1,019 ⟶ 1,025:
 
// concat :: [[a]] -> [a]
//const concat ::= [String]xs -=> String
const concat = xs => .flat(1);
ys => 0 < ys.length ? (
ys.every(Array.isArray) ? (
[]
) : ""
).concat(...ys) : ys
)(list(xs));
 
 
Line 1,059:
// A simple function lifted to one which applies
// to a tuple, transforming only its first item.
xy([x, y]) => Tuple(f(xy[0]x))(y);
xy[1]
);
 
 
Line 1,071 ⟶ 1,069:
(a, b) => op(b, a)
) : (x => y => op(y)(x));
 
 
// fst :: (a, b) -> a
const fst = tpl =>
// First member of a pair.
tpl[0];
 
 
Line 1,109 ⟶ 1,101:
s.padStart(n, c)
) : s;
 
 
// list :: StringOrArrayLike b => b -> [a]
const list = xs =>
// xs itself, if it is an Array,
// or an Array derived from xs.
Array.isArray(xs) ? (
xs
) : Array.from(xs || []);
 
 
Line 1,179 ⟶ 1,162:
const reverse = xs =>
xs.slice(0).reverse();
 
 
// snd :: (a, b) -> b
const snd = tpl =>
// Second member of a pair.
tpl[1];
 
 
Line 1,202 ⟶ 1,179:
return "number" !== t ? (
(() => {
const [i, mx] = [x, maxBound(x)].map(fromEnum);
) : "" fromEnum
);
 
return i < mx ? (
9,655

edits

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