Skip to content

Instantly share code, notes, and snippets.

@knowsudhanshu
Last active July 20, 2022 08:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save knowsudhanshu/abfd566b1311d4fb0a2760f657f02bd2 to your computer and use it in GitHub Desktop.
Save knowsudhanshu/abfd566b1311d4fb0a2760f657f02bd2 to your computer and use it in GitHub Desktop.
extension Sequence {
/// Reduce: applies a given operation on elements of a given sequence and returns a single value (of same type as of the elements in given sequence) at the end
/// - Parameters:
/// - initialValue: Initial value for the operation to start
/// - operation: function that'll be applied on elements of the given sequence
/// - Returns: final value after applying operation on the elements of given sequence
func myReduce(_ initialValue: Element,
_ operation: (_ value1: Element, _ value2: Element) -> Element) -> Element {
var output: Element = initialValue
for item in self {
output = operation(output, item)
}
return output
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment