swiftにおける遅延評価

4625691e57c20fb2b0116608f1bd8649?s=47 kichikuchi
August 13, 2018

 swiftにおける遅延評価

4625691e57c20fb2b0116608f1bd8649?s=128

kichikuchi

August 13, 2018
Tweet

Transcript

  1. 5.
  2. 6.

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

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

    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͕࢖͑Δ
  4. 13.

    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͕࢖͑Δ
  5. 14.

    class LazyCat { lazy var ja = "ʹΌʙ" lazy var

    en = "mew" } let cat = LazyCat() // ͲͪΒ΋allocate͞Ε͍ͯͳ͍ print(cat.ja) // english͸allocate͞Εͳ͍ ඞཁͳ࣌ʹBMMPDBUJPO͕ൃੜ͢ΔͨΊ ϝϞϦΛઅ໿Ͱ͖Δ ෆཁͳBMMPDBUJPO͕ൃੜ͠ͳ͍
  6. 19.

    ௨ৗͷ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 }
  7. 20.

    ௨ৗͷ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
  8. 21.

    ௨ৗͷ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)
  9. 22.

    ௨ৗͷ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. 23.

    ௨ৗͷ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 ແବ͕ଟ͍
  11. 24.

    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. 25.

    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 } ϩά͕දࣔ͞Εͳ͍
  13. 26.

    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͕ධՁ͞ΕΔʂ
  14. 27.

    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
  15. 28.

    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 ແବ͕ͳ͍ʂ