Skip to content
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 whereKey, whereValue, whereKeyValue on Iterable<MapEntry> #22

Closed
MarcelGarus opened this issue Aug 5, 2020 · 3 comments · Fixed by #27
Closed

Add whereKey, whereValue, whereKeyValue on Iterable<MapEntry> #22

MarcelGarus opened this issue Aug 5, 2020 · 3 comments · Fixed by #27

Comments

@MarcelGarus
Copy link

Dealing with MapEntrys is cumbersome:

final differentChunks = _newChunks.entries
        .where((entry) => entry.value.isDirty)
        .where((entry) => entry.value.data != _originalChunks[entry.key])
        .toList();

Extension methods for whereKey, whereValue and whereKeyValue on Iterable<MapEntry<K, V>> could make this much more readable:

final differentChunks = _newChunks.entries
        .whereValue((chunk) => chunk.isDirty)
        .whereKeyValue((index, chunk) => chunk.data != _originalChunks[index])
        .toList();

Code context: I have a List of data chunks and a List of possibly changed data chunks, which have an isDirty property to indicate whether some values were changed. It's possible that some data is changed and then changed back again, so to get a List of only those chunks that are actually different than the other, I first filter by the isDirty property and only then do the expensive comparison of actual values.

@MarcelGarus
Copy link
Author

MarcelGarus commented Aug 5, 2020

Come to think of it, keys and value getters would also be very helpful.
I'd also be happy to file the PR.

@johnsonmh
Copy link
Collaborator

This an interesting idea, personally I feel like it might make more sense as an extension of Map rather than Iterable<MapEntry>.

Would something like this be similarly useful?

void main() {
  print({1: 2, 3: 5, 6: 7}.whereKey((key) => key % 2 == 0));
  // Returns {6: 7}
}

extension Foo on Map {
  Map whereKey<K, V>(bool Function(K) callback) {
    return Map.fromEntries(this.entries.where((entry) => callback(entry.key)));
  }
}

@MarcelGarus
Copy link
Author

MarcelGarus commented Aug 6, 2020

Yes, that would also solve what I'm trying to do. 👌
And because Map already has keys and values getters, they wouldn't need to be implemented anymore.

It's not quite as efficient though, because it would convert the Map into an Iterable<MapEntry> for every step instead of just acting on the Iterable<MapEntry> directly. This is especially noticeable if you would otherwise rely on the laziness of Iterables, for example by aborting after few elements: someReallyLargeMap.whereKey((key) => key.startsWith('blub')).values.take(5)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants