• notice
  • Congratulations on the launch of the Sought Tech site

Summary of higher-order functions of Swift collection types

Use of flatMap/compactMap (swift 5.3)

-Does not return nil

There is no nil in the returned array after flatMap/compactMap processing, and it will unpack the Optional

Take a look at the implementation of map and flatMap/compactMap as follows

let colors = ["red", "yellow", "green", ""]
let colorsOfMap = colors.map {item -> Int? in
    let length = item.count
    guard length> 0 else {
        return nil
    }
    return length
}
print(colorsOfMap)

The results are

[Optional(3), Optional(6), Optional(5), nil]

The implementation of flatMap/compactMap is as follows

-flatMap

let colorsOfFlatMap = colors.flatMap {item ->Int? in
    let length = item.count
    guard length> 0 else {
        return nil
    }
    return length
}
print(colorsOfFlatMap)

-compactMap

let colorsOfFlatMap = colors.compactMap {item ->Int? in
    let length = item.count
    guard length> 0 else {
        return nil
    }
    return length
}
print(colorsOfFlatMap)

The reason why compactMap is used here is because'flatMap' is deprecated Please use compactMap(_:)

The results are

[3, 6, 5]

-Open array

compactMap can open the (two-dimensional, N-dimensional) array together into a new array

let array = [[1,2,3],[4,5,6],[7,8,9]]

// Compared
let arr1 = array.map {$0}
print(arr1)
let arr2 = array.flatMap {$0}
print(arr2)

The results are

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

[1, 2, 3, 4, 5, 6, 7, 8, 9]

-Combine arrays

compactMap can merge different arrays into one array.The number of merged arrays is the product of the two arrays to be merged.

let animals = ["cat", "dog", "pig"]
let counts = [1,2,3]
let newArray = counts.flatMap {count in
    animals.map({ animal in
        return animal + "\(count)"
    })
}
print(newArray)

The results are

["cat1", "dog1", "pig1", "cat2", "dog2", "pig2", "cat3", "dog3", "pig3"]

Use of Map (swift 5.3)

Each element in the collection type is processed once and converted to a new array

Array series

-Case 1-Iterate over each element

let colors = ["red", "yellow", "green", "blue"]
let counts = colors.map {(color: String) -> Int in
    return color.count
}
print(counts)

The result is [3,6,5,4]

-Case 2-A simpler method

let counts1 = colors.map {$0.count}
print(counts1)

The result is also [3,6,5,4]

-Case 3-Converting to an object array (What is the purpose of converting to an object array)

class Color {
    var name: String
    init(name: String) {
        self.name = name
    }
}
let colorsObj = colors.map {return Color(name: $0)}
for obj in colorsObj {
    print(obj.name)
}

The results are

red

yellow

green

blue

Collection series

let ColorsSet: Set = ["red", "yellow", "green", "blue"]
let colorsCount = ColorsSet.map {$0.count}
print(colorsCount)

The result is [3, 6, 4, 5]

Dictionary series

let dict = [2: "red", 4: "yellow", 6: "green", 8: "blue"]
let keys = dict.map {$0.key}
print(keys)
let values = dict.map {$0.value}
print(values)

The results are

[2, 8, 6, 4]

["red", "blue", "green", "yellow"]

Tags

Technical otaku

Sought technology together

Related Topic

0 Comments

Leave a Reply

+