본문 바로가기

프로그래밍

swift 함수 연습 3


ver1


func helloGenerator(message : String) -> (String, Int) -> String

{

return 

{

(name : String, age : Int) -> String in

return "이름 : \(name) 나이 : \(age) + \(message)"

}

}


ver2


func helloGenerator(message : String) -> (String, Int) -> String

{

return 

{

name, age -> String in

return "이름 : \(name) 나이 : \(age) + \(message)"

}

}


ver3


func helloGenerator(message : String) -> (String, Int) -> String

{

return 

{

return "이름 : \($0) 나이 : \($1) + \(message)"

}

}


ver4


func helloGenerator(message : String) -> (String, Int) -> String

{

return {"이름 : \($0) 나이 : \($1) + \(message)"}

}



closure


let numbers = [ 6, 23, 4, -7, 1, 9, 72]


let sortedNumbers = numbers.sorted { $0 < $1 }

print(sortedNumbers)


let evens = numbers.filter { $0 % 2 == 0 }

print(evens)


let hundreds = numbers.map { $0 * 100 }

print(hundreds)


// numbers 배열 전체를 더한다.

print(numbers.reduce(0) { $0 + $1 })


print(numbers.reduce(0, +))



https://devxoul.gitbooks.io/ios-with-swift-in-40-hours/content/Chapter-3/functions-and-closures.html