“Super Quick”
Answer:
- Filter
- Map
- FlatMap
- CompactMap
- Reduce
Answer:
- Filter works on collections to filter out the elements of Collection based on some criteria.
let array: [Int] = [1, 2, 3, 4, 5, 6, 7, 8, 9]
// var evenArr: [Int] = array.filter { value -> Bool in
// value % 2 == 0
// }
var evenArr: [Int] = array.filter { $0 % 2 == 0 }
print(eventArr) // 2, 4, 6, 8
Answer:
- Map works on the collection to apply an operation on all elements of a collection.
let array: [Int] = [1, 2, 3, 4, 5, 6, 7, 8, 9]
//var mappedArray: [Int] = array.map { value -> Int in
// value * 2
//}
var mappedArray: [Int] = arrap.map { value * 2 }
print(mappedArray) // 2, 4, 6, 8, 10, 12, 14, 16, 18
Answer:
- Reduce works on the Collection to prepare a single value from the collection of values.
let array: [Int] = [1, 2, 3, 4, 5, 6, 7, 8, 9]
var result: Int = 0
result = array.reduce(result) { partialResult, value in
partialResult + value
}
print(result) // 45
Answer:
- Flat map extracts the Collection of collections into a collection.
- [ [1, 2, 3], [4, 5, 6] ].flatmap ⇒ [1, 2, 3, 4, 5, 6]
let array: [[Int]] = [[1, 2, 3], [4, 5], [6, 7], [8]]
let flatMappedArr: [Int] = array.flatMap { $0 }
print(flatMappedArr) // 1, 2, 3, 4, 5, 6, 7, 8
Answer:
- Compact Map removes all nils from the collection.
let array: [Int?] = [0, 1, 2, 3, 4, nil, nil, 7, 8, nil]
let compactMappedArr: [Int] = array.compactMap { $0 }
print(compactMappedArr) // 0, 1, 2, 3, 4, 7, 8
Section 3, Conditional Statement
Section 10, static type vs dynamic type
Section 15, higher order function