Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Swift Warsaw 2: Laziness in Swift

Swift Warsaw 2: Laziness in Swift

Slides from the talk I gave at the second Swift Warsaw meetup on 2014-08-28 in Warsaw, Poland.

https://github.com/narfdotpl/swift-warsaw-2-laziness-in-swift
http://swiftwarsaw.com/

Maciej Konieczny

August 28, 2014
Tweet

More Decks by Maciej Konieczny

Other Decks in Programming

Transcript

  1. class BlogPost { var filename: String var image = Image()

    init(filename: String) { self.filename = filename } }
  2. class BlogPost { var filename: String lazy var image =

    Image() init(filename: String) { self.filename = filename } }
  3. class BlogPost { var filename: String lazy var image =

    Image() init(filename: String) { self.filename = filename } } var post = BlogPost(filename: "sw2.md") post.image
  4. class BlogPost { var filename: String lazy var image =

    Image() init(filename: String) { self.filename = filename } }
  5. class BlogPost { var filename: String lazy var image =

    \ Image(forFilename: self.filename) init(filename: String) { self.filename = filename } }
  6. class BlogPost { var filename: String lazy var image =

    { Image(forFilename: self.filename) }() init(filename: String) { self.filename = filename } }
  7. - (Image *)image { if (!_image) { _image = [[Image

    alloc] imageForFilename:self.filename]; } return _image; }
  8. for x in xs { // ... } var _g

    = xs.generate() while let x = _g.next() { // ... }
  9. class Integers: SequenceType { func generate() -> GeneratorOf<Int> { var

    n = -1 return GeneratorOf { ++n } } } for i in Integers() { println(i) // 0, 1, 2, 3, ... }
  10. var x = integers \ .filter { $0 % 2

    == 1 } \ .map { $0 * $0 }
  11. var x = integers \ .filter { $0 % 2

    == 1 } \ .map { $0 * $0 } \ .filter { $0 > 100 }
  12. var x = integers \ .filter { $0 % 2

    == 1 } \ .map { $0 * $0 } \ .filter { $0 > 100 } \ .first!
  13. var x = integers \ .filter { $0 % 2

    == 1 } \ .map { $0 * $0 } \ .filter { $0 > 100 } \ .first! println(x) // 121
  14. var x = integers \ .filter { $0 % 2

    == 1 } \ .map { $0 * $0 } \ .filter { $0 > 100 } \ .first! println(x) // 121
  15. var x = integers.filter { return $0 % 2 ==

    1 }.map { return $0 * $0 }.filter { return $0 > 10 }.first! println(x) // 25
  16. var x = integers.filter { println("\n\($0)") println("odd?") return $0 %

    2 == 1 }.map { println("square") return $0 * $0 }.filter { println("threshold") return $0 > 10 }.first! println(x) // 25
  17. integers.filter { $0 % 2 == 1 } \ .map

    { $0 * $0 } \ .filter { $0 > 10 } \ .first!
  18. integers.filter { $0 % 2 == 1 } \ .map

    { $0 * $0 } \ .filter { $0 > 10 } \ .first! 0
  19. integers.filter { $0 % 2 == 1 } \ .map

    { $0 * $0 } \ .filter { $0 > 10 } \ .first! 0 odd?
  20. integers.filter { $0 % 2 == 1 } \ .map

    { $0 * $0 } \ .filter { $0 > 10 } \ .first! 0 odd? 1
  21. integers.filter { $0 % 2 == 1 } \ .map

    { $0 * $0 } \ .filter { $0 > 10 } \ .first! 0 odd? 1 odd?
  22. integers.filter { $0 % 2 == 1 } \ .map

    { $0 * $0 } \ .filter { $0 > 10 } \ .first! 0 odd? 1 odd? square
  23. integers.filter { $0 % 2 == 1 } \ .map

    { $0 * $0 } \ .filter { $0 > 10 } \ .first! 0 odd? 1 odd? square threshold
  24. integers.filter { $0 % 2 == 1 } \ .map

    { $0 * $0 } \ .filter { $0 > 10 } \ .first! 0 odd? 1 odd? square threshold 2 odd? 3 odd? square threshold 4 odd? 5 odd? square threshold
  25. extension LazySequence { var first: LazySequence.Generator.Element? { for x in

    self { return x } return nil } } integers.first! // 0
  26. func f() -> Bool { println("f") return true } func

    g() -> Bool { println("g") return false }
  27. func or(left: Bool, right: Bool) -> Bool { if left

    { return left } else { return right } }
  28. func or(left: Bool, right: Bool) -> Bool { if left

    { return left } else { return right } }
  29. func or(left: Bool, right: Bool) -> Bool { if left

    { return left } else { return right } } println(or(f(), g())) // f, g, true
  30. func or(left: Bool, right: () -> Bool) -> Bool {

    if left { return left } else { return right() } } println(or(f(), { g() })) // f, true
  31. func or(left: Bool, right: @autoclosure () -> Bool) -> Bool

    { if left { return left } else { return right() } } println(or(f(), g())) // f, true
  32. lazy var image = Image() lazy var image = {

    Image(forFilename: self.filename) }()
  33. for x in xs { // ... } var _g

    = xs.generate() while let x = _g.next() { // ... }