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

fix types in sumBy to enable summing a list full of doubles #71

Merged
merged 1 commit into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/src/base/iterable_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,11 @@ extension FicIterableExtension<T> on Iterable<T> {
/// expect(['a', 'ab', 'abc', 'abcd', 'abcde'].sumBy((e) => e.length), 15);
/// ```
N sumBy<N extends num>(N Function(T element) mapper) {
N result = 0 as N;
num result = 0;
for (final value in this) {
result = result + mapper(value) as N;
result = result + mapper(value);
}
return result;
return result as N;
}

/// The arithmetic mean of the elements of a non-empty iterable.
Expand Down
1 change: 1 addition & 0 deletions test/base/iterable_extension_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ void main() {
test('sumBy', () {
expect([1, 2, 3, 4, 5].sumBy((e) => e), 15);
expect([1.5, 2.5, 3.3, 4, 5].sumBy((e) => e), 16.3);
expect([1.5, 2.5, 3.3, 4.2, 5.9].sumBy((e) => e), 17.4);
expect([].sumBy((e) => (e is int) ? e : 0), 0);
expect([''].sumBy((e) => e.length), 0);
expect(['a'].sumBy((e) => e.length), 1);
Expand Down