Skip to content

Latest commit

 

History

History
17 lines (12 loc) · 845 Bytes

File metadata and controls

17 lines (12 loc) · 845 Bytes

Reduce

Did you know what the reduce function is intended for in Swift programming? It's essentially designed to condense an array into a single value by repeatedly applying a given operation. Think of it as a way to reduce your collection down to one result, whether that's summing up numbers, concatenating strings, or more complex custom operations. It's a versatile tool that takes two parameters: an initial result and a closure that defines how to combine the elements.

let numbers = [1, 2, 3, 4, 5]
let sum = numbers.reduce(0, { $0 + $1 })
print(sum) // Output: 15

let words = ["Swift", "programming", "is", "fun!"]
let sentence = words.reduce("") { $0 == "" ? $1 : $0 + " " + $1 }
print(sentence) // Output: "Swift programming is fun!"

Reference

YouTube 👀