Code Snapshots

클로저, 튜플, map, flatMap 등 Fastcampus RxSwift 강좌를 들으며 재밌었던 코드 스니펫 모음

Closure :: Curling

func add(value: Int) -> (Int) -> Int {
    return { value2 in return value + value2 }
}

let addTwo = add(value: 2)
addTwo(10)
add(value: 10)(2)

Tuple :: with Named

let nameTuple: (first: Int, second: Int) = (3,6)
nameTuple.first
nameTuple.second

Tuple :: with Switch

let tuple: (Int,Int) = (3,5)
switch tuple {
case let (a,_):
    print("a: \(a)")
}

Map, FlatMap

let array = [0,10,2,203,43,23,3241,32103,]
array.map { (item: Int) -> String in
    return "\(item+10)"
}

let stringArr = ["good", "https://google.com", "https://agit.io", "some"]
stringArr.flatMap { (string: String) -> String? in
    return URL(string: string)?.host
}