Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
|
|
@ -0,0 +1,60 @@
|
|||
(function () {
|
||||
'use strict';
|
||||
|
||||
// arithmetic_mean :: [Number] -> Number
|
||||
function arithmetic_mean(ns) {
|
||||
return (
|
||||
ns.reduce( // sum
|
||||
function (sum, n) {
|
||||
return (sum + n);
|
||||
},
|
||||
0
|
||||
) / ns.length
|
||||
);
|
||||
}
|
||||
|
||||
// geometric_mean :: [Number] -> Number
|
||||
function geometric_mean(ns) {
|
||||
return Math.pow(
|
||||
ns.reduce( // product
|
||||
function (product, n) {
|
||||
return (product * n);
|
||||
},
|
||||
1
|
||||
),
|
||||
1 / ns.length
|
||||
);
|
||||
}
|
||||
|
||||
// harmonic_mean :: [Number] -> Number
|
||||
function harmonic_mean(ns) {
|
||||
return (
|
||||
ns.length / ns.reduce( // sum of inverses
|
||||
function (invSum, n) {
|
||||
return (invSum + (1 / n));
|
||||
},
|
||||
0
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
var values = [arithmetic_mean, geometric_mean, harmonic_mean]
|
||||
.map(function (f) {
|
||||
return f([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
|
||||
}),
|
||||
mean = {
|
||||
Arithmetic: values[0], // arithmetic
|
||||
Geometric: values[1], // geometric
|
||||
Harmonic: values[2] // harmonic
|
||||
}
|
||||
|
||||
return JSON.stringify({
|
||||
values: mean,
|
||||
test: "is A >= G >= H ? " +
|
||||
(
|
||||
mean.Arithmetic >= mean.Geometric &&
|
||||
mean.Geometric >= mean.Harmonic ? "yes" : "no"
|
||||
)
|
||||
}, null, 2);
|
||||
|
||||
})();
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"values": {
|
||||
"Arithmetic": 5.5,
|
||||
"Geometric": 4.528728688116765,
|
||||
"Harmonic": 3.414171521474055
|
||||
},
|
||||
"test": "is A >= G >= H ? yes"
|
||||
}
|
||||
|
|
@ -0,0 +1,63 @@
|
|||
(() => {
|
||||
|
||||
// arithmeticMean :: [Number] -> Number
|
||||
const arithmeticMean = xs =>
|
||||
foldl((sum, n) => sum + n, 0, xs) / length(xs);
|
||||
|
||||
// geometricMean :: [Number] -> Number
|
||||
const geometricMean = xs =>
|
||||
raise(foldl((product, x) => product * x, 1, xs), 1 / length(xs));
|
||||
|
||||
// harmonicMean :: [Number] -> Number
|
||||
const harmonicMean = xs =>
|
||||
length(xs) / foldl((invSum, n) => invSum + (1 / n), 0, xs);
|
||||
|
||||
// GENERIC FUNCTIONS ------------------------------------------------------
|
||||
|
||||
// A list of functions applied to a list of arguments
|
||||
// <*> :: [(a -> b)] -> [a] -> [b]
|
||||
const ap = (fs, xs) => //
|
||||
[].concat.apply([], fs.map(f => //
|
||||
[].concat.apply([], xs.map(x => [f(x)]))));
|
||||
|
||||
// foldl :: (b -> a -> b) -> b -> [a] -> b
|
||||
const foldl = (f, a, xs) => xs.reduce(f, a);
|
||||
|
||||
// length :: [a] -> Int
|
||||
const length = xs => xs.length;
|
||||
|
||||
// mapFromList :: [(k, v)] -> Dictionary
|
||||
const mapFromList = kvs =>
|
||||
foldl((a, [k, v]) =>
|
||||
(a[(typeof k === 'string' && k) || show(k)] = v, a), {}, kvs);
|
||||
|
||||
// raise :: Num -> Int -> Num
|
||||
const raise = (n, e) => Math.pow(n, e);
|
||||
|
||||
// show :: a -> String
|
||||
// show :: a -> Int -> String
|
||||
const show = (...x) =>
|
||||
JSON.stringify.apply(
|
||||
null, x.length > 1 ? [x[0], null, x[1]] : x
|
||||
);
|
||||
|
||||
// zip :: [a] -> [b] -> [(a,b)]
|
||||
const zip = (xs, ys) =>
|
||||
xs.slice(0, Math.min(xs.length, ys.length))
|
||||
.map((x, i) => [x, ys[i]]);
|
||||
|
||||
// TEST -------------------------------------------------------------------
|
||||
// mean :: Dictionary
|
||||
const mean = mapFromList(zip(
|
||||
['Arithmetic', 'Geometric', 'Harmonic'],
|
||||
ap([arithmeticMean, geometricMean, harmonicMean], [
|
||||
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||
])
|
||||
));
|
||||
|
||||
return show({
|
||||
values: mean,
|
||||
test: `is A >= G >= H ? ${mean.Arithmetic >= mean.Geometric &&
|
||||
mean.Geometric >= mean.Harmonic ? "yes" : "no"}`
|
||||
}, 2);
|
||||
})();
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"values": {
|
||||
"Arithmetic": 5.5,
|
||||
"Geometric": 4.528728688116765,
|
||||
"Harmonic": 3.414171521474055
|
||||
},
|
||||
"test": "is A >= G >= H ? yes"
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue