-
-
Notifications
You must be signed in to change notification settings - Fork 8
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
[DRAFT] Feature: Add findIndex
method to SortedSet
.
#187
base: main
Are you sure you want to change the base?
Conversation
Hi, thanks for working on this feature! As far as I'm concerned, if you see a use case for As to your question about So if a Let me know if you have further considerations. |
Thank you for your kind reply! I'll explain my usage of Now I understand the purpose of
|
My apologies, I did not fully explain the reason for For example, However, as long as it's not possible to "modify" the collection (even if it's already immutable), it's perfectly fine to pass a To support this, Rimbu has variant versions of collections. For instance, With import type { VariantSet } from '@rimbu/collection-types';
import { SortedSet } from '@rimbu/sorted';
const myStringSet = SortedSet.of('a', 'b');
function doSomethingWithSet(set: VariantSet<string | number>): boolean {
return set.has(3) || set.has('c');
}
console.log(doSomethingWithSet(myStringSet));
// writes 'false' Now for simple types this is not extremely useful. But consider if you are putting objects with some inheritance into the set: interface PricedItem {
name: string;
price: number;
}
interface Fruit extends PricedItem {
countryOfOrigin: string;
}
const mySet = SortedSet.of<Fruit>({
name: 'Apple',
price: 1.39,
countryOfOrigin: 'Belgium',
});
function calculatePrice(set: SortedSet<PricedItem>) {
return set.stream().fold(0, (result, item) => result + item.price);
}
function calculatePrice2(set: VariantSet<PricedItem>) {
return set.stream().fold(0, (result, item) => result + item.price);
}
calculatePrice(mySet); // not allowed, type error
calculatePrice2(mySet); // no problem This example is also convoluted since for this case you would probably use a This is the fundamental reason for |
By the way, for the |
This pull request adds the
findIndex
method toSortedSet
, complementing the existing getAtIndex method. However, it is not yet complete; I plan to include tests and extend the same method to SortedMap.Before proceeding, I have a few questions:
isComparable
in thecomp
? I introduced an error-throwing mechanism when the key is not comparable just to pass type check, as seen here. But I'm unsure why the argument type should be like that so that this step becomes necessary.Thank you for your guidance.
All Submissions:
New Feature Submissions:
Changes to Core Features: