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

Introduce IterableMapEntryExtension for use with Map.entries. #715

Merged
merged 2 commits into from
Nov 5, 2024

Conversation

jonasfj
Copy link
Member

@jonasfj jonasfj commented Nov 1, 2024

These utilities could often be useful when working with maps. We could also put this into a different utility package.

Example:

final myMap = {
  'foo': 42,
  'bar': -1,
  'foobar': 21,
};

// myMap without negative values
myMap.entries.whereValue((v) => v >= 0).toMap();

// myMap, but only keys that start with 'foo'
myMap.entries.whereKey((k) => k.startsWith('foo')).toMap();

I'm tempted to also suggest:

  Iterable<T> mapKeyValue<T>(T Function(K, V) convert) =>
      map((e) => convert(e.key, e.value));

But the name is a bit long. mapKV is lovely short, but probably too ugly.
When doing .map over Iterable<MapEntry> you generally don't want to write .map((entry) => entry.key.

Copy link

github-actions bot commented Nov 1, 2024

Package publishing

Package Version Status Publish tag (post-merge)
package:characters 1.4.0 ready to publish characters-v1.4.0
package:args 2.6.1-wip WIP (no publish necessary)
package:async 2.12.0 already published at pub.dev
package:collection 1.19.1-wip WIP (no publish necessary)
package:convert 3.1.2 already published at pub.dev
package:crypto 3.0.6 already published at pub.dev
package:fixnum 1.1.1 already published at pub.dev
package:logging 1.3.0 already published at pub.dev
package:os_detect 2.0.3-wip WIP (no publish necessary)
package:path 1.9.1 already published at pub.dev
package:platform 3.1.6 already published at pub.dev
package:typed_data 1.4.0 already published at pub.dev

Documentation at https://github.com/dart-lang/ecosystem/wiki/Publishing-automation.

Copy link

github-actions bot commented Nov 1, 2024

PR Health

Breaking changes ⚠️
Package Change Current Version New Version Needed Version Looking good?
collection Non-Breaking 1.19.1 1.19.1-wip 1.20.0
Got "1.19.1-wip" expected >= "1.20.0" (non-breaking changes)
⚠️

This check can be disabled by tagging the PR with skip-breaking-check.

Changelog Entry ✔️
Package Changed Files

Changes to files need to be accounted for in their respective changelogs.

Coverage ✔️
File Coverage
pkgs/collection/lib/src/iterable_extensions.dart 💚 100 %

This check for test coverage is informational (issues shown here will not fail the PR).

API leaks ✔️

The following packages contain symbols visible in the public API, but not exported by the library. Export these symbols or remove them from your publicly visible API.

Package Leaked API symbols
License Headers ✔️
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
Files
no missing headers

All source files should start with a license header.

**Example**:
```dart
final myMap = {
  'foo': 42,
  'bar': -1,
  'foobar': 21,
};

// myMap without negative values
myMap.entries.whereValue((v) => v >= 0).toMap();

// myMap, but only keys that start with 'foo'
myMap.entries.whereKey((k) => k.startsWith('foo')).toMap();
```
Copy link
Member

@lrhn lrhn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it.
I'm not awfully happy about people working on MapEntrys in general, but this seems to be withing reason (it's not just treating them as arbitrary pairs).

pkgs/collection/lib/src/iterable_extensions.dart Outdated Show resolved Hide resolved
/// Creates a new lazy [Iterable] with all elements whose [MapEntry.value]
/// satisfy the predicate [test].
Iterable<MapEntry<K, V>> whereValue(bool Function(V) test) =>
where((e) => test(e.value));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Too bad where is taken, so it can't be where({bool Function(K)? key, bool Function(V)? value}))

Copy link
Member Author

@jonasfj jonasfj Nov 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I feel the same with .map.

But I wouldn't make where({bool Function(K)? key, bool Function(V)? value}).
I would make: where(bool Function(K, V) test), ignoring a parameter in a closure is not that bad.

Maybe, we should consider adding:

  • wherePair(bool Function(K, V) test)
  • mapPair<T>(T Function(K, V) toElement)

We could consider adding such extension methods to:

  • Iterable<MapEntry<K, V>>, and,
  • Iterable<(A, B)>.

But that seems outside the scope of this PR.

And it's entirely possible that a language feature providing pattern matching in the signature of a closure is better.

Copy link
Member

@lrhn lrhn Nov 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe whereEntry/mapEntry/anyEntry/everyEntry with an X Function(K, V) argument. (Don't say "pair", it's not — just — a pair! It's a key-and-value.)
Some of those (not whereEntry) could be extensions to Map<K,V> too. (Not saying they should, but they could.)

pkgs/collection/lib/src/iterable_extensions.dart Outdated Show resolved Hide resolved
pkgs/collection/lib/src/iterable_extensions.dart Outdated Show resolved Hide resolved
pkgs/collection/lib/src/iterable_extensions.dart Outdated Show resolved Hide resolved
pkgs/collection/lib/src/iterable_extensions.dart Outdated Show resolved Hide resolved
pkgs/collection/test/extensions_test.dart Outdated Show resolved Hide resolved
pkgs/collection/test/extensions_test.dart Show resolved Hide resolved
pkgs/collection/test/extensions_test.dart Show resolved Hide resolved
pkgs/collection/test/extensions_test.dart Show resolved Hide resolved
@jonasfj jonasfj merged commit 9e37915 into dart-lang:main Nov 5, 2024
14 checks passed
@jonasfj jonasfj deleted the IterableMapEntryExtension branch November 5, 2024 15:33
Copy link
Member

@lrhn lrhn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM (Rubberstamp!)

@@ -1,5 +1,5 @@
name: collection
version: 1.19.1
version: 1.19.1-wip
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be 1.20.0, unless 1.19.0 hasn't been released yet.
(Adds new feature ⇒ minor version increment.)

copybara-service bot pushed a commit to dart-lang/sdk that referenced this pull request Nov 7, 2024
Revisions updated by `dart tools/rev_sdk_deps.dart`.

core (https://github.com/dart-lang/core/compare/4b62792..1156cfe):
  1156cfe4  2024-11-06  Jacob MacDonald  Use `values` iterator  and `update` in map equality/hash (dart-lang/core#718)
  9ab5a189  2024-11-05  Jacob MacDonald  add a basic benchmark for DeepCollectionEquality (dart-lang/core#717)
  9e379158  2024-11-05  Jonas Finnemann Jensen  Introduce `IterableMapEntryExtension` for use with `Map.entries`. (dart-lang/core#715)

dartdoc (https://github.com/dart-lang/dartdoc/compare/5168f81..24c2a96):
  24c2a966  2024-11-04  dependabot[bot]  Bump the github-actions group across 1 directory with 4 updates (dart-lang/dartdoc#3922)

http (https://github.com/dart-lang/http/compare/8db0d0a..03ced4d):
  03ced4d  2024-11-06  Brian Quinlan  Upgrade flutter_http_example to cupertino_http (dart-lang/http#1393)
  df59bac  2024-11-05  Brian Quinlan  Release package:cupertino_http 2.0.1 (dart-lang/http#1400)
  0544e1f  2024-11-05  Brian Quinlan  Fix a bug where cupertino_http did not work on iOS<17. (dart-lang/http#1399)

sse (https://github.com/dart-lang/sse/compare/1b02011..befbd6d):
  befbd6d  2024-11-04  dependabot[bot]  Bump actions/checkout from 4.2.0 to 4.2.2 in the github-actions group (dart-archive/sse#118)

Change-Id: I2b4ebfe2f56f9478c7e89cd2d3e50485ae44caa0
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/393943
Reviewed-by: Konstantin Shcheglov <[email protected]>
Commit-Queue: Devon Carew <[email protected]>
Auto-Submit: Devon Carew <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants