swiftにおける遅延評価

4625691e57c20fb2b0116608f1bd8649?s=47 kichikuchi
August 13, 2018

 swiftにおける遅延評価

4625691e57c20fb2b0116608f1bd8649?s=128

kichikuchi

August 13, 2018
Tweet

Transcript

  1. TXJGUʹ͓͚Δ ஗ԆධՁ $POOFIJUP.BSDIÉˌʙJ04ࢢʙ 

  2. ॴଐɿίωώτגࣜձࣾ ۀ຿ɿJ04ΞϓϦ։ൃ

  3. ͦ΋ͦ΋ ஗ԆධՁͱ͸

  4. ݁Ռ͕ඞཁʹͳΔ·Ͱؔ਺Λ࣮ߦ͠ͳ͍͜ͱ CZ͍͢͝)ຊ

  5. None
  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) // ΫϩίϚ͸൓ө͞Εͳ͍ʂ // ໊લ͸γϩίϚͰ͢ ॳΊͯࢀরͨ͠ ඞཁʹͳͬͨ ࣌ʹॳظԽ͞ΕΔʂ
  7. TXJGUʹ͸ओʹ छྨͷ࢖͍ํ͕͋Δ MB[ZQSPQFSUZ MB[ZDPMMFDUJPO

  8. MB[ZQSPQFSUZ

  9. MB[ZQSPQFSUZ ɾMB[Zम০ࢠΛ͚ͭΔ͜ͱͰએݴͰ͖Δ ɾॳճΞΫηε࣌ʹ஋͕ॳظԽ͞ΕΔ ɾMB[Zम০ࢠ͸WBSʹ͔͚ͭ͠ΒΕͳ͍ class LazyCat { lazy var name

    = "γϩίϚ" }
  10. ͳͥWBS͚ͩʁ ఆ਺ MFU ͸ΠϯελϯεͷॳظԽ׬ྃ࣌ʹ஋Λ อ͍࣋ͯ͠ͳ͚Ε͹ͳΒͳ͍ MB[ZΛ͚ͭΔͱॳظԽ࣌Ͱ͸ͳ͘ΞΫηε࣌ʹ ஋͕อ࣋͞ΕΔΑ͏ʹͳΔ

  11. Կ͕خ͍͠ͷ͔ ɾॳظԽ࣌ʹTFMG͕࢖͑Δ ɾෆཁͳBMMPDBUJPO͕ൃੜ͠ͳ͍

  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͕࢖͑Δ
  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͕࢖͑Δ
  14. class LazyCat { lazy var ja = "ʹΌʙ" lazy var

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

  16. NBQ pMUFSͳͲ͕ ஗ԆධՁ͞ΕΔDPMMFDUJPO

  17. let array = Array(1...10) let lazyArray = array.lazy MB[ZͰऔಘͰ͖Δʂ

  18. NBQ pMUFSͷධՁ

  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 }
  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
  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)
  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
  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 ແବ͕ଟ͍
  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 }
  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 } ϩά͕දࣔ͞Εͳ͍
  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͕ධՁ͞ΕΔʂ
  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
  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 ແବ͕ͳ͍ʂ
  29. ·ͱΊ TXJGUͷ஗ԆධՁ ɾMB[ZQSPQFSUZ ɾMB[ZDPMMFDUJPO MB[ZQSPQFSUZ ɾॳظԽ࣌ʹTFMGΛ࢖͑Δ ɾϝϞϦΛઅ໿Ͱ͖Δ MB[ZDPMMFDUJPO ɾNBQ pMUFSར༻࣌ʹඞཁͳ෼͚ͩධՁ͞ΕΔ