-
Notifications
You must be signed in to change notification settings - Fork 87
Add extension methods to package:collection. #135
Conversation
This is a first crack at adding extension methods to package:collection. They are not tested. Some are probably not useful (I expect |
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.
Did something get lost in the upload? There are no extension methods added here. This does look like it might be some work towards adding extensions, but it doesn't match the PR description as is.
Whoops. Forgot to git-add the new files. |
lib/src/list_extensions.dart
Outdated
} | ||
|
||
/// Maps each element and its index to a new value. | ||
Iterable<R> mapIndexed<R>(R convert(int index, E element)) sync* { |
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.
How much value are we buying by specializing this against Iterable.mapIndexed
? Do we expect this[index]
to be meaningfully different (faster?) than for(var element in this)
?
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.
Faster, but possibly not by a lot.
It has a better chance of ending up monomorphic.
We found a Contributor License Agreement for you (the sender of this pull request), but were unable to find agreements for all the commit author(s) or Co-authors. If you authored these, maybe you used a different email address in the git commits than was used to sign the CLA (login here to double check)? If these were authored by someone else, then they will need to sign a CLA as well, and confirm that they're okay with these being contributed to Google. ℹ️ Googlers: Go here for more info. |
A Googler has manually verified that the CLAs look good. (Googler, please make sure the reason for overriding the CLA status is clearly documented in these comments.) ℹ️ Googlers: Go here for more info. |
@lrhn looks stale? |
Was waiting for null-safety. I should be able to continue this now. |
CLAs look good, thanks! ℹ️ Googlers: Go here for more info. |
PTAL |
I had been waiting to review after the merge conflicts were resolved... Do you want me to do a first pass and then re-review after solving the conflicts? |
Conflicts should be gone now. |
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.
Overall I'm slightly worried about adding so many of these speculatively since there is a high cost to remove them, but given that they are extension methods I think the overall cost to keeping them is low.
Add a number of useful extension methods to List and Iterable, and to a few other types. Also add a quick-sort implementation that these can extension methods can defer to, one which allows sorting only a range of the list.
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.
Addressed comments.
lib/src/iterable_extensions.dart
Outdated
|
||
/// The elements of this iterable with duplicates removed. | ||
Iterable<T> unique() sync* { | ||
var seen = <T>{}; |
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.
Probably on the web, not on the VM.
On the other hand, most people use {}
, so if this is the only code using HashSet
, it will increase the compiled size of the web code. Dropping the method anyway.
lib/src/iterable_extensions.dart
Outdated
var result = <K, Set<T>>{}; | ||
for (var element in this) { | ||
var key = keyOf(element); | ||
result[key] = (result[key] ?? {})..add(element); |
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 don't think I was trying to optimize, just ended up here and didn't recognize it could be written even shorter.
lib/src/iterable_extensions.dart
Outdated
/// var parts = [1, 2, 3, 4, 5, 6, 7, 8, 9].split(isPrime); | ||
/// print(parts); // ([1], [2], [3, 4], [5, 6], [7, 8, 9]) | ||
/// ``` | ||
Iterable<List<T>> splitBefore(bool test(T element)) => |
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've seen requests for something like it, for example https://stackoverflow.com/questions/22274033/how-do-i-split-or-chunk-a-list-into-equal-parts-with-dart
lib/src/iterable_extensions.dart
Outdated
/// The sum of the elements. | ||
/// | ||
/// The sum is zero if the iterable is empty. | ||
T sum() { |
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 thought about it, and was on the fence about whether naming it for the operation or the result. If it's the operation, then it should be a method.
It's a little annoying that the best verb for the operation is also the best noun for the result. (Ditto for average
. Truly, any noun can be verbed.)
Let's make them getters.
…#135) Add a number of useful extension methods to `List` and `Iterable`, and to a few other types. A good part of these extensions are backed by the algorithms of `algorithms.dart`, which is expanded to support them: * Added `quickSort`. * Add extra optional `Random` argument to `shuffle`. * Generalize some of the functions in algorithms.dart to work on specific properties of the objects (`binarySearchBy`, `lowerBoundBy`, `insertionSortBy`, `quickSortBy`, `mergeSortBy`). The new methods are not exported from the library yet, they are intended to be used through the extension.
Add a number of useful extension methods to
List
andIterable
, and to a few other types.A good part of these extensions are backed by the algorithms of
algorithms.dart
, which is expanded to support them:quickSort
.Random
argument toshuffle
.algorithms.dart
to work on specific properties of the objects (binarySearchBy
,lowerBoundBy
,insertionSortBy
,quickSortBy
,mergeSortBy
).