-
Notifications
You must be signed in to change notification settings - Fork 59
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
Add filter method to IMap #123
Comments
You really don't need such method in I attached an example below for your convenience. IMap.fromPairs(
IMap.empty(StringOrder).pairs().filter((a) => true), // Put your predicate in here
StringOrder,
); |
Thank you for the example! |
We have a filter method implemented as an extension that uses a extension IMapX<K,V> on IMap<K,V> {
IMap<K, V> filter(bool f(K k, V v)) =>
this.foldLeftKV( IMap.empty(this.order), (a, K k, V v) => f(k,v) ? a.put(k, v) : a );
} You can use it like: final m = IMap.from(StringOrder, {"a":1,"b":2,"c":3});
final m2 = m.filter((k, v) => v != 2);
print("Map: $m Map2: $m2"); Edit: @Iri-Hor your comment about temporary extension IMapX<K,V> on IMap<K,V> {
/// Alternative IMap filter implementation that initializes with the original IMap and removes filtered items
IMap<K, V> filter(bool predicate(K k, V v)) =>
this.foldLeftKV(this, (acc, K k, V v) => predicate(k,v) ? acc : acc.remove(k));
} We have not benchmarked either of these implementations. |
The IList class has the convenient filter method:
filter(bool predicate(A a)) → IList
It would be handy to have the same mehtod in IMap
The text was updated successfully, but these errors were encountered: