-
Notifications
You must be signed in to change notification settings - Fork 661
feat(collections): add sumOf #1108
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
c2b59a1
add sumof tests
grian32 08ea479
add sumof, fix test
grian32 8c37da4
some other test stuff
grian32 4c1ec8e
lint, fmt
grian32 fcde415
fix example code hopefully
grian32 434ca79
even more fmt
grian32 06d7f6a
export sumof from mod.ts
grian32 6514b0d
add more sumof tests
grian32 8ff42a6
add more object tests
grian32 93c46d3
fmt
grian32 a3f412e
leon review
grian32 9c5395e
add extra tests
grian32 e8e1d05
test fmt
grian32 d461866
Merge branch 'main' into main
grian32 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 hidden or 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
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. | ||
|
|
||
| /** | ||
| * Applies the given selector to all elements in the given collection and calculates the sum of the results | ||
| * | ||
| * Example: | ||
| * | ||
| * ```ts | ||
| * import { sumOf } from "./sum_of.ts" | ||
| * import { assertEquals } from "../testing/asserts.ts" | ||
| * | ||
| * const people = [ | ||
| * { name: 'Anna', age: 34 }, | ||
| * { name: 'Kim', age: 42 }, | ||
| * { name: 'John', age: 23 }, | ||
| * ] | ||
| * const totalAge = sumOf(people, i => i.age) | ||
| * | ||
| * assertEquals(totalAge, 99) | ||
| * ``` | ||
| */ | ||
| export function sumOf<T>( | ||
| array: Array<T>, | ||
| selector: (el: T) => number, | ||
| ): number { | ||
| let sum = 0; | ||
|
|
||
| for (const i of array) { | ||
| sum += selector(i); | ||
| } | ||
|
|
||
| return sum; | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. | ||
|
|
||
| import { assertEquals } from "../testing/asserts.ts"; | ||
| import { sumOf } from "./sum_of.ts"; | ||
|
|
||
| Deno.test("[collections/sumOf] On object properties", () => { | ||
| const object = [ | ||
| { name: "Kyle", age: 34 }, | ||
| { name: "John", age: 42 }, | ||
| { name: "Anna", age: 23 }, | ||
| ]; | ||
|
|
||
| const actual = sumOf(object, (i) => i.age); | ||
grian32 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| assertEquals(actual, 99); | ||
| }); | ||
|
|
||
| Deno.test("[collections/sumOf] Add 2 to each num", () => { | ||
| const array = [1, 2, 3]; | ||
|
|
||
| const actual = sumOf(array, (i) => i + 2); | ||
|
|
||
| assertEquals(actual, 12); | ||
| }); | ||
|
|
||
| Deno.test("[collections/sumOf] Regular sum", () => { | ||
| const array = [1, 2, 3]; | ||
|
|
||
| const actual = sumOf(array, (i) => i); | ||
|
|
||
| assertEquals(actual, 6); | ||
| }); | ||
|
|
||
| Deno.test("[collections/sumOf] Negatives with regular sum", () => { | ||
| const array = [-1, -2, -3]; | ||
|
|
||
| const actual = sumOf(array, (i) => i); | ||
|
|
||
| assertEquals(actual, -6); | ||
| }); | ||
|
|
||
| Deno.test("[collections/sumOf] Mixed negatives and positives with regular sum", () => { | ||
| const array = [-1, 2, 3, -5]; | ||
|
|
||
| const actual = sumOf(array, (i) => i); | ||
|
|
||
| assertEquals(actual, -1); | ||
| }); | ||
|
|
||
| Deno.test("[collections/sumBy] Selector turns nums into negatives", () => { | ||
| const array = [1, 3, 5, 3]; | ||
|
|
||
| const actual = sumOf(array, (i) => i - 6); | ||
|
|
||
| assertEquals(actual, -12); | ||
| }); | ||
|
|
||
| Deno.test("[collections/sumBy] Selector turns nums into zeros", () => { | ||
| const array = [3, 3, 3, 3]; | ||
|
|
||
| const actual = sumOf(array, (i) => i - 3); | ||
|
|
||
| assertEquals(actual, 0); | ||
| }); | ||
|
|
||
| Deno.test("[collections/sumOf] On negative object properties", () => { | ||
| const object = [ | ||
| { name: "Kyle", age: -34 }, | ||
| { name: "John", age: -42 }, | ||
| { name: "Anna", age: -23 }, | ||
| ]; | ||
|
|
||
| const actual = sumOf(object, (i) => i.age); | ||
|
|
||
| assertEquals(actual, -99); | ||
| }); | ||
|
|
||
| Deno.test("[collections/sumOf] On mixed object properties", () => { | ||
| const object = [ | ||
| { name: "Kyle", age: -34 }, | ||
| { name: "John", age: 42 }, | ||
| { name: "Anna", age: -23 }, | ||
| ]; | ||
|
|
||
| const actual = sumOf(object, (i) => i.age); | ||
|
|
||
| assertEquals(actual, -15); | ||
| }); | ||
|
|
||
| Deno.test("[collections/sumOf] No mutation", () => { | ||
| const array = [1, 2, 3, 4]; | ||
|
|
||
| sumOf(array, (i) => i + 2); | ||
|
|
||
| assertEquals(array, [1, 2, 3, 4]); | ||
| }); | ||
|
|
||
| Deno.test("[collections/sumOf] Empty array results in 0", () => { | ||
| const array: number[] = []; | ||
|
|
||
| const actual = sumOf(array, (i) => i + 2); | ||
|
|
||
| assertEquals(actual, 0); | ||
| }); | ||
|
|
||
| Deno.test("[collections/sumOf] NaN and Infinity", () => { | ||
| const array = [ | ||
| 1, | ||
| 2, | ||
| Number.POSITIVE_INFINITY, | ||
| 3, | ||
| 4, | ||
| Number.NEGATIVE_INFINITY, | ||
| 5, | ||
| 6, | ||
| Number.NaN, | ||
| 7, | ||
| 8, | ||
| ]; | ||
|
|
||
| const actual = sumOf(array, (i) => i); | ||
|
|
||
| assertEquals(actual, NaN); | ||
| }); | ||
|
|
||
| Deno.test("[collections/sumOf] Infinity", () => { | ||
| const array = [1, 2, Infinity, 3, 4, 5, 6, 7, 8]; | ||
|
|
||
| const actual = sumOf(array, (i) => i); | ||
|
|
||
| assertEquals(actual, Infinity); | ||
| }); | ||
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.
Uh oh!
There was an error while loading. Please reload this page.