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

Getting_all_enum_element

hirose yudai
August 28, 2017
650

 Getting_all_enum_element

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

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

hirose yudai

August 28, 2017
Tweet

Transcript

  1. QPUBUPUJQT
    (FUUJOHBMM&OVNFMFNFOU

    View full-size slide

  2. ࣗݾ঺հ
    ໊લኍ੉༤େ
    ৬छJ04&OHJOFFS
    Α͘ษڧձͰ͸ࣗ࡞044ʹ͍ͭͯ࿩
    ͯ͠·͢ɻ໊͍ͭͨ͋ͩ͸4UBSޤ৯
    Ͱ͢ɻ4UBS͍ͩ͘͞ɻ
    HJUIVCIUUQHJUIVCDPNCBOO[BJ

    View full-size slide

  3. ࠓ೔͓࿩͢͠Δ಺༰
    *OUܕͷ࿈൪ͷ&OVNͷ͢΂ͯͷཁૉΛऔಘͰ͖ΔϚ
    ΠΫϩϥΠϒϥϦΛ։ൃ͠·ͨͷͰ͝঺հ

    View full-size slide

  4. ࢖͍ํ
    FOVN:PVS&OVN*OU &OVNFSBCMF\
    DBTFPOF
    DBTFUXP
    DBTFUISFF
    ^
    QSJOU :PVS&OVNFMFNFOUT
    <POF UXP UISFF>
    QSJOU :PVS&OVNDPVOU

    *OUͰએݴ

    View full-size slide

  5. ࢖͍ํ
    FOVN:PVS&OVN*OU &OVNFSBCMF\
    DBTFPOF
    DBTFUXP
    DBTFUISFF
    ^
    QSJOU :PVS&OVNFMFNFOUT
    <POF UXP UISFF>
    QSJOU :PVS&OVNDPVOU

    &OVNFSBCMFͱ͍͏ϓϩτίϧ͕͋ΔͷͰ४ڌ

    View full-size slide

  6. ࢖͍ํ
    FOVN:PVS&OVN*OU &OVNFSBCMF\
    DBTFPOF
    DBTFUXP
    DBTFUISFF
    ^
    QSJOU :PVS&OVNFMFNFOUT
    <POF UXP UISFF>
    QSJOU :PVS&OVNDPVOU

    FMFNFOUTͷDPVOU

    View full-size slide

  7. એݴ
    public protocol Enumerable: RawRepresentable {
    static var enumerate: AnySequence { get }
    static var elements: [Self] { get }
    static var count: Int { get }
    static var startIndex: Int { get }
    }

    View full-size slide

  8. ࣮૷
    public extension Enumerable where RawValue == Int {
    public static var enumerate: AnySequence {
    return AnySequence { () -> AnyIterator 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
    }
    }

    View full-size slide

  9. ࣮૷
    public extension Enumerable where RawValue == Int {
    public static var enumerate: AnySequence {
    return AnySequence { () -> AnyIterator 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͸഑ྻ

    View full-size slide

  10. ࣮૷
    public extension Enumerable where RawValue == Int {
    public static var enumerate: AnySequence {
    return AnySequence { () -> AnyIterator 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
    ͔Β࡞ΒΕΔ

    View full-size slide

  11. ࣮૷
    public extension Enumerable where RawValue == Int {
    public static var enumerate: AnySequence {
    return AnySequence { () -> AnyIterator 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

    View full-size slide

  12. public extension Enumerable where RawValue == Int {
    public static var enumerate: AnySequence {
    return AnySequence { () -> AnyIterator 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ʹͳΔ·ͰͷཁૉͰ഑ྻΛ࡞Δ

    View full-size slide

  13. public extension Enumerable where RawValue == Int {
    public static var enumerate: AnySequence {
    return AnySequence { () -> AnyIterator 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

    View full-size slide

  14. public extension Enumerable where RawValue == Int {
    public static var enumerate: AnySequence {
    return AnySequence { () -> AnyIterator 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͕࡞ΒΕΔ

    View full-size slide

  15. public extension Enumerable where RawValue == Int {
    public static var enumerate: AnySequence {
    return AnySequence { () -> AnyIterator 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͕࡞ΒΕΔ

    View full-size slide

  16. ࢖͑ͦ͏ͳ৔໘
    ΋ͱ΋ͱ6*$PMMFDUJPO7JFXɾ6*5BCMF7JFXͰ
    4FDUJPOͷ਺ΛܾΊ͔ͨͬͨɻ%BUB4PVSDFͷؔ਺Ͱ
    ΋ͦͷ&OVNΛݩʹ෼ذ͍ͨ͠ɻͦͷอূ͕ཉ͍͠
    Ұ౓એݴͨ͠&OVNΛมߋͨ͠৔߹ʹཁૉ਺͕উख
    ʹมΘͬͯཉ͍͠

    View full-size slide

  17. ͓·͚
    ࣮͸)BTIBCMFʹ४ڌͯ͠ೖΕ͹
    FMFNFOUTʹཁૉ͕͢΂ͯೖ͍ͬͯΔ
    ։ൃϒϥϯν͕ଘࡏ͍ͯ͠·͢
    ͨͩɺVOTBGFͳ͜ͱΛ͍ͯͯ҆͠શੑ͕อূͰ͖ͳ͍
    ͷͰ͝࢖༻ͷࡍ͸ؾΛ෇͚͍ͯͩ͘͞
    IUUQTHJUIVCDPNCBOO[BJ&OVNFSBCMFUSFF
    FOVNFSBCMF@BEPQU@IBTIBCMF

    View full-size slide

  18. ·ͱΊ
    ϑΝΠϧ͚ͩͷ؆୯ͳϥΠϒϥϦͳͷͰ
    ௚઀ϓϩδΣΫτʹೖΕͨΓɺίʔυίϐϖͰ΋͍͍͔΋͠
    Ε·ͤΜ
    *OU͚͔ͩ͠ରԠͰ͖ͯͳ͍Ͱ͚͢Ͳɺ௒ศརͰ͢
    ͪΐͬͱͰ΋ʮ͓ͬʯͬͯࢥͬͨͦ͜ͷ͋ͳͨ
    4UBS͍ͩ͘͞
    IUUQTHJUIVCDPNCBOO[BJ&OVNFSBCMF

    View full-size slide