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

Swift Functional Programming

Swift Functional Programming

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

E3d8c68398580685ee7ef4ec2cc1cfc2?s=128

Hirofumi Wakasugi

September 24, 2017
Tweet

Transcript

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

  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 . ())
  3. !)JSPGVNJ8BLBTVHJ !U  4XJGUษڧத ؔ਺ܕϓϩάϥϛϯάษڧத ࣗݾ঺հ

  4. 8IZ4XJGU  ͳͥ4XJGUͷ࿩Λ͢Δͷ͔ͷઆ໌  4XJGUͰؔ਺ܕϓϩάϥϛϯάͷ࣮ફ * ίʔυͷ࣮ྫɺέʔεελσΟ ** ؔ਺ܕϓϩάϥϛϯάͷͨΊͷϥΠϒϥϦͷ঺հ ***

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

    4%, $PDPB ʹΑΔ੍ݶͱͦͷXPSLBSPVOE ؔ਺ϓϩάϥϛϯάελΠϧͰͷ8FCϓϩάϥϛϯά * 7BQPSͱ͍͏8"'Λ࢖ͬͯؔ਺ܕϓϩάϥϛϯάΛద༻ͯ͠ΈΔ ΞδΣϯμ ࠓ೔࿩͢ͷ͸͜Ε͚ͩʂ
  6. 8):48*'5

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

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

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

  10. 8):48*'5

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

  12. ͦ΋ͦ΋

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

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

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

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

  17. "QQMF88%$ 4XJGUൃද

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

  19. pMUFSSFEVDFNBQqBU.BQ

  20. 0QUJPOBM

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

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

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

  24. ͡Ό͋

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

  26. /0

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

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

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

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

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

  32. 4XJGUͰ͸ʜ w .PEVMBCMFͳσβΠϯ΍એݴతͳهड़Λ΍Γ΍͘͢͢Δ࢓૊Έ͕͋Δ w 0QUJPOBM "SSBZͳͲ͕'VODUPS.POBEͱ࣮ͯ͠૷͞Ε͍ͯΔ
 ϝιουνΣʔϯͰ͖Δ w 1SPUPDPM1SPUPDPM&YUFOTJPOͰܕΛ֦ு MJLFUZQFDMBTTUZQF

    DMBTTJOTUBODJBUJPO  w ԋࢉࢠͷఆ͕ٛՄೳ <*> <^>)
  33. w ෭࡞༻Λൃੜͤ͞ʹ͍͘ ࢀরಁաʹ͠΍͍͢  w σʔλܕ͕جຊJNNVUBCMFͳ஋ܕ w letʹΑΔଋറΛଅਐ͢Δݴޠ࢓༷ w *NNVUBCMFͳ஋͸ίϯύΠϥʹΑͬͯ࠷దԽ͞ΕΔ

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

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

  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͔ΒͷύϥμΠϜγϑτ͸Ψν
  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
  38. 'VDOUJPOBM4XJGU$PNNVOJUZ w'VODUJPOBM4XJGU$POGFSFODF
 IUUQGVOTXJGUDPOGDPN  wUZQFMJGU4XJGU[ wUIPVHIUCPU3VOFT

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

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

  41. ͋Εʁ

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

  43. 8):48*'5

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

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

  46. 4XJGUͰ࣮૷ࡁ enum List<T> { case null indirect case cons(T, List<T>)

    } ˢ๯಄ͷϦετͷ࣮૷Ͱ΋࠶ؼ&OVNΛ࢖͍ͬͯΔ
  47. UZQFBMJBTʹHFOFSJDT͕࢖͑ͳ͍

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

  49. ଞʹ΋ʜ wFYQFSJNFOUBMͰ͸͋Δ͕ؔ਺߹੒ԋࢉࢠ͕ຊମʹ࣮૷
 IUUQTHJUIVCDPNBQQMFTXJGUCMPCECDDCEGEEFTUEMJC JOUFSOBM4XJGU&YQFSJNFOUBM4XJGU&YQFSJNFOUBMTXJGU- w(FOFSJDT.BOJGFTUPͷதͰߴ֊ܕͷ࣮૷ܭը͋Γ
 IUUQTHJUIVCDPNBQQMFTXJGUCMPCECDDCEGEEFEPDT (FOFSJDT.BOJGFTUPNEIJHIFSLJOEFEUZQFT 
 protocol Functor

    { associatedtype A func fmap<FB where FB ~= Self>(f: A -> FB.A) -> FB }
  50. ׂͱΨϯΨϯมΘͬͯΔ wωοτͷ࣍৘ใ͸ݹ͘ͳ͍ͬͯΔ΋ͷ΋ଟ͍ wؔ਺ܕϓϩάϥϛϯάͷจ຺Ͱͷมߋ΋ؚ·ΕΔ 4XJGUJTFWPMWJOH UPP GBTU ʮ4XJGUͰؔ਺ܕϓϩάϥϛϯάແཧʯ ͬͯࢥͬͨਓ΋࠷৽৘ใʹ৮ΕΔͱ໘ന͍͔΋ʁ