New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
can add a Set operate: R.isSubset ? #1914
Comments
could be interesting, are you willing to do a PR? A couple of notes: I think the signature would have to be |
Are you suggesting the native If it were
|
Since Array(list) is not set, isSubset should use uniq and sort, like union and intersection. |
const isSubset = R.curry((set, subset) => {
const sortU = R.compose(R.sort((a, b) => a - b), R.uniq);
return R.equals(sortU(R.intersection(set, subset)), sortU(subset));
});
console.log(isSubset([1, 2], [1, 2, 3])); // true
console.log(isSubset([1, 2, 2, 2], [1, 2, 3])); // true
console.log(isSubset([1, 2, 3, 4], [1, 2, 3])); // false list's subset looks wired. |
Of course not everything we might want to test for uniqueness/subset is necessarily ordered. I don't think an actual implementation could use any kind of |
another implementation:
|
I don't like using arbitrary lists as arguments to a function called const set = [2, 1, 3, 1, 3]; // Not a Set
const subset = [2, 2, 2, 1, 2]; // // Not a Set
isSubset(set, subset) // should throw TypeError Simplest would be to find a better name ( |
...or conceivably to support something like sanctuary-js/sanctuary-set#1. |
OK, I will take a look at that repository to see what should I do. |
That's a huge piece of work. I was really just riffing on the suggestion from @buzzdecafe. Ramda generally does not supply types. But because of our insistence on value equality, the native Set type is fairly useless to us; something like the work here might be more helpful. But it's a large undertaking, not a quick PR, I'm afraid. |
@adispring Yet another implementation taking the subset as first argument (maybe for the Cookbook along var isSubset = R.compose(R.isEmpty, R.useWith(R.difference, [R.uniq, R.uniq]));
console.log(isSubset([1, 2], [1, 2, 3])); // true
console.log(isSubset([1, 2, 2, 2], [1, 2, 3])); // true
console.log(isSubset([1, 2, 3, 4], [1, 2, 3])); // false
isSubset([2, 2, 2, 1, 2], [2, 1, 3, 1, 3]); // true |
I pretty much prefer the implementation of @adispring . Not only it is simpler but for me it's also more useful to take the big set first. |
const isSubset = R.curry((xs, ys) =>
R.all(R.contains(R.__, ys), xs)) |
I think having an Regarding the naming, I guess an alternative name could be |
Yet another implementation would be to check whether the union equals const sortU = R.pipe(R.uniq, R.sortBy(R.identity));
const isSubset = (subset, set) => R.equals(sortU(R.union(subset, set)), sortU(set)); Another alternative would be to compare the cardinalities of the sets, rather than the sets themselves: const isSubset = (subset, set) => R.equals(R.length(R.intersection(subset, set)), R.length(subset)); (This follows on from the first method by @adispring - checking whether intersection equals See here. |
Are you interested in submitting a PR for this? |
@CrossEye |
The text was updated successfully, but these errors were encountered: