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

Getting_all_enum_element

hirose yudai
August 28, 2017
720

 Getting_all_enum_element

potatotips #43で発表させていただいた内容です
Enumを便利に使えるマイクロライブラリを開発した話

https://potatotips.connpass.com/event/63472/

hirose yudai

August 28, 2017
Tweet

Transcript

  1. ࢖͍ํ FOVN:PVS&OVN*OU &OVNFSBCMF\ DBTFPOF DBTFUXP DBTFUISFF ^ QSJOU :PVS&OVNFMFNFOUT <POF

    UXP UISFF> QSJOU :PVS&OVNDPVOU  &OVNFSBCMFͱ͍͏ϓϩτίϧ͕͋ΔͷͰ४ڌ
  2. એݴ public protocol Enumerable: RawRepresentable { static var enumerate: AnySequence<Self>

    { get } static var elements: [Self] { get } static var count: Int { get } static var startIndex: Int { get } }
  3. ࣮૷ public extension Enumerable where RawValue == Int { public

    static var enumerate: AnySequence<Self> { return AnySequence { () -> AnyIterator<Self> in var i = startIndex return AnyIterator { () -> Self? in let element = Self(rawValue: i) i += 1 return element } } } public static var elements: [Self] { return Array(enumerate) } public static var count: Int { return elements.count } public static var startIndex: Int { return 0 } }
  4. ࣮૷ public extension Enumerable where RawValue == Int { public

    static var enumerate: AnySequence<Self> { return AnySequence { () -> AnyIterator<Self> in var i = startIndex return AnyIterator { () -> Self? in let element = Self(rawValue: i) i += 1 return element } } } public static var elements: [Self] { return Array(enumerate) } public static var count: Int { return elements.count } public static var startIndex: Int { return 0 } } FMFNFOUT͸഑ྻ
  5. ࣮૷ public extension Enumerable where RawValue == Int { public

    static var enumerate: AnySequence<Self> { return AnySequence { () -> AnyIterator<Self> in var i = startIndex return AnyIterator { () -> Self? in let element = Self(rawValue: i) i += 1 return element } } } public static var elements: [Self] { return Array(enumerate) } public static var count: Int { return elements.count } public static var startIndex: Int { return 0 } } FOVNFSBUF 4FRVFODF ͔Β࡞ΒΕΔ
  6. ࣮૷ public extension Enumerable where RawValue == Int { public

    static var enumerate: AnySequence<Self> { return AnySequence { () -> AnyIterator<Self> in var i = startIndex return AnyIterator { () -> Self? in let element = Self(rawValue: i) i += 1 return element } } } public static var elements: [Self] { return Array(enumerate) } public static var count: Int { return elements.count } public static var startIndex: Int { return 0 } } FOVNFSBUF
  7. public extension Enumerable where RawValue == Int { public static

    var enumerate: AnySequence<Self> { return AnySequence { () -> AnyIterator<Self> in var i = startIndex return AnyIterator { () -> Self? in let element = Self(rawValue: i) i += 1 return element } } } public static var elements: [Self] { return Array(enumerate) } public static var count: Int { return elements.count } public static var startIndex: Int { return 0 } } ͜ͷϧʔϓ͸OJMΛฦ͢ͱऴΘΓ OJMʹͳΔ·ͰͷཁૉͰ഑ྻΛ࡞Δ
  8. public extension Enumerable where RawValue == Int { public static

    var enumerate: AnySequence<Self> { return AnySequence { () -> AnyIterator<Self> in var i = startIndex return AnyIterator { () -> Self? in let element = Self(rawValue: i) i += 1 return element } } } public static var elements: [Self] { return Array(enumerate) } public static var count: Int { return elements.count } public static var startIndex: Int { return 0 } } 4FMG SBX7BMVF*OU Ͱ&MFNFOUΛ࡞Δ ରԠ͢Δ΋ͷ͕ͳ͔ͬͨΒOJM
  9. public extension Enumerable where RawValue == Int { public static

    var enumerate: AnySequence<Self> { return AnySequence { () -> AnyIterator<Self> in var i = startIndex return AnyIterator { () -> Self? in let element = Self(rawValue: i) i += 1 return element } } } public static var elements: [Self] { return Array(enumerate) } public static var count: Int { return elements.count } public static var startIndex: Int { return 0 } } ࿈൪Ͱ͋Ε͹JODSFNFOU͍͚ͯ͠͹ ࣍ͷ&MFNFOU͕࡞ΒΕΔ
  10. public extension Enumerable where RawValue == Int { public static

    var enumerate: AnySequence<Self> { return AnySequence { () -> AnyIterator<Self> in var i = startIndex return AnyIterator { () -> Self? in let element = Self(rawValue: i) i += 1 return element } } } public static var elements: [Self] { return Array(enumerate) } public static var count: Int { return elements.count } public static var startIndex: Int { return 0 } } ࿈൪Ͱ͋Ε͹JODSFNFOU͍͚ͯ͠͹ ࣍ͷ&MFNFOU͕࡞ΒΕΔ