Initial data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 72d218235f
commit f23f22d71c
199087 changed files with 3378941 additions and 0 deletions

View file

@ -0,0 +1 @@
Math.max.apply(null, [ 0, 1, 2, 5, 4 ]); // 5

View file

@ -0,0 +1,93 @@
(function () {
// (a -> a -> Ordering) -> [a] -> a
function maximumBy(f, xs) {
return xs.reduce(function (a, x) {
return a === undefined ? x : (
f(x, a) > 0 ? x : a
);
}, undefined);
}
// COMPARISON FUNCTIONS FOR SPECIFIC DATA TYPES
//Ordering: (LT|EQ|GT)
// GT: 1 (or other positive n)
// EQ: 0
// LT: -1 (or other negative n)
function wordSortFirst(a, b) {
return a < b ? 1 : (a > b ? -1 : 0)
}
function wordSortLast(a, b) {
return a < b ? -1 : (a > b ? 1 : 0)
}
function wordLongest(a, b) {
return a.length - b.length;
}
function cityPopulationMost(a, b) {
return a.population - b.population;
}
function cityPopulationLeast(a, b) {
return b.population - a.population;
}
function cityNameSortFirst(a, b) {
var strA = a.name,
strB = b.name;
return strA < strB ? 1 : (strA > strB ? -1 : 0);
}
function cityNameSortLast(a, b) {
var strA = a.name,
strB = b.name;
return strA < strB ? -1 : (strA > strB ? 1 : 0);
}
var lstWords = [
'alpha', 'beta', 'gamma', 'delta', 'epsilon', 'zeta', 'eta',
'theta', 'iota', 'kappa', 'lambda'
];
var lstCities = [
{
name: 'Shanghai',
population: 24.15
}, {
name: 'Karachi',
population: 23.5
}, {
name: 'Beijing',
population: 21.5
}, {
name: 'Tianjin',
population: 14.7
}, {
name: 'Istanbul',
population: 14.4
}, , {
name: 'Lagos',
population: 13.4
}, , {
name: 'Tokyo',
population: 13.3
}
];
return [
maximumBy(wordSortFirst, lstWords),
maximumBy(wordSortLast, lstWords),
maximumBy(wordLongest, lstWords),
maximumBy(cityPopulationMost, lstCities),
maximumBy(cityPopulationLeast, lstCities),
maximumBy(cityNameSortFirst, lstCities),
maximumBy(cityNameSortLast, lstCities)
]
})();

View file

@ -0,0 +1,21 @@
[
"alpha",
"zeta",
"epsilon",
{
"name": "Shanghai",
"population": 24.15
},
{
"name": "Tokyo",
"population": 13.3
},
{
"name": "Beijing",
"population": 21.5
},
{
"name": "Tokyo",
"population": 13.3
}
]

View file

@ -0,0 +1 @@
Math.max(...[ 0, 1, 2, 5, 4 ]); // 5

View file

@ -0,0 +1,94 @@
(() => {
"use strict";
// ----------- GREATEST ELEMENT OF A LIST ------------
// maximumByMay :: (a -> a -> Ordering) ->
// [a] -> Maybe a
const maximumByMay = f =>
// Nothing, if the list is empty,
// or just the maximum value when compared
// in terms of f.
xs => Boolean(xs.length) ? (
Just(xs.slice(1).reduce(
(a, x) => 0 < f(a)(x) ? (
a
) : x,
xs[0]
))
) : Nothing();
// ---------------------- TEST -----------------------
const main = () =>
JSON.stringify(
catMaybes([
maximumByMay(
comparing(x => x.length)
)([
"alpha", "beta", "gamma", "delta",
"epsilon", "zeta", "eta"
]),
maximumByMay(comparing(x => x.length))([]),
maximumByMay(comparing(x => x.n))([{
k: "Bejing",
n: 21.5
}, {
k: "Delhi",
n: 16.7
}, {
k: "Karachi",
n: 23.5
}, {
k: "Lagos",
n: 16.0
}, {
k: "Shanghai",
n: 24.3
}, {
k: "Tokyo",
n: 13.2
}])
]),
null, 2
);
// --------------------- GENERIC ---------------------
// Just :: a -> Maybe a
const Just = x => ({
type: "Maybe",
Nothing: false,
Just: x
});
// Nothing :: Maybe a
const Nothing = () => ({
type: "Maybe",
Nothing: true
});
// catMaybes :: [Maybe a] -> [a]
const catMaybes = mbs =>
mbs.flatMap(
m => m.Nothing ? (
[]
) : [m.Just]
);
// comparing :: (a -> b) -> (a -> a -> Ordering)
const comparing = f =>
x => y => {
const
a = f(x),
b = f(y);
return a < b ? -1 : (a > b ? 1 : 0);
};
// MAIN ---
return main();
})();