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

Added product() and product(for:) methods to Sequence #1168

Merged
merged 2 commits into from
Jan 30, 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ The changelog for **SwifterSwift**. Also see the [releases](https://github.com/S
### Added
- **Measurement**
- Added `+=`, `-=`, `*=`, `/=` to add, subtract, multiply and divide measurements. [#1162](https://github.com/SwifterSwift/SwifterSwift/pull/1162) by [Roman Podymov](https://github.com/RomanPodymov)
- **Sequence**
- Added `product()` for calculating the product of all `Numeric` elements. [#1168](https://github.com/SwifterSwift/SwifterSwift/pull/1168) by [MartonioJunior](https://github.com/MartonioJunior)
- Added `product(for:)` for calculating the product of the `Numeric` property for all elements in `Sequence`. [#1168](https://github.com/SwifterSwift/SwifterSwift/pull/1168) by [MartonioJunior](https://github.com/MartonioJunior)
- **UIView**
- Added `removeBlur()` method for removing the applied blur effect from the view. [#1159](https://github.com/SwifterSwift/SwifterSwift/pull/1159) by [regi93](https://github.com/regi93)
- Added `makeCircle(diameter:)` method to make the view circular. [#1165](https://github.com/SwifterSwift/SwifterSwift/pull/1165) by [happyduck-git](https://github.com/happyduck-git)
Expand Down
21 changes: 21 additions & 0 deletions Sources/SwifterSwift/SwiftStdlib/SequenceExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,16 @@ public extension Sequence {
// Inspired by: https://swiftbysundell.com/articles/reducers-in-swift/
return reduce(.zero) { $0 + $1[keyPath: keyPath] }
}

/// SwifterSwift: Product of all elements in sequence.
///
/// ["James", "Wade", "Bryant"].product(for: \.count) -> 120
///
/// - Parameter map: Mapping function to `Numeric` value.
/// - Returns: product of the sequence's elements.
func product<T: Numeric>(for map: (Element) -> T) -> T {
reduce(1) { $0 * map($1) }
}

/// SwifterSwift: Returns the first element of the sequence with having property by given key path equals to given
/// `value`.
Expand Down Expand Up @@ -308,3 +318,14 @@ public extension Sequence where Element: AdditiveArithmetic {
return reduce(.zero, +)
}
}

// MARK: - Methods (Numeric)

public extension Sequence where Element: Numeric {
/// SwifterSwift: Product of all elements in sequence.
///
/// [1, 2, 3, 4, 5].product() -> 120
///
/// - Returns: product of the sequence's elements.
func product() -> Element { reduce(1, *) }
}
10 changes: 10 additions & 0 deletions Tests/SwiftStdlibTests/SequenceExtensionsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,16 @@ final class SequenceExtensionsTests: XCTestCase {
XCTAssertEqual(["James", "Wade", "Bryant"].sum(for: \.count), 15)
XCTAssertEqual(["a", "b", "c", "d"].sum(for: \.count), 4)
}

func testProduct() {
XCTAssertEqual([1, 2, 3, 4, 5].product(), 120)
XCTAssertEqual([1.2, 2.3, 3.4, 4.5, 5.6].product(), 236.4768, accuracy: 0.001)
}

func testMapFunctionProduct() {
XCTAssertEqual(["James", "Wade", "Bryant"].product(for: \.count), 120)
XCTAssertEqual(["a", "b", "c", "d"].product(for: \.count), 1)
}

func testKeyPathSorted() {
let array = ["James", "Wade", "Bryant"]
Expand Down