$30 off During Our Annual Pro Sale. View Details »

Swift Functional Programming

Swift Functional Programming

(car (cdr ファンクション倶楽部)) https://esminc.connpass.com/event/57534/ で話そうと思ってたやつ

Hirofumi Wakasugi

September 24, 2017
Tweet

More Decks by Hirofumi Wakasugi

Other Decks in Programming

Transcript

  1. Swift
    Functional
    Programming
    Hirofumi Wakasugi (@5T111111)

    View Slide

  2. 4XJGUؔ਺ܕϓϩάϥϛϯά
    let list: List = .cons("pork", .cons("beef", .cons("chicken", .null)))
    // => (pork . (beef . (chicken . ())))
    car(list) // => pork
    cdr(list) // => (beef . (chicken . ()))
    car(cdr(list)) // => beef
    cdr(cdr(list)) // => (chicken . ())

    View Slide

  3. !)JSPGVNJ8BLBTVHJ !U

    4XJGUษڧத
    ؔ਺ܕϓϩάϥϛϯάษڧத
    ࣗݾ঺հ

    View Slide

  4. 8IZ4XJGU ͳͥ4XJGUͷ࿩Λ͢Δͷ͔ͷઆ໌

    4XJGUͰؔ਺ܕϓϩάϥϛϯάͷ࣮ફ
    *
    ίʔυͷ࣮ྫɺέʔεελσΟ
    **
    ؔ਺ܕϓϩάϥϛϯάͷͨΊͷϥΠϒϥϦͷ঺հ
    ***
    4%, $PDPB
    ʹΑΔ੍ݶͱͦͷXPSLBSPVOE
    ؔ਺ܕϓϩάϥϛϯάελΠϧͰͷ8FCϓϩάϥϛϯά
    *
    7BQPSͱ͍͏8"'Λ࢖ͬͯؔ਺ܕϓϩάϥϛϯάΛద༻ͯ͠ΈΔ
    ΞδΣϯμ

    View Slide

  5. 8IZ4XJGU ͳͥ4XJGUͷ࿩Λ͢Δͷ͔ͷઆ໌

    4XJGUͰؔ਺ܕϓϩάϥϛϯάͷ࣮ફ
    *
    ίʔυͷ࣮ྫɺέʔεελσΟ
    **
    ؔ਺ܕϓϩάϥϛϯάͷͨΊͷϥΠϒϥϦͷ঺հ
    ***
    4%, $PDPB
    ʹΑΔ੍ݶͱͦͷXPSLBSPVOE
    ؔ਺ϓϩάϥϛϯάελΠϧͰͷ8FCϓϩάϥϛϯά
    *
    7BQPSͱ͍͏8"'Λ࢖ͬͯؔ਺ܕϓϩάϥϛϯάΛద༻ͯ͠ΈΔ
    ΞδΣϯμ
    ࠓ೔࿩͢ͷ͸͜Ε͚ͩʂ

    View Slide

  6. 8):48*'5

    View Slide

  7. w੩తͳܕνΣοΫͱܕ҆શੑΛඋ͑ͨܕγεςϜ͕͋Δ
    wωΠςΟϒίʔυʹίϯύΠϧ͞ΕΔ
    wαʔόʔαΠυͷ8FCϓϩάϥϛϯάͰ࢖͑ΔՄೳੑ͕͋Δ
    ݸਓతͳझຯ

    View Slide

  8. w੩తͳܕνΣοΫͱܕ҆શੑΛඋ͑ͨܕγεςϜ͕͋Δ
    wωΠςΟϒίʔυʹίϯύΠϧ͞ΕΔ
    wαʔόʔαΠυͷ8FCϓϩάϥϛϯάͰ࢖͑ΔՄೳੑ͕͋Δ
    ݸਓతͳझຯ
    (PͰ͠ΐ

    View Slide

  9. w੩తͳܕνΣοΫͱܕ҆શੑΛඋ͑ͨܕγεςϜ͕͋Δ
    wωΠςΟϒίʔυʹίϯύΠϧ͞ΕΔ
    wαʔόʔαΠυͷ8FCϓϩάϥϛϯάͰ࢖͑ΔՄೳੑ͕͋Δ
    ݸਓతͳझຯ
    Swift

    View Slide

  10. 8):48*'5

    View Slide

  11. ؔ਺ܕϓϩάϥϛϯάͱ4XJGU

    View Slide

  12. ͦ΋ͦ΋

    View Slide

  13. 4XJGUͰ
    ؔ਺ܕϓϩάϥϛϯά
    ͱ͍͏ωλ͕

    View Slide

  14. શ͘໨৽͍͠΋ͷͰ͸ͳ͍

    View Slide

  15. ͘Β͍͸࿩͞Ε͖ͯͨ
    ͨͿΜ

    View Slide

  16. ͳΜͰ·ͨ͋͑ͯ
    ͦΜͳ࿩Λ͍ͨ͠ͷ͔

    View Slide

  17. "QQMF88%$
    4XJGUൃද

    View Slide

  18. ؔ਺͕ϑΝʔετΫϥεʁ

    View Slide

  19. pMUFSSFEVDFNBQqBU.BQ

    View Slide

  20. 0QUJPOBM

    View Slide

  21. ͥΜͿ
    0CKFDUJWF$ʹ͸
    ͳ͔ͬͨͧʁ

    View Slide

  22. ؔ਺ܕͷಛ௃Λඋ͑ͨݴޠ͕
    "QQMFެࣜͰ
    ʮJ04NBD04։ൃʯ
    ͱ͍͏ڊେϚʔέοτʹݱΕͨ

    View Slide

  23. ྑ͘΋ѱ͘΋
    ؔ਺ܕʹରͯ͠
    ஫໨͕ू·Δ
    ͭͷ͖͔͚ͬʹͳͬͨ

    View Slide

  24. ͡Ό͋

    View Slide

  25. 4XJGU͸ؔ਺ܕϓϩ
    άϥϛϯάݴޠͳ
    ͷ͔ʁ

    View Slide

  26. /0

    View Slide

  27. 4XJGUͰؔ਺ܕϓϩ
    άϥϛϯά͸Մೳ
    ͔ʁ

    View Slide

  28. *%0/`55)*/,40
    ؔ਺ܕϓϩάϥϛϯά͸ίʔσΟϯάελΠϧʹա͗ͳ
    ͍ͱ͍͏ݟํ΋͋Δ͕ɺ
    ؔ਺ܕͬΆ͍ػೳ΍ख๏ΛҰ෦औΓೖΕͯ΋
    ͦΕ͚ͩͰ͸ؔ਺ܕϓϩάϥϛϯάʹ͸ͳΒͳ͍ͱࢥ͏
    FH+BWB4DSJQU+BWB3VCZ$ -JOR
    FUD

    View Slide

  29. ͦ͏ࢥͬͯͨ࣌ظ͕Ӡʑ

    View Slide

  30. ͋Εʁ
    Α͘ݟͯΈΔͱʜ
    4XJGU͸ࢥͬͨҎ্ʹ
    ؔ਺ܕϓϩάϥϛϯά͕
    ΍Γ΍ͦ͢͏ʹݟ͑·ͧ͢ʁʁ

    View Slide

  31. 4XJGUͰ͸ʜ
    w ؔ਺Λ஋ͱͯ͠ѻ͑Δ
    w ؔ਺͸ϑΝʔετΫϥε
    w typealiasͰؔ਺ͷܕఆٛ΋Մೳ MJLFtypeJO)BTLFMM

    View Slide

  32. 4XJGUͰ͸ʜ
    w .PEVMBCMFͳσβΠϯ΍એݴతͳهड़Λ΍Γ΍͘͢͢Δ࢓૊Έ͕͋Δ
    w 0QUJPOBM "SSBZͳͲ͕'VODUPS.POBEͱ࣮ͯ͠૷͞Ε͍ͯΔ

    ϝιουνΣʔϯͰ͖Δ
    w 1SPUPDPM1SPUPDPM&YUFOTJPOͰܕΛ֦ு MJLFUZQFDMBTTUZQF
    DMBTTJOTUBODJBUJPO

    w ԋࢉࢠͷఆ͕ٛՄೳ <*> <^>)

    View Slide

  33. w ෭࡞༻Λൃੜͤ͞ʹ͍͘ ࢀরಁաʹ͠΍͍͢

    w σʔλܕ͕جຊJNNVUBCMFͳ஋ܕ
    w letʹΑΔଋറΛଅਐ͢Δݴޠ࢓༷
    w *NNVUBCMFͳ஋͸ίϯύΠϥʹΑͬͯ࠷దԽ͞ΕΔ
    4XJGUͰ͸ʜ

    View Slide

  34. w౰ͨΓલͷΑ͏ʹΫϩʔδϟ͕͋Δ
    w౰ͨΓલͷΑ͏ʹύλʔϯϚον͕͋Δ
    w౰ͨΓલͷΑ͏ʹHVBSEจ͕͋Δ
    w౰ͨΓલͷΑ͏ʹ຤ඌ࠶ؼ͕࠷దԽ͞ΕΔ
    w౰ͨΓલͷΑ͏ʹΧϦʔԽʹઐ༻ͷߏจ͕͋Δ
    4XJGUͰফ͑·ͨ͠ʂ
    4XJGUͰ͸ʜ

    View Slide

  35. ެࣜͳݟղ
    w"QQMFࣗ਎͸4XJGU͕ؔ਺ܕͩͱ͸શ͘ݴͬͯͳ͍

    View Slide

  36. ެࣜͳݟղ
    w"QQMFࣗ਎͸4XJGU͕ؔ਺ܕͩͱ͸શ͘ݴͬͯͳ͍
    ͕ɺ101701ͳϓϩάϥϛϯάΛڧ͘ਪ঑
    (WWDC 2015 - Building Better Apps with Value Types in Swift)
    (WWDC 2016 - Protocol and Value Oriented Programming in UIKit Apps)
    001͔ΒͷύϥμΠϜγϑτ͸Ψν

    View Slide

  37. ެࣜͳݟղ
    “Objective-C without the C” implies something subtractive, but
    Swift dramatically expands the design space through the
    introduction of generics and functional programming concepts.
    - Chris Lattner

    View Slide

  38. 'VDOUJPOBM4XJGU$PNNVOJUZ
    w'VODUJPOBM4XJGU$POGFSFODF

    IUUQGVOTXJGUDPOGDPN

    wUZQFMJGU4XJGU[
    wUIPVHIUCPU3VOFT

    View Slide

  39. ઐ໳Ͱॻ੶΋ग़͍ͯΔ

    View Slide

  40. ͜Ε͸΋͏
    ؔ਺ܕϓϩάϥϛϯά͕Ͱ͖Δͱ
    ݴ͍͍ͬͯΜ͡Όͳ͍Ͱ͔͢Ͷʜ

    View Slide

  41. ͋Εʁ

    View Slide

  42. ͳΜͰ·ͨ͋͑ͯࠓߋ
    ʮ4XJGUͰؔ਺ܕΆΑΆΑʙʯ
    Έ͍ͨͳ࿩Λ͍ͨ͠ͷ͔
    શવ͜ͷཧ༝ʹͳͬͯͳ͘ͳ͍ʁ

    View Slide

  43. 8):48*'5

    View Slide

  44. 4XJGU1SPHSBNNJOH-BOHVBHF&WPMVUJPO
    IUUQTHJUIVCDPNBQQMFTXJGUFWPMVUJPO
    4XJGUJTFWPMWJOH UPP
    GBTU

    View Slide

  45. &OVNͰ࠶ؼతͳσʔλߏ଄Λ࡞Εͳ͍

    View Slide

  46. 4XJGUͰ࣮૷ࡁ
    enum List {
    case null
    indirect case cons(T, List)
    }
    ˢ๯಄ͷϦετͷ࣮૷Ͱ΋࠶ؼ&OVNΛ࢖͍ͬͯΔ

    View Slide

  47. UZQFBMJBTʹHFOFSJDT͕࢖͑ͳ͍

    View Slide

  48. UZQFBMJBTʹHFOFSJDT͕࢖͑ͳ͍
    4XJGUͰ࢖͑ΔΑ͏ʹͳͬͨ

    View Slide

  49. ଞʹ΋ʜ
    wFYQFSJNFOUBMͰ͸͋Δ͕ؔ਺߹੒ԋࢉࢠ͕ຊମʹ࣮૷

    IUUQTHJUIVCDPNBQQMFTXJGUCMPCECDDCEGEEFTUEMJC
    JOUFSOBM4XJGU&YQFSJNFOUBM4XJGU&YQFSJNFOUBMTXJGU-
    w(FOFSJDT.BOJGFTUPͷதͰߴ֊ܕͷ࣮૷ܭը͋Γ

    IUUQTHJUIVCDPNBQQMFTXJGUCMPCECDDCEGEEFEPDT
    (FOFSJDT.BOJGFTUPNEIJHIFSLJOEFEUZQFT

    protocol Functor {
    associatedtype A
    func fmap(f: A -> FB.A) -> FB
    }

    View Slide

  50. ׂͱΨϯΨϯมΘͬͯΔ
    wωοτͷ࣍৘ใ͸ݹ͘ͳ͍ͬͯΔ΋ͷ΋ଟ͍
    wؔ਺ܕϓϩάϥϛϯάͷจ຺Ͱͷมߋ΋ؚ·ΕΔ
    4XJGUJTFWPMWJOH UPP
    GBTU
    ʮ4XJGUͰؔ਺ܕϓϩάϥϛϯάແཧʯ
    ͬͯࢥͬͨਓ΋࠷৽৘ใʹ৮ΕΔͱ໘ന͍͔΋ʁ

    View Slide