compactMap

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.

Syntax:

let transformed = sequence.compactMap(transform)

The transform closure is a function that takes an element of the sequence as its input and returns a transformed version of the element.

Example 1:

In the following example, we are using the compactMap function to transform an array of optional integers into a non-optional array:

Swift




// Swift program to use compactMap function 
let optionals: [Int?] = [1, 2, nil, 4, nil]
  
// Transforming an array of optional integers into a non-optional array
// Using compactMap
let integers = optionals.compactMap { $0 }
  
print(integers)


Output:

[1, 2, 4]

In this example, the compactMap() function applies the closure { $0 } to each element in the options array, resulting in a new array with the non-nil elements. Any nil values are automatically filtered out.

You can also use the compactMap function to transform elements of a sequence into a different type, just like with the map function. 

Example 2:

In the following example, we are using the compactMap() to convert an array of optional strings to an array of integers:

Swift




// Swift program to use compactMap function 
let optionals: [String?] = ["1", "2", "nil", "4", "nil"]
  
// Converting an array of optional strings to an array of integers
// Using compactMap() function
let integers = optionals.compactMap { Int($0!) }
print(integers)


Output:

[1, 2, 4]

In this example, the compactMap() function applies the closure { Int($0!) } to each element in the optionals array, converting each string to an integer using the Int initializer. Any nil values are automatically filtered out.

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