Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Commit

Permalink
Add .toMap extensions for tuples/pairs and key+value records
Browse files Browse the repository at this point in the history
  • Loading branch information
tjarvstrand committed Sep 13, 2024
1 parent 24b75d8 commit adf8ba6
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/src/iterable_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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};
}

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,
Expand Down

0 comments on commit adf8ba6

Please sign in to comment.