Skip to content

Latest commit

 

History

History

section15-higher_order_fuctions

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

Higher Order Functions 1 ~ 6

1) What are the higher order functions available in Swift?

“Super Quick”

Answer:

  • Filter
  • Map
  • FlatMap
  • CompactMap
  • Reduce

2) What is Filter and How does it work?

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

3) What is map and how does it work?

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

4) What is reduce and how does it work?

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

5) What is flatmap in Swift?

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

6) What is Compact Map in Swift?

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

Table Of Contents

Section 1, Data Type

Section 2, Operator

Section 3, Conditional Statement

Section 4, Enum

Section 5, functions

Section 6, struct

Section 7, initializers

Section 8, closures

Section 9, OOP

Section 10, static type vs dynamic type

Section 11, optional

Section 12, generic

Section 13, subscript

Section 14, access specifier

Section 15, higher order function

Section 16, delegate

Section 17, extension

Section 18, Memory Management

Section 19, protocols

Section 20, collections

Section 21, KVO and KVC

Section 22, Exception Handling

Section 23, Framework

Section 24, Objective-C