reduce

The reduce function takes closure as its argument and combines all the elements in a sequence to produce a single value. Here is the basic syntax for the reduce function:

Syntax:

let result = sequence.reduce(initial, combine)

The initial value is the starting value for the reduction, and the combined closure is a function that takes two values as its input and returns a single, combined value. The combined closure is applied to the initial value and the first element of the sequence, and then to the result of that operation and the next element of the sequence, and so on until all the elements have been processed.

Example 1:

Here is an example of using the reduce function to calculate the sum of an array of integers:

Swift




// Swift program to use reduce() function 
let numbers = [1, 2, 3, 4, 5]
  
// Finding the sum of an array of integers
let sum = numbers.reduce(0, { $0 + $1 })
  
print(sum)


Output:

15

In this example, the reduce function starts with an initial value of 0 and combines each element in the numbers array with the running total using the closure { $0 + $1 }.

You can also use the reduce function to perform more complex operations, such as generating a string by concatenating the elements of an array. 

Example 2:

In the following example, we are using reduce to concatenate an array of strings:

Swift




// Swift program to use reduce() function 
let words = ["geeks", "for", "geeks"]
  
// Concatenating the array of string
let sentence = words.reduce("", { $0 + " " + $1 })
  
print(sentence)


Output:

geeks for geeks

In this example, the reduce function starts with an initial value of an empty string and combines each element in the words array with the running total using the closure { $0 + ” ” + $1 }.



Higher-Order Functions in Swift

Higher-order functions are functions that take other functions as arguments or return functions as their output. These functions are an important aspect of functional programming, which is a programming paradigm that focuses on the use of functions to model computation. Higher-order functions enable developers to abstract common patterns of function applications and make code more concise and reusable.

In Swift, higher-order functions are a powerful tool for manipulating and transforming collections of data. Swift provides several higher-order functions as part of its standard library, including map, filter, reduce, and sorted. In this article, we will explore these functions in detail, along with some examples of how they can be used.

Similar Reads

map

The map function takes closure as its argument and applies it to each element in a sequence, returning a new sequence of transformed elements. Here is the basic syntax for the map function:...

compactMap

...

forEach

...

filter

The compactMap function is similar to the map function, but it filters out any nil values from the resulting sequence. This can be useful when working with options, as it allows you to transform a sequence of optional values into a non-optional sequence while ignoring any nil values....

Sorted

...

reduce

...

Contact Us