September Morn Update
This commit is contained in:
parent
4e2d22a71d
commit
aac6731f2c
6856 changed files with 141342 additions and 21127 deletions
|
|
@ -1,49 +1,158 @@
|
|||
(() => {
|
||||
'use strict';
|
||||
|
||||
// sparkLine :: [Num] -> String
|
||||
let sparkLine = xs => {
|
||||
let min = minimumBy(numericOrdering, xs),
|
||||
max = maximumBy(numericOrdering, xs),
|
||||
range = max - min;
|
||||
const main = () => {
|
||||
|
||||
return xs.map(x => ((x - min) * 7) / range)
|
||||
.map(
|
||||
n => (n >= 0 && n < 8) ? '▁▂▃▄▅▆▇█'
|
||||
.split('')[Math.round(n)] : undefined
|
||||
).join('');
|
||||
},
|
||||
// sparkLine :: [Num] -> String
|
||||
const sparkLine = xs => {
|
||||
const hist = dataBins(8)(xs);
|
||||
return unlines([
|
||||
concat(map(
|
||||
i => '▁▂▃▄▅▆▇█' [i],
|
||||
hist.indexes
|
||||
)),
|
||||
unwords(xs),
|
||||
[
|
||||
'Min: ' + hist.min,
|
||||
'Mean: ' + hist.mean.toFixed(2),
|
||||
'Median: ' + hist.median,
|
||||
'Max: ' + hist.max,
|
||||
].join('\t'),
|
||||
''
|
||||
]);
|
||||
};
|
||||
|
||||
// maximumBy :: (a -> a -> Ordering) -> [a] -> a
|
||||
maximumBy = (f, xs) =>
|
||||
xs.reduce((a, x) =>
|
||||
a === undefined ? x : (
|
||||
f(x, a) > 0 ? x : a
|
||||
|
||||
// dataBins :: Int -> [Num] ->
|
||||
// {indexes:: [Int], min:: Float, max:: Float,
|
||||
// range :: Float, lbounds :: [Float]}
|
||||
const dataBins = intBins => xs => {
|
||||
const
|
||||
iLast = intBins - 1,
|
||||
ys = sort(xs),
|
||||
mn = ys[0],
|
||||
mx = last(ys),
|
||||
rng = mx - mn,
|
||||
w = rng / intBins,
|
||||
lng = xs.length,
|
||||
mid = lng / 2,
|
||||
lbounds = map(
|
||||
i => mn + (w * i),
|
||||
enumFromTo(1, iLast)
|
||||
);
|
||||
return {
|
||||
indexes: map(
|
||||
x => {
|
||||
const mb = findIndex(b => b > x, lbounds);
|
||||
return mb.Nothing ? (
|
||||
iLast
|
||||
) : mb.Just;
|
||||
},
|
||||
xs
|
||||
),
|
||||
undefined
|
||||
),
|
||||
lbounds: lbounds,
|
||||
min: mn,
|
||||
median: even(lng) ? (
|
||||
sum([ys[mid - 1], ys[mid]]) / 2
|
||||
) : ys[Math.floor(mid)],
|
||||
mean: sum(xs) / lng,
|
||||
max: mx,
|
||||
range: rng
|
||||
};
|
||||
};
|
||||
|
||||
// numbersFromString :: String -> [Float]
|
||||
const numbersFromString = s =>
|
||||
map(x => parseFloat(x, 10),
|
||||
s.split(/[,\s]+/)
|
||||
);
|
||||
|
||||
return unlines(map(
|
||||
compose(sparkLine, numbersFromString),
|
||||
[
|
||||
'0, 1, 19, 20',
|
||||
'0, 999, 4000, 4999, 7000, 7999',
|
||||
'1 2 3 4 5 6 7 8 7 6 5 4 3 2 1',
|
||||
'1.5, 0.5 3.5, 2.5 5.5, 4.5 7.5, 6.5'
|
||||
]
|
||||
));
|
||||
};
|
||||
|
||||
// GENERIC FUNCTIONS ----------------------------
|
||||
|
||||
// Just :: a -> Maybe a
|
||||
const Just = x => ({
|
||||
type: 'Maybe',
|
||||
Nothing: false,
|
||||
Just: x
|
||||
});
|
||||
|
||||
// Nothing :: Maybe a
|
||||
const Nothing = () => ({
|
||||
type: 'Maybe',
|
||||
Nothing: true,
|
||||
});
|
||||
|
||||
// compose (<<<) :: (b -> c) -> (a -> b) -> a -> c
|
||||
const compose = (f, g) => x => f(g(x));
|
||||
|
||||
// concat :: [[a]] -> [a]
|
||||
// concat :: [String] -> String
|
||||
const concat = xs =>
|
||||
0 < xs.length ? (() => {
|
||||
const unit = 'string' !== typeof xs[0] ? (
|
||||
[]
|
||||
) : '';
|
||||
return unit.concat.apply(unit, xs);
|
||||
})() : [];
|
||||
|
||||
|
||||
// minimumBy :: (a -> a -> Ordering) -> [a] -> a
|
||||
minimumBy = (f, xs) =>
|
||||
xs.reduce((a, x) =>
|
||||
a === undefined ? x : (
|
||||
f(x, a) < 0 ? x : a
|
||||
),
|
||||
undefined
|
||||
),
|
||||
// enumFromTo :: (Int, Int) -> [Int]
|
||||
const enumFromTo = (m, n) =>
|
||||
Array.from({
|
||||
length: 1 + n - m
|
||||
}, (_, i) => m + i);
|
||||
|
||||
numericOrdering = (a, b) => a < b ? -1 : (a > b ? 1 : 0);
|
||||
// even :: Int -> Bool
|
||||
const even = n => 0 === n % 2;
|
||||
|
||||
// TEST
|
||||
// last :: [a] -> a
|
||||
const last = xs =>
|
||||
0 < xs.length ? xs.slice(-1)[0] : undefined;
|
||||
|
||||
return ["1 2 3 4 5 6 7 8 7 6 5 4 3 2 1",
|
||||
"1.5, 0.5 3.5, 2.5 5.5, 4.5 7.5, 6.5",
|
||||
"3 2 1 0 -1 -2 -3 -4 -3 -2 -1 0 1 2 3",
|
||||
"-1000 100 1000 500 200 -400 -700 621 -189 3"
|
||||
].map(
|
||||
s => s.split(/[,\s]+/)
|
||||
.map(x => parseFloat(x, 10))
|
||||
).map(sparkLine);
|
||||
// map :: (a -> b) -> [a] -> [b]
|
||||
const map = (f, xs) =>
|
||||
(Array.isArray(xs) ? (
|
||||
xs
|
||||
) : xs.split('')).map(f);
|
||||
|
||||
// sort :: Ord a => [a] -> [a]
|
||||
const sort = xs => xs.slice()
|
||||
.sort((a, b) => a < b ? -1 : (a > b ? 1 : 0));
|
||||
|
||||
|
||||
// findIndex :: (a -> Bool) -> [a] -> Maybe Int
|
||||
const findIndex = (p, xs) => {
|
||||
const
|
||||
i = (
|
||||
'string' !== typeof xs ? (
|
||||
xs
|
||||
) : xs.split('')
|
||||
).findIndex(p);
|
||||
return -1 !== i ? (
|
||||
Just(i)
|
||||
) : Nothing();
|
||||
};
|
||||
|
||||
// sum :: [Num] -> Num
|
||||
const sum = xs => xs.reduce((a, x) => a + x, 0);
|
||||
|
||||
// unlines :: [String] -> String
|
||||
const unlines = xs => xs.join('\n');
|
||||
|
||||
// unwords :: [String] -> String
|
||||
const unwords = xs => xs.join(' ');
|
||||
|
||||
// MAIN ---
|
||||
return main();
|
||||
})();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue