-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add "sum" function for Sequences with AnyCurrency elements
Motivation: It is common for developers to want a quick sum of values with currencies, to display totals. To mirror functionality found on Sequence protocols with `max` and `min`, a `sum` with a predicate overload should be provided. Modifications: - Add a `sum` function that starts with a zero value and adds all the elements together - Add a `sum(where:)` overload that filters the elements by a predicate before summing Result: It should be more natural and easy for developers to quickly get a sum of currencies.
- Loading branch information
Showing
5 changed files
with
668 additions
and
686 deletions.
There are no files selected for viewing
This file contains 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,49 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Currency open source project | ||
// | ||
// Copyright (c) 2020 Currency project authors | ||
// Licensed under MIT License | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of Currency project authors | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import Foundation | ||
|
||
extension Sequence where Element: AnyCurrency { | ||
/// Returns the sum total of all amounts in the sequence. | ||
/// | ||
/// For example: | ||
/// | ||
/// let amounts: [USD] = [304.98, 19.02] | ||
/// let sumTotal = amounts.sum() | ||
/// print(sumTotal) | ||
/// // prints "324" | ||
/// | ||
/// If the sequence has no elements, you will receive a currency with a value of "0". | ||
/// - Complexity: O(*n*) , where *n* is the length of the sequence. | ||
/// - Returns: A currency value representing the sum total of all the amounts in the sequence. | ||
public func sum() -> Element { | ||
return self.reduce(into: .init(.zero), { $0 += $1 }) | ||
} | ||
|
||
/// Returns the sum total of all amounts in the sequence that satify the given predicate. | ||
/// For example: | ||
/// | ||
/// let amounts: [USD] = [304.98, 19.02, 30.21] | ||
/// let sumTotal = amounts.sum(where: { $0.roundedAmount > 20 }) | ||
/// print(sumTotal) | ||
/// // prints "335.19" | ||
/// | ||
/// - Complexity: O(*n*), where *n* is the length of the sequence. | ||
/// - Parameter predicate: A closure that takes a currency element as its argument | ||
/// and returns a Boolean value that indicates whether the passed element should be included in the sum. | ||
/// - Returns:A currency value representing the sum total of all the amounts in the sequence that satisfies `predicate`. | ||
public func sum(where predicate: (Element) throws -> Bool) rethrows -> Element { | ||
return try self.filter(predicate).sum() | ||
} | ||
} |
This file contains 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 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
Oops, something went wrong.