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 = () => |
||
transpose( |
|||
chunksOf(16)( |
|||
enumFromTo(32)(127) |
|||
.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" |
|||
) : 127 === n ? ( |
) : 127 === n ? ( |
||
"Del" |
|||
) : chr(n); |
) : chr(n); |
||
// |
// ---------------- 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 = |
const chr = x => |
||
// The character at unix code-point x. |
|||
String.fromCodePoint(x); |
|||
// comparing :: (a -> b) -> (a -> a -> Ordering) |
|||
// chunksOf :: Int -> [a] -> [[a]] |
|||
const chunksOf = n => { |
|||
// xs split into sublists of length n. |
|||
// The last sublist will be short if n |
|||
// does not evenly divide the length of xs . |
|||
const go = xs => { |
|||
const chunk = xs.slice(0, n); |
|||
return 0 < chunk.length ? ( |
|||
[chunk].concat( |
|||
go(xs.slice(n)) |
|||
) |
|||
) : []; |
|||
}; |
}; |
||
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 = |
const enumFromTo = m => |
||
n => Array.from({ |
|||
length: 1 + n - m |
|||
}, (_, 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 = |
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)) |
|||
. |
c => s => n > s.length ? ( |
||
s.padEnd(n, c) |
|||
) : s; |
|||
// justifyRight :: Int -> Char -> String -> String |
// justifyRight :: Int -> Char -> String -> String |
||
const justifyRight = |
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) |
|||
c => s => Boolean(s) ? ( |
|||
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 = |
const transpose = rows => |
||
// The columns of the input transposed |
|||
const |
|||
// into new rows. |
|||
length |
// This version assumes input rows of even length. |
||
0 < rows.length ? rows[0].map( |
|||
(x, i) => rows.flatMap( |
|||
v => v[i] |
|||
) |
|||
) : []; |
|||
); |
|||
}; |
|||
// unlines :: [String] -> String |
|||
const unlines = xs => xs.join('\n'); |
|||
// MAIN |
// MAIN --- |
||
return asciiTable(); |
return asciiTable(); |
||
})();</lang> |
})();</lang> |