Adding sum and average extensions - #98
Merged
Merged
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR adds new array extension methods for calculating sums and averages of array elements using key paths. It introduces support for both numeric types that conform to AdditiveArithmetic and provides specialized handling for floating-point and integer types.
Key changes:
- Added
sum(by:)method that calculates sums using key paths for anyAdditiveArithmetictype - Added
average(of:)methods with overloads forBinaryFloatingPointandBinaryIntegertypes - Added comprehensive test coverage for the new functionality including edge cases like empty arrays
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| Sources/KamaalExtensions/Array.swift | Implements sum and average extension methods with comprehensive documentation |
| Tests/KamaalExtensionsTests/ArrayTests.swift | Adds extensive test coverage for new sum and average methods, plus formatting improvements to existing tests |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| ] | ||
|
|
||
| let avgAge = people.average(of: \.age) | ||
| #expect(abs(avgAge - 21.3333333333) < 0.000001) |
There was a problem hiding this comment.
The magic number 21.3333333333 should be calculated as 64.0/3.0 to make the expected result clearer and more maintainable.
Suggested change
| #expect(abs(avgAge - 21.3333333333) < 0.000001) | |
| #expect(abs(avgAge - (64.0 / 3.0)) < 0.000001) |
kamaal111
force-pushed
the
more-extensions
branch
from
August 16, 2025 14:57
b23135f to
b1b3a0d
Compare
kamaal111
force-pushed
the
more-extensions
branch
from
August 16, 2025 14:59
b1b3a0d to
8912fc5
Compare
kamaal111
marked this pull request as ready for review
August 16, 2025 14:59
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This PR adds new array extension methods for calculating sums and averages of array elements using key paths. It introduces support for both numeric types that conform to AdditiveArithmetic and provides specialized handling for floating-point and integer types.
Key changes: