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,16 @@
function sort_disjoint(values, indices) {
var sublist = [];
indices.sort(function(a, b) { return a > b; });
for (var i = 0; i < indices.length; i += 1) {
sublist.push(values[indices[i]]);
}
sublist.sort(function(a, b) { return a < b; });
for (var i = 0; i < indices.length; i += 1) {
values[indices[i]] = sublist.pop();
}
return values;
}

View file

@ -0,0 +1,27 @@
(function () {
'use strict';
// disjointSort :: [a] -> [Int] -> [a]
function disjointSort(xs, indices) {
// Sequence of indices discarded
var indicesSorted = indices.sort(),
subsetSorted = indicesSorted
.map(function (i) {
return xs[i];
})
.sort();
return xs
.map(function (x, i) {
var iIndex = indicesSorted.indexOf(i);
return iIndex !== -1 ? (
subsetSorted[iIndex]
) : x;
});
}
return disjointSort([7, 6, 5, 4, 3, 2, 1, 0], [6, 1, 7])
})();

View file

@ -0,0 +1,98 @@
(() => {
'use strict';
// disjointSort :: [Int] -> [Int] -> [Int]
const disjointSort = (indices, xs) => {
const
ks = sort(indices),
dct = mapFromList(
zip(ks, sort(map(k => xs[k], ks)))
);
return map(
(x, i) => {
const v = dct[i.toString()];
return undefined !== v ? v : x;
},
xs
);
};
// main :: IO ()
const main = () =>
showLog(
disjointSort(
[6, 1, 7],
[7, 6, 5, 4, 3, 2, 1, 0]
)
);
// GENERIC FUNCTIONS ----------------------------
// length :: [a] -> Int
const length = xs => xs.length || Infinity;
// map :: (a -> b) -> [a] -> [b]
const map = (f, xs) => xs.map(f);
// mapFromList :: [(k, v)] -> Dict
const mapFromList = kvs =>
kvs.reduce(
(a, kv) => {
const k = kv[0];
return Object.assign(a, {
[(('string' === typeof k) && k) || showJSON(k)]: kv[1]
});
}, {}
);
// showJSON :: a -> String
const showJSON = x => JSON.stringify(x);
// showLog :: a -> IO ()
const showLog = (...args) =>
console.log(
args
.map(JSON.stringify)
.join(' -> ')
);
// sort :: Ord a => [a] -> [a]
const sort = xs => xs.slice()
.sort((a, b) => a < b ? -1 : (a > b ? 1 : 0));
// take :: Int -> [a] -> [a]
// take :: Int -> String -> String
const take = (n, xs) =>
xs.constructor.constructor.name !== 'GeneratorFunction' ? (
xs.slice(0, n)
) : [].concat.apply([], Array.from({
length: n
}, () => {
const x = xs.next();
return x.done ? [] : [x.value];
}));
// Tuple (,) :: a -> b -> (a, b)
const Tuple = (a, b) => ({
type: 'Tuple',
'0': a,
'1': b,
length: 2
});
// Use of `take` and `length` here allows for zipping with non-finite
// lists - i.e. generators like cycle, repeat, iterate.
// zip :: [a] -> [b] -> [(a, b)]
const zip = (xs, ys) => {
const lng = Math.min(length(xs), length(ys));
return Infinity !== lng ? (() => {
const bs = take(lng, ys);
return take(lng, xs).map((x, i) => Tuple(x, bs[i]));
})() : zipGen(xs, ys);
};
// MAIN ---
return main();
})();