Pascal's triangle: Difference between revisions
→JS ES6: Updated primitives, tidied.
Drkameleon (talk | contribs) |
(→JS ES6: Updated primitives, tidied.) |
||
Line 3,482:
===ES6===
<lang JavaScript>(() => {
const main = () =>▼
showPascal(take(7, pascal()));▼
// ---------------- PASCAL'S TRIANGLE ----------------
// pascal :: Generator [[Int]]
Line 3,492 ⟶ 3,490:
iterate(
xs => zipWith(
// ---------------------- TEST -----------------------
// main :: IO ()
▲ const main = () =>
showPascal(
);
// showPascal :: [[Int]] -> String
const showPascal = xs => {
const w = last(xs).join(" ").length;
};
//
//
const
});▼
margin = c.repeat(Math.floor(gap / 2)),
dust = c.repeat(gap % 2);
return `${margin}${s}${margin}${dust}`;
▲ // Size of space -> filler Char -> String -> Centered String
▲ const
▲ q = qr[0];
▲ return replicateString(q, c) +
};▼
//
const
// applications of f to x.
let v = x;
while (true) {▼
v = f(v);
▲ };
▲ function* iterate(f, x) {
▲ let v = x;
▲ while (true) {
▲ yield(v);
▲ v = f(v);
▲ }
// last :: [a] -> a
Line 3,549 ⟶ 3,555:
0 < xs.length ? xs.slice(-1)[0] : undefined;
▲ // length :: [a] -> Int
// take :: Int -> [a] -> [a]
// take :: Int -> String -> String
const take =
// The first n elements of a list,
xs.constructor.constructor.name !== 'GeneratorFunction' ? (▼
// string of characters, or stream.
xs => "GeneratorFunction" !== xs
xs.slice(0, n)
) :
length: n
}, () => {
const x = xs.next();
return x.done ? [] : [x.value];
}).flat();
// zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
const zipWith =
// A list constructed by zipping with a
▲ const
// custom function, rather than with the
// default tuple
xs =>
// MAIN ---
|