Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
|
|
@ -0,0 +1,16 @@
|
|||
// in A but not in B
|
||||
function relative_complement(A, B) {
|
||||
return A.filter(function(elem) {return B.indexOf(elem) == -1});
|
||||
}
|
||||
|
||||
// in A or in B but not in both
|
||||
function symmetric_difference(A,B) {
|
||||
return relative_complement(A,B).concat(relative_complement(B,A));
|
||||
}
|
||||
|
||||
var a = ["John", "Serena", "Bob", "Mary", "Serena"].unique();
|
||||
var b = ["Jim", "Mary", "John", "Jim", "Bob"].unique();
|
||||
|
||||
print(a);
|
||||
print(b);
|
||||
print(symmetric_difference(a,b));
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
function Difference(A,B)
|
||||
{
|
||||
var a = A.length, b = B.length, c = 0, C = [];
|
||||
for (var i = 0; i < a; i++)
|
||||
{ var j = 0, k = 0;
|
||||
while (j < b && B[j] !== A[i]) j++;
|
||||
while (k < c && C[k] !== A[i]) k++;
|
||||
if (j == b && k == c) C[c++] = A[i];
|
||||
}
|
||||
return C;
|
||||
}
|
||||
|
||||
function SymmetricDifference(A,B)
|
||||
{
|
||||
var D1 = Difference(A,B), D2 = Difference(B,A),
|
||||
a = D1.length, b = D2.length;
|
||||
for (var i = 0; i < b; i++) D1[a++] = D2[i];
|
||||
return D1;
|
||||
}
|
||||
|
||||
|
||||
/* Example
|
||||
A = ['John', 'Serena', 'Bob', 'Mary', 'Serena'];
|
||||
B = ['Jim', 'Mary', 'John', 'Jim', 'Bob'];
|
||||
|
||||
Difference(A,B); // 'Serena'
|
||||
Difference(B,A); // 'Jim'
|
||||
SymmetricDifference(A,B); // 'Serena','Jim'
|
||||
*/
|
||||
|
|
@ -0,0 +1,64 @@
|
|||
(() => {
|
||||
'use strict';
|
||||
|
||||
const symmetricDifference = (xs, ys) =>
|
||||
union(difference(xs, ys), difference(ys, xs));
|
||||
|
||||
|
||||
// GENERIC FUNCTIONS ------------------------------------------------------
|
||||
|
||||
// First instance of x (if any) removed from xs
|
||||
// delete_ :: Eq a => a -> [a] -> [a]
|
||||
const delete_ = (x, xs) => {
|
||||
const i = xs.indexOf(x);
|
||||
return i !== -1 ? (xs.slice(0, i)
|
||||
.concat(xs.slice(i, -1))) : xs;
|
||||
};
|
||||
|
||||
// (\\) :: (Eq a) => [a] -> [a] -> [a]
|
||||
const difference = (xs, ys) =>
|
||||
ys.reduce((a, x) => filter(z => z !== x, a), xs);
|
||||
|
||||
// filter :: (a -> Bool) -> [a] -> [a]
|
||||
const filter = (f, xs) => xs.filter(f);
|
||||
|
||||
// flip :: (a -> b -> c) -> b -> a -> c
|
||||
const flip = f => (a, b) => f.apply(null, [b, a]);
|
||||
|
||||
// foldl :: (b -> a -> b) -> b -> [a] -> b
|
||||
const foldl = (f, a, xs) => xs.reduce(f, a);
|
||||
|
||||
// nub :: [a] -> [a]
|
||||
const nub = xs => {
|
||||
const mht = unconsMay(xs);
|
||||
return mht.nothing ? xs : (
|
||||
([h, t]) => [h].concat(nub(t.filter(s => s !== h)))
|
||||
)(mht.just);
|
||||
};
|
||||
|
||||
// show :: a -> String
|
||||
const show = x => JSON.stringify(x, null, 2);
|
||||
|
||||
// unconsMay :: [a] -> Maybe (a, [a])
|
||||
const unconsMay = xs => xs.length > 0 ? {
|
||||
just: [xs[0], xs.slice(1)],
|
||||
nothing: false
|
||||
} : {
|
||||
nothing: true
|
||||
};
|
||||
|
||||
// union :: [a] -> [a] -> [a]
|
||||
const union = (xs, ys) => {
|
||||
const sx = nub(xs);
|
||||
return sx.concat(foldl(flip(delete_), nub(ys), sx));
|
||||
};
|
||||
|
||||
// TEST -------------------------------------------------------------------
|
||||
const
|
||||
a = ["John", "Serena", "Bob", "Mary", "Serena"],
|
||||
b = ["Jim", "Mary", "John", "Jim", "Bob"];
|
||||
|
||||
return show(
|
||||
symmetricDifference(a, b)
|
||||
);
|
||||
})();
|
||||
|
|
@ -0,0 +1 @@
|
|||
["Serena", "Jim"]
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
const symmetricDifference = (...args) => {
|
||||
let result = new Set();
|
||||
for (const x of args)
|
||||
for (const e of new Set(x))
|
||||
if (result.has(e)) result.delete(e)
|
||||
else result.add(e);
|
||||
|
||||
return [...result];
|
||||
}
|
||||
// TEST -------------------------------------------------------------------
|
||||
console.log(symmetricDifference(["Jim", "Mary", "John", "Jim", "Bob"],["John", "Serena", "Bob", "Mary", "Serena"]));
|
||||
console.log(symmetricDifference([1, 2, 5], [2, 3, 5], [3, 4, 5]));
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
["Jim", "Serena"]
|
||||
[1, 4, 5]
|
||||
Loading…
Add table
Add a link
Reference in a new issue