Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
SWIFT THE implicit PARTS
MAXIM ZAKS 
@ICEX33
WHAT DO I MEAN BY implicit PARTS?
AGENDA 
▸ Values 
▸ Functions 
▸ Structures
Declaration of a constant 
let name : String = "Maxim";
Declaration of a constant with type inference and without semicolon 
let name = "Maxim"
Declaration of constant immutable array 
let names : Array<String> = ["Maxim", "Anton"] 
let names : [String] = ["Maxim", ...
Declaration of mutable array (as variable) 
var names = ["Maxim", "Anton"] 
names.append("Fox") // ["Maxim", "Anton", "Fox...
Declaration of Optional value 
var name : Optional<String> = "Maxim" 
name!.isEmpty == false // ??? 
var name : String? = ...
Declaration of unwraped value if applicable 
var name : String? = nil 
if let _name = name { 
print("(_name)") 
} else { 
...
Declaration of value which is only optional in the beginning 
var name : ImplicitlyUnwrappedOptional<String> = "Maxim" 
na...
Decomposition of touples 
var a = 0, b = 0 
let touple = (20, 30) 
a = touple.0 
b = touple.1 
// Or just like this: 
(a, ...
Convert from literal 
let url : NSURL = "http://nshipster.com/" 
"http://nshipster.com/".host
extension NSURL: StringLiteralConvertible { 
public class func convertFromExtendedGraphemeClusterLiteral(value: String) ->...
▸ NilLiteralConvertible 
▸ BooleanLiteralConvertible 
▸ IntegerLiteralConvertible 
▸ FloatLiteralConvertible 
▸ StringLite...
FUNCTIONS
Shortes function 
func f() -> Void {} 
func f() -> () {} 
func f() {}
Function without parameter label 
func f(_ v : Int) -> Int { return v + 1 } 
func f(v : Int) -> Int { return v + 1 } 
let ...
Functoin with parameter label equal to parameter name 
func f(value value : Int) -> Int { return value + 1 } 
func f(#valu...
In/Out parameter 
func f(inout v : Int) { v + 1 } 
var v = 10 
f(&v) // v = 11
Copy parameter in to variable 
func f(var v : [String]) -> [String] { 
v.append("Anton") 
return v 
} 
let a = ["Maxim"] 
...
Rest parameter 
func sum(values:Int...) -> Int{ 
return values.reduce(0, +) 
} 
let sum1 = sum(1,2,3,4) // sum1 = 10 
let ...
Default parameter value 
func f(_ v : Int = 10) -> Int { 
return v + 1 
} 
let v1 = f() // v1 = 11 
let v2 = f(3) // v2 = ...
Curried function 
func f(a : Int)(b : Int) -> Int { 
return a + b 
} 
let v1 = f(1)(2)
Returning a closure (idea behind currying) 
func f(a : Int) -> (Int -> Int) { 
let f1 = { 
(b: Int) -> Int in 
return a + ...
Returning a closure (idea behind currying) 
func f(a : Int) -> (Int -> Int) { 
return { 
(b: Int) -> Int in 
return a + b ...
Remove Closure types 
func f(a : Int) -> (Int -> Int) { 
return { 
b in 
return a + b 
} 
} 
let v1 = f(1)(2)
Remove return keyword 
func f(a : Int) -> (Int -> Int) { 
return { 
b in a + b 
} 
} 
let v1 = f(1)(2)
Remove parameter name 
func f(a : Int) -> (Int -> Int) { 
return { 
a + $0 
} 
} 
let v1 = f(1)(2)
Call a function with value 
func f(sum : Int) { 
print("sum is (sum)") 
} 
f(1 + 2) // f(3)
Call function with closure enables lazy evaluation 
func f(sum : () -> Int) { 
print("sum is (sum())") 
} 
f({1 + 2}) // l...
Call function with auto closure enables lazy evaluation implicitly 
func f(sum : @autoclosure () -> Int) { 
print("sum is ...
If last operator is a function you can take it out of parenthesis 
func f(a : Int, b: Int, operation : (Int,Int) -> Int) {...
Or keep it 
func f(a : Int, b: Int, operation : (Int,Int) -> Int) { 
print("sum is ( operation(a, b) )") 
} 
f(1, 2, +)
Operators are functions 
1 + 2 
infix operator + { 
associativity left 
precedence 140 
} 
func +(lhs: Int, rhs: Int) -> I...
Execution is based on precedence 
1 + 2 * 4 
infix operator + { 
associativity left 
precedence 140 
} 
infix operator * {...
STRUCTURES
Touple with named values can be used as structs 
typealias MyPoint = (x:Int, y:Int) 
let point : MyPoint = (x:25, y:30) 
p...
Struct with implicit intializer 
struct MyPoint { 
let x : Int 
let y : Int 
} 
let point = MyPoint(x:25, y:30) 
point.x /...
Methods are curried functions 
struct MyName { 
let name : String 
init(_ n : String) { 
name = n 
} 
func desc()->String{...
TO BE 
CONTINUED...
THANK YOU! @ICEX33
Upcoming SlideShare
Loading in …5
×

Swift the implicit parts

533 views

Published on

Published in: Education
  • Be the first to comment

Swift the implicit parts

  1. 1. SWIFT THE implicit PARTS
  2. 2. MAXIM ZAKS @ICEX33
  3. 3. WHAT DO I MEAN BY implicit PARTS?
  4. 4. AGENDA ▸ Values ▸ Functions ▸ Structures
  5. 5. Declaration of a constant let name : String = "Maxim";
  6. 6. Declaration of a constant with type inference and without semicolon let name = "Maxim"
  7. 7. Declaration of constant immutable array let names : Array<String> = ["Maxim", "Anton"] let names : [String] = ["Maxim", "Anton"] let names = ["Maxim", "Anton"] names.append("Fox") // Compile error
  8. 8. Declaration of mutable array (as variable) var names = ["Maxim", "Anton"] names.append("Fox") // ["Maxim", "Anton", "Fox"]
  9. 9. Declaration of Optional value var name : Optional<String> = "Maxim" name!.isEmpty == false // ??? var name : String? = nil name?.isEmpty == false // ???
  10. 10. Declaration of unwraped value if applicable var name : String? = nil if let _name = name { print("(_name)") } else { print("No name!") }
  11. 11. Declaration of value which is only optional in the beginning var name : ImplicitlyUnwrappedOptional<String> = "Maxim" name.isEmpty var name : String! = nil name.isEmpty // Runtime Exception
  12. 12. Decomposition of touples var a = 0, b = 0 let touple = (20, 30) a = touple.0 b = touple.1 // Or just like this: (a, b) = touple
  13. 13. Convert from literal let url : NSURL = "http://nshipster.com/" "http://nshipster.com/".host
  14. 14. extension NSURL: StringLiteralConvertible { public class func convertFromExtendedGraphemeClusterLiteral(value: String) -> Self { return self(string: value) } public class func convertFromStringLiteral(value: String) -> Self { return self(string: value) } }
  15. 15. ▸ NilLiteralConvertible ▸ BooleanLiteralConvertible ▸ IntegerLiteralConvertible ▸ FloatLiteralConvertible ▸ StringLiteralConvertible / UnicodeScalarLiteralConvertible / ExtendedGraphemeClusterLiteralConvertible ▸ ArrayLiteralConvertible ▸ DictionaryLiteralConvertible
  16. 16. FUNCTIONS
  17. 17. Shortes function func f() -> Void {} func f() -> () {} func f() {}
  18. 18. Function without parameter label func f(_ v : Int) -> Int { return v + 1 } func f(v : Int) -> Int { return v + 1 } let v1 = f(10) // v1 = 11
  19. 19. Functoin with parameter label equal to parameter name func f(value value : Int) -> Int { return value + 1 } func f(#value : Int) -> Int { return value + 1 } let v1 = f(value : 10) // v1 = 11
  20. 20. In/Out parameter func f(inout v : Int) { v + 1 } var v = 10 f(&v) // v = 11
  21. 21. Copy parameter in to variable func f(var v : [String]) -> [String] { v.append("Anton") return v } let a = ["Maxim"] let a2 = f(a) // a = ["Maxim"] , a2 = ["Maxim", "Anton"]
  22. 22. Rest parameter func sum(values:Int...) -> Int{ return values.reduce(0, +) } let sum1 = sum(1,2,3,4) // sum1 = 10 let sum2 = sum() // sum2 = 0
  23. 23. Default parameter value func f(_ v : Int = 10) -> Int { return v + 1 } let v1 = f() // v1 = 11 let v2 = f(3) // v2 = 4
  24. 24. Curried function func f(a : Int)(b : Int) -> Int { return a + b } let v1 = f(1)(2)
  25. 25. Returning a closure (idea behind currying) func f(a : Int) -> (Int -> Int) { let f1 = { (b: Int) -> Int in return a + b } return f1 } let v1 = f(1)(2)
  26. 26. Returning a closure (idea behind currying) func f(a : Int) -> (Int -> Int) { return { (b: Int) -> Int in return a + b } } let v1 = f(1)(2)
  27. 27. Remove Closure types func f(a : Int) -> (Int -> Int) { return { b in return a + b } } let v1 = f(1)(2)
  28. 28. Remove return keyword func f(a : Int) -> (Int -> Int) { return { b in a + b } } let v1 = f(1)(2)
  29. 29. Remove parameter name func f(a : Int) -> (Int -> Int) { return { a + $0 } } let v1 = f(1)(2)
  30. 30. Call a function with value func f(sum : Int) { print("sum is (sum)") } f(1 + 2) // f(3)
  31. 31. Call function with closure enables lazy evaluation func f(sum : () -> Int) { print("sum is (sum())") } f({1 + 2}) // lazy evaluation
  32. 32. Call function with auto closure enables lazy evaluation implicitly func f(sum : @autoclosure () -> Int) { print("sum is (sum())") } f(1 + 2) // still lazy evaluation
  33. 33. If last operator is a function you can take it out of parenthesis func f(a : Int, b: Int, operation : (Int,Int) -> Int) { print("sum is ( operation(a, b) )") } f(1,2) { $0 + $1 }
  34. 34. Or keep it func f(a : Int, b: Int, operation : (Int,Int) -> Int) { print("sum is ( operation(a, b) )") } f(1, 2, +)
  35. 35. Operators are functions 1 + 2 infix operator + { associativity left precedence 140 } func +(lhs: Int, rhs: Int) -> Int
  36. 36. Execution is based on precedence 1 + 2 * 4 infix operator + { associativity left precedence 140 } infix operator * { associativity left precedence 150 }
  37. 37. STRUCTURES
  38. 38. Touple with named values can be used as structs typealias MyPoint = (x:Int, y:Int) let point : MyPoint = (x:25, y:30) point.x // 25 point.y // 30
  39. 39. Struct with implicit intializer struct MyPoint { let x : Int let y : Int } let point = MyPoint(x:25, y:30) point.x // 25 point.y // 30
  40. 40. Methods are curried functions struct MyName { let name : String init(_ n : String) { name = n } func desc()->String{ return "My name is: (name)" } } let myName = MyName("Maxim") myName.desc() MyName.desc(myName)()
  41. 41. TO BE CONTINUED...
  42. 42. THANK YOU! @ICEX33

×