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

swiftにおける遅延評価

kichikuchi
August 13, 2018

 swiftにおける遅延評価

kichikuchi

August 13, 2018
Tweet

More Decks by kichikuchi

Other Decks in Technology

Transcript

  1. class LazyCat { var name = "" lazy var selfIntro:

    String = { return "໊લ͸\(name)Ͱ͢" }() } let cat = LazyCat() cat.name = "γϩίϚ" print(cat.selfIntro) // selfIntro͕ॳظԽ͞ΕΔ // ໊લ͸γϩίϚͰ͢ cat.name = "ΫϩίϚ" print(cat.selfIntro) // ΫϩίϚ͸൓ө͞Εͳ͍ʂ // ໊લ͸γϩίϚͰ͢ ॳΊͯࢀরͨ͠ ඞཁʹͳͬͨ ࣌ʹॳظԽ͞ΕΔʂ
  2. class LazyCat { var name = "γϩίϚ" lazy var selfIntro:

    String = { return "໊લ͸\(self.name)Ͱ͢" }() var selfIntroduction: String = { return "໊લ͸\(self.name)Ͱ͢" }() // error: Use of unresolved identifier 'self' } DMPTVSF಺ͰΠϯελϯεม਺ʹΞΫηεͰ͖Δʂ ॳظԽ࣌ʹTFMG͕࢖͑Δ
  3. class LazyCat { lazy var lazyName = name() var normalName

    = name() // Cannot use instance member 'name' within property initializer // property initializers run before 'self' is available func name() -> String { return "γϩίϚ" } } ΠϯελϯεϝιουͰॳظԽͰ͖Δ ॳظԽ࣌ʹTFMG͕࢖͑Δ
  4. class LazyCat { lazy var ja = "ʹΌʙ" lazy var

    en = "mew" } let cat = LazyCat() // ͲͪΒ΋allocate͞Ε͍ͯͳ͍ print(cat.ja) // english͸allocate͞Εͳ͍ ඞཁͳ࣌ʹBMMPDBUJPO͕ൃੜ͢ΔͨΊ ϝϞϦΛઅ໿Ͱ͖Δ ෆཁͳBMMPDBUJPO͕ൃੜ͠ͳ͍
  5. ௨ৗͷDPMMFDUJPOͷڍಈ let array = Array(1...10) let normalArray = array.map {

    value -> Int in print(value) return value * 2 } .filter { value in print(value) return value < 10 }
  6. ௨ৗͷDPMMFDUJPOͷڍಈ let array = Array(1...10) let normalArray = array.map {

    value -> Int in print(value) return value * 2 } .filter { value in print(value) return value < 10 } 1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20
  7. ௨ৗͷDPMMFDUJPOͷڍಈ let array = Array(1...10) let normalArray = array.map {

    value -> Int in print(value) return value * 2 } .filter { value in print(value) return value < 10 } print(normalArray.first) 1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 Optional(2)
  8. ௨ৗͷDPMMFDUJPOͷڍಈ let array = Array(1...10) let normalArray = array.map {

    value -> Int in print(value) return value * 2 } .filter { value in print(value) return value < 10 } print(normalArray.first) 1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 Optional(2) <         > ˣ <         > ˣ <   > ˣ  NBQ\ ^ pMUFS\^ pSTU
  9. ௨ৗͷDPMMFDUJPOͷڍಈ let array = Array(1...10) let normalArray = array.map {

    value -> Int in print(value) return value * 2 } .filter { value in print(value) return value < 10 } print(normalArray.first) 1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 Optional(2) <         > ˣ <         > ˣ <   > ˣ  NBQ\ ^ pMUFS\^ pSTU ແବ͕ଟ͍
  10. MB[ZDPMMFDUJPOͷڍಈ let array = Array(1...10) let lazyArray = array.lazy .map

    { value -> Int in print(value) return value * 2 } .filter { value in print(value) return value < 10 }
  11. MB[ZDPMMFDUJPOͷڍಈ let array = Array(1...10) let lazyArray = array.lazy .map

    { value -> Int in print(value) return value * 2 } .filter { value in print(value) return value < 10 } ϩά͕දࣔ͞Εͳ͍
  12. MB[ZDPMMFDUJPOͷڍಈ let array = Array(1...10) let lazyArray = array.lazy .map

    { value -> Int in print(value) return value * 2 } .filter { value in print(value) return value < 10 } print(lazyArray.first) 1 2 Optional(2) pSTUʹΞΫηεͨ࣌͠ʢඞཁʹͳͬͨ࣌ʣʹ ඞཁͳ෼͚ͩNBQ pMUFS͕ධՁ͞ΕΔʂ
  13. MB[ZDPMMFDUJPOͷڍಈ let array = Array(1...10) let lazyArray = array.lazy .map

    { value -> Int in print(value) return value * 2 } .filter { value in print(value) return value < 10 } print(lazyArray.first) 1 2 Optional(2) pSTUʹΞΫηεͨ࣌͠ʢඞཁʹͳͬͨ࣌ʣ ඞཁͳ෼͚ͩNBQ pMUFS͕ධՁ͞ΕΔʂ <         > ˣ <> ˣ <> ˣ  NBQ\ ^ pMUFS\^ pSTU
  14. MB[ZDPMMFDUJPOͷڍಈ let array = Array(1...10) let lazyArray = array.lazy .map

    { value -> Int in print(value) return value * 2 } .filter { value in print(value) return value < 10 } print(lazyArray.first) 1 2 Optional(2) pSTUʹΞΫηεͨ࣌͠ʢඞཁʹͳͬͨ࣌ʣ ඞཁͳ෼͚ͩNBQ pMUFS͕ධՁ͞ΕΔʂ <         > ˣ <> ˣ <> ˣ  NBQ\ ^ pMUFS\^ pSTU ແବ͕ͳ͍ʂ