Skip to content

Adding sum and average extensions - #98

Merged
kamaal111 merged 1 commit into
mainfrom
more-extensions
Aug 16, 2025
Merged

Adding sum and average extensions#98
kamaal111 merged 1 commit into
mainfrom
more-extensions

Conversation

@kamaal111

@kamaal111 kamaal111 commented Aug 16, 2025

Copy link
Copy Markdown
Member

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 any AdditiveArithmetic type
  • Added average(of:) methods with overloads for BinaryFloatingPoint and BinaryInteger types
  • Added comprehensive test coverage for the new functionality including edge cases like empty arrays

@kamaal111
kamaal111 requested a review from Copilot August 16, 2025 14:56

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 any AdditiveArithmetic type
  • Added average(of:) methods with overloads for BinaryFloatingPoint and BinaryInteger types
  • 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)

Copilot AI Aug 16, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copilot uses AI. Check for mistakes.
@kamaal111
kamaal111 marked this pull request as ready for review August 16, 2025 14:59
@kamaal111
kamaal111 merged commit 1525ccf into main Aug 16, 2025
2 checks passed
@kamaal111
kamaal111 deleted the more-extensions branch August 16, 2025 15:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants