This repository has been archived by the owner on Oct 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 87
Add .toMap extensions for tuples/pairs and key+value records #359
Closed
tjarvstrand
wants to merge
1
commit into
dart-archive:master
from
tjarvstrand:iterable-record-extensions
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -914,6 +914,18 @@ extension IterableIterableExtension<T> on Iterable<Iterable<T>> { | |
}; | ||
} | ||
|
||
/// Extensions that apply to iterables of tuples/pairs (records with two unnamed elements). | ||
extension IterableTupleExtension<T1, T2> on Iterable<(T1, T2)> { | ||
/// Returns a map from the first element of each tuple to the second element. | ||
Map<T1, T2> get toMap => {for (final element in this) element.$1: element.$2}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (Just as style comments, not endorsing the functionality: |
||
} | ||
|
||
extension IterableKeyValueExtension<T1, T2> on Iterable<({T1 key, T2 value})> { | ||
/// Returns a map from the key of each element to the value of that element. | ||
Map<T1, T2> get toMap => | ||
{for (final element in this) element.key: element.value}; | ||
} | ||
|
||
/// Extensions that apply to iterables of [Comparable] elements. | ||
/// | ||
/// These operations can assume that the elements have a natural ordering, | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not keen on just using a positional pair as the representation of a key/value pair.
I'd use a type like
({T1 key, T2 value})
instead. I can see that there is a version below that does that, so I would just drop this one.It does get a little more verbose if you don't want to use
key
andvalue
as the names for the key and value.And if you do have a list of pairs, then having to do
.map((p) => (key: p.$1, value: p.$2))
does feel unnecessary.(Maps are not sets of pairs. Now that we have records,
Set<(...,...)>
s are sets of pairs!)