Skip to content

Commit

Permalink
Added product function to Sequence (#1168)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartonioJunior authored Jan 30, 2024
1 parent a8aee67 commit cf79269
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
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

0 comments on commit cf79269

Please sign in to comment.