Show ASCII table: Difference between revisions

Content added Content deleted
Line 2,618: Line 2,618:
=={{header|JavaScript}}==
=={{header|JavaScript}}==
<lang javascript>(() => {
<lang javascript>(() => {

"use strict";

// ------------------- ASCII TABLE -------------------


// asciiTable :: String
// asciiTable :: String
const asciiTable = () =>
const asciiTable = () =>
unlines(
transpose(
map(xs => concat(
chunksOf(16)(
map(justifyLeft(12, ' '),
enumFromTo(32)(127)
xs
.map(asciiEntry)
)
),
transpose(
chunksOf(
16,
map(asciiEntry,
enumFromTo(32, 127)
)
)
)
)
)
);
)
.map(
xs => xs.map(justifyLeft(12)(" "))
.join("")
)
.join("\n");


// asciiEntry :: Int -> String
// asciiEntry :: Int -> String
const asciiEntry = n => {
const asciiEntry = n => {
const k = asciiName(n);
const k = asciiName(n);

return '' === k ? (
''
return "" === k ? (
""
) : (justifyRight(4, ' ', n.toString()) + ' : ' + k);
) : `${justifyRight(4)(" ")(n.toString())} : ${k}`;
};
};


Line 2,649: Line 2,649:
const asciiName = n =>
const asciiName = n =>
32 > n || 127 < n ? (
32 > n || 127 < n ? (
''
""
) : 32 === n ? (
) : 32 === n ? (
'Spc'
"Spc"
) : 127 === n ? (
) : 127 === n ? (
'Del'
"Del"
) : chr(n);
) : chr(n);


// GENERIC FUNCTIONS ----------------------------------
// ---------------- GENERIC FUNCTIONS ----------------

// chunksOf :: Int -> [a] -> [[a]]
const chunksOf = (n, xs) =>
xs.reduce((a, _, i, xs) =>
i % n ? a : a.concat([xs.slice(i, i + n)]), []);


// chr :: Int -> Char
// chr :: Int -> Char
const chr = String.fromCodePoint;
const chr = x =>
// The character at unix code-point x.
String.fromCodePoint(x);



// comparing :: (a -> b) -> (a -> a -> Ordering)
const comparing = f =>
// chunksOf :: Int -> [a] -> [[a]]
(x, y) => {
const chunksOf = n => {
const
// xs split into sublists of length n.
a = f(x),
// The last sublist will be short if n
b = f(y);
// does not evenly divide the length of xs .
return a < b ? -1 : (a > b ? 1 : 0);
const go = xs => {
const chunk = xs.slice(0, n);

return 0 < chunk.length ? (
[chunk].concat(
go(xs.slice(n))
)
) : [];
};
};


// concat :: [[a]] -> [a]
return go;
};
// concat :: [String] -> String
const concat = xs =>
0 < xs.length ? (() => {
const unit = 'string' !== typeof xs[0] ? (
[]
) : '';
return unit.concat.apply(unit, xs);
})() : [];


// concatMap :: (a -> [b]) -> [a] -> [b]
const concatMap = (f, xs) =>
0 < xs.length ? (() => {
const unit = 'string' !== typeof xs ? (
[]
) : '';
return unit.concat.apply(unit, xs.map(f))
})() : [];


// enumFromTo :: Int -> Int -> [Int]
// enumFromTo :: Int -> Int -> [Int]
const enumFromTo = (m, n) =>
const enumFromTo = m =>
m <= n ? iterateUntil(
n => Array.from({
x => n <= x,
length: 1 + n - m
x => 1 + x,
}, (_, i) => m + i);
m
) : [];


// iterateUntil :: (a -> Bool) -> (a -> a) -> a -> [a]
const iterateUntil = (p, f, x) => {
const vs = [x];
let h = x;
while (!p(h))(h = f(h), vs.push(h));
return vs;
};


// justifyLeft :: Int -> Char -> String -> String
// justifyLeft :: Int -> Char -> String -> String
const justifyLeft = (n, cFiller) => strText =>
const justifyLeft = n =>
// The string s, followed by enough padding (with
n > strText.length ? (
// the character c) to reach the string length n.
(strText + cFiller.repeat(n))
.substr(0, n)
c => s => n > s.length ? (
) : strText;
s.padEnd(n, c)
) : s;



// justifyRight :: Int -> Char -> String -> String
// justifyRight :: Int -> Char -> String -> String
const justifyRight = (n, cFiller, strText) =>
const justifyRight = n =>
// The string s, preceded by enough padding (with
n > strText.length ? (
// the character c) to reach the string length n.
(cFiller.repeat(n) + strText)
.slice(-n)
c => s => Boolean(s) ? (
) : strText;
s.padStart(n, c)
) : "";


// length :: [a] -> Int
const length = xs => xs.length;

// map :: (a -> b) -> [a] -> [b]
const map = (f, xs) => xs.map(f);

// maximumBy :: (a -> a -> Ordering) -> [a] -> a
const maximumBy = (f, xs) =>
0 < xs.length ? (
xs.slice(1)
.reduce((a, x) => 0 < f(x, a) ? x : a, xs[0])
) : undefined;

// replicate :: Int -> a -> [a]
const replicate = (n, x) =>
Array.from({
length: n
}, () => x);


// transpose :: [[a]] -> [[a]]
// transpose :: [[a]] -> [[a]]
const transpose = tbl => {
const transpose = rows =>
// The columns of the input transposed
const
gaps = replicate(
// into new rows.
length(maximumBy(comparing(length), tbl)), []
// This version assumes input rows of even length.
),
0 < rows.length ? rows[0].map(
rows = map(xs => xs.concat(gaps.slice(xs.length)), tbl);
(x, i) => rows.flatMap(
return map(
v => v[i]
(_, col) => concatMap(row => [row[col]], rows),
)
rows[0]
) : [];
);
};


// unlines :: [String] -> String
const unlines = xs => xs.join('\n');


// MAIN -----------------------------------------------
// MAIN ---
return asciiTable();
return asciiTable();
})();</lang>
})();</lang>