Blog / May 29, 2022 / 3 mins read / By Mahi Garg

Filter Operator: Swift

The collection is something which is used by almost everyone. It makes our life easy. Array, Set, and Dictionary are the best examples of them.

To iterate, filter, or modify the existing collection object, Swift provides us with a few in builds transform operators. Filter operator is one of them. Let’s try to understand the filter operator in detail.

What is filter operator?

The filter operator is used to create a new collection object by iterating over the existing collection object and filtering the elements based on the predicates to it.

The predicate that needs to be checked for, is passed as a higher-order function named isIncluded to the filter function.

Since the filter operator is present in Iterator, it can be used with all three collections ie Array, Set, and Dictionary.

/// Returns an array containing, in order, the elements of the sequence that satisfy the given predicate.
/// - Parameter isIncluded: A closure that takes an element of the sequence as its argument and returns a Boolean value indicating whether the element should be included in the returned array.
/// - Returns: An array of the elements that `isIncluded` allowed.
/// - Complexity: O(*n*), where *n* is the length of the sequence.
@inlinable public func filter(_ isIncluded: (Iterator.Element) throws -> Bool) rethrows -> [Iterator.Element]

Keep in mind, that this filter function never holds the exceptions but it throws them to the caller. If there are any chances of any kind of exception at runtime, we need to wrap this inside a try-catch block.


Filter with Array

filter operator can be used over an Array to check the predicate on its elements and return the filtered Array. It can be used with a named parameter or using $0.

let array = [1, 2, 3, 4, 5, 6]
let filteredArray = array.filter { item in
    item % 2 == 0
}
//or
let filteredArray = array.filter {
    $0 % 2 == 0
}
print(filteredArray)

It can also be used with enumerated for positional-based access.

let array = [1, 2, 3, 4, 5, 6]
let filteredArray = array.enumerated().filter {index, item in
    //I am doing some rocket science here
    print("Item at \(index) is \(item)")
    return  index != 2 && item % 2 == 0
}
//or
let filteredArray = array.enumerated().filter {
    //I am doing some rocket science here
    print("Item at \($0) is \($1)")
    return  $0 != 2 && $1 % 2 == 0
}
print(filteredArray)

Filter with Set

Similar to Array, the filter operator can be used over a Set to check the predicate on its elements and return the filtered Set. It can be used with a named parameter or using $0.

let set = [1, 2, 3, 4, 5, 6]
let filteredSet = set.filter { item in
    item % 2 == 0
}
//or
let filteredSet = set.filter {
    $0 % 2 == 0
}
print(filteredSet)

There is no point in using an enumerated filter with the Set as it never guarantees the sequence in a Set.


Filter with Dictionary

Similar to Array and Set, the filter operator can be used over a Dictionary to check the predicate on its elements and return the filtered Dictionary. It can be used with a named parameter or using $0.

let map = [1: "One", 2: "Two", 3: "Three"]
let filteredMap = map.filter { item in
    item.key % 2 == 0
}
//or
let filteredMap = map.filter {
    $0.key % 2 == 0
}
print(filteredMap)

There is no point in using an enumerated filter with the Dictionary as it never guarantees the sequence in a Dictionary.

Comments