2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -1,59 +1,53 @@
(function () {
// Generalised max() function
// [a] -> (a -> n) -> a
function max(list, fnCompare) {
return list.reduce(function (acc, b) {
var a = acc || b,
lngDiff = fnCompare(a, b);
return lngDiff ? (lngDiff > 0 ? b : a) : a;
}, null)
// (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
// 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 === null ? b : a === b ? 0 : a < b ? -1 : 1;
return a < b ? 1 : (a > b ? -1 : 0)
}
function wordSortLast(a, b) {
return a === null ? b : a === b ? 0 : a < b ? 1 : -1;
return a < b ? -1 : (a > b ? 1 : 0)
}
function wordLongest(a, b) {
var lngA = a ? a.length : b.length,
lngB = b.length;
return lngA === lngB ? 0 : lngA > lngB ? -1 : 1;
return a.length - b.length;
}
function cityPopulationMost(a, b) {
var nA = a ? a.population : b.population,
nB = b.population;
return nA === nB ? 0 : nA > nB ? -1 : 1;
return a.population - b.population;
}
function cityPopulationLeast(a, b) {
var nA = a ? a.population : b.population,
nB = b.population;
return nA === nB ? 0 : nA < nB ? -1 : 1;
return b.population - a.population;
}
function cityNameSortFirst(a, b) {
var sA = a ? a.name : b.name,
sB = b.name;
var strA = a.name,
strB = b.name;
return sA === sB ? 0 : sA < sB ? -1 : 1;
return strA < strB ? 1 : (strA > strB ? -1 : 0);
}
function cityNameSortLast(a, b) {
var sA = a ? a.name : b.name,
sB = b.name;
var strA = a.name,
strB = b.name;
return sA === sB ? 0 : sA > sB ? -1 : 1;
return strA < strB ? -1 : (strA > strB ? 1 : 0);
}
var lstWords = [
@ -62,38 +56,38 @@
];
var lstCities = [
{
name: 'Shanghai',
population: 24.15
{
name: 'Shanghai',
population: 24.15
}, {
name: 'Karachi',
population: 23.5
name: 'Karachi',
population: 23.5
}, {
name: 'Beijing',
population: 21.5
name: 'Beijing',
population: 21.5
}, {
name: 'Tianjin',
population: 14.7
name: 'Tianjin',
population: 14.7
}, {
name: 'Istanbul',
population: 14.4
name: 'Istanbul',
population: 14.4
}, , {
name: 'Lagos',
population: 13.4
name: 'Lagos',
population: 13.4
}, , {
name: 'Tokyo',
population: 13.3
name: 'Tokyo',
population: 13.3
}
];
return [
max(lstWords, wordSortFirst),
max(lstWords, wordSortLast),
max(lstWords, wordLongest),
max(lstCities, cityPopulationMost),
max(lstCities, cityPopulationLeast),
max(lstCities, cityNameSortFirst),
max(lstCities, cityNameSortLast)
maximumBy(wordSortFirst, lstWords),
maximumBy(wordSortLast, lstWords),
maximumBy(wordLongest, lstWords),
maximumBy(cityPopulationMost, lstCities),
maximumBy(cityPopulationLeast, lstCities),
maximumBy(cityNameSortFirst, lstCities),
maximumBy(cityNameSortLast, lstCities)
]
})();