// CLOSURE
//1. SIMPLE CLOSURE
let disp = {print(" Simple closure displaying")}
disp()
// 2. RETURN FROM CLOSURE
let returnName = {
(name : String) -> String in
return name
}
print(returnName("Ram"))
// 3. CLOSURE : MULTIPLE ARGUMENT SINGLE RETURN
let getCalc = {
(x : Int, y : Int) -> Int in
return x+y
}
print(getCalc(15, 35))
// 4. CLOSURE : MULTIPLE ARGUMENT MULTIPLE RETURN
let getValue = {
(i : Int, j : Int) -> (Int,Int) in
return (i,j)
}
print(getValue(55,45))
// 5. CLOSURE : COMPARISON
func ascend(s1: String, s2: String) -> Bool {
return s1 > s2
}
let stringcmp = ascend(s1 : "swift", s2 : "great")
print (stringcmp)
// 6. CLOSURE : SHORTHAND ARGUMENT
var shorthand: (String, String) -> String
shorthand = { $1 }
print(shorthand("100", "200"))
// 7. CLOSURE : SORTING ARRAY
let numb = [98, -20, -30, 42, 18, 35]
var sortedNumbers = numb.sorted{ (left: Int, right: Int) -> Bool in
return left < right
}
let asc = numb.sorted()
print(asc)
// 8.
func calcDecrement(forDecrement total: Int) -> () -> Int {
var overallDecrement = 100
func decrementer() -> Int {
overallDecrement -= total
print(overallDecrement)
return overallDecrement
}
return decrementer
}
let decrem = calcDecrement(forDecrement: 18)
decrem()
decrem()
decrem()
No comments:
Post a Comment