Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Swift Functional Programming
Search
Hirofumi Wakasugi
September 24, 2017
Programming
0
260
Swift Functional Programming
(car (cdr ファンクション倶楽部))
https://esminc.connpass.com/event/57534/
で話そうと思ってたやつ
Hirofumi Wakasugi
September 24, 2017
Tweet
Share
More Decks by Hirofumi Wakasugi
See All by Hirofumi Wakasugi
Racc Introduction
5t111111
1
410
Keep Calm and Enjoy Gachima
5t111111
3
350
Middleman on Production
5t111111
0
160
名古屋Ruby会議03 高速一言感想
5t111111
3
1k
ドキュメント翻訳懺悔のLT ~進捗ダメです~
5t111111
1
580
UFO演算子について
5t111111
0
310
Rubyと戯れるOSS DTM
5t111111
4
740
Rubyの拡張をCrystalで書いてみる
5t111111
0
100
YARV INTRODUCTION
5t111111
0
86
Other Decks in Programming
See All in Programming
EventSourcingの理想と現実
wenas
6
2.3k
Pinia Colada が実現するスマートな非同期処理
naokihaba
4
220
macOS でできる リアルタイム動画像処理
biacco42
9
2.4k
レガシーシステムにどう立ち向かうか 複雑さと理想と現実/vs-legacy
suzukihoge
14
2.2k
Remix on Hono on Cloudflare Workers
yusukebe
1
290
距離関数を極める! / SESSIONS 2024
gam0022
0
280
Laravel や Symfony で手っ取り早く OpenAPI のドキュメントを作成する
azuki
2
120
Realtime API 入門
riofujimon
0
150
とにかくAWS GameDay!AWSは世界の共通言語! / Anyway, AWS GameDay! AWS is the world's lingua franca!
seike460
PRO
1
860
Kaigi on Rails 2024 〜運営の裏側〜
krpk1900
1
200
TypeScriptでライブラリとの依存を限定的にする方法
tutinoko
2
660
OnlineTestConf: Test Automation Friend or Foe
maaretp
0
110
Featured
See All Featured
Practical Orchestrator
shlominoach
186
10k
Rails Girls Zürich Keynote
gr2m
94
13k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
26
1.4k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
Automating Front-end Workflow
addyosmani
1366
200k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
665
120k
10 Git Anti Patterns You Should be Aware of
lemiorhan
654
59k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
364
24k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
A Modern Web Designer's Workflow
chriscoyier
693
190k
Facilitating Awesome Meetings
lara
50
6.1k
Gamification - CAS2011
davidbonilla
80
5k
Transcript
Swift Functional Programming Hirofumi Wakasugi (@5T111111)
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 . ())
!)JSPGVNJ8BLBTVHJ !U 4XJGUษڧத ؔܕϓϩάϥϛϯάษڧத ࣗݾհ
8IZ4XJGU ͳͥ4XJGUͷΛ͢Δͷ͔ͷઆ໌ 4XJGUͰؔܕϓϩάϥϛϯάͷ࣮ફ * ίʔυͷ࣮ྫɺέʔεελσΟ ** ؔܕϓϩάϥϛϯάͷͨΊͷϥΠϒϥϦͷհ ***
4%, $PDPB ʹΑΔ੍ݶͱͦͷXPSLBSPVOE ؔܕϓϩάϥϛϯάελΠϧͰͷ8FCϓϩάϥϛϯά * 7BQPSͱ͍͏8"'ΛͬͯؔܕϓϩάϥϛϯάΛద༻ͯ͠ΈΔ ΞδΣϯμ
8IZ4XJGU ͳͥ4XJGUͷΛ͢Δͷ͔ͷઆ໌ 4XJGUͰؔܕϓϩάϥϛϯάͷ࣮ફ * ίʔυͷ࣮ྫɺέʔεελσΟ ** ؔܕϓϩάϥϛϯάͷͨΊͷϥΠϒϥϦͷհ ***
4%, $PDPB ʹΑΔ੍ݶͱͦͷXPSLBSPVOE ؔϓϩάϥϛϯάελΠϧͰͷ8FCϓϩάϥϛϯά * 7BQPSͱ͍͏8"'ΛͬͯؔܕϓϩάϥϛϯάΛద༻ͯ͠ΈΔ ΞδΣϯμ ࠓ͢ͷ͜Ε͚ͩʂ
8):48*'5
w੩తͳܕνΣοΫͱܕ҆શੑΛඋ͑ͨܕγεςϜ͕͋Δ wωΠςΟϒίʔυʹίϯύΠϧ͞ΕΔ wαʔόʔαΠυͷ8FCϓϩάϥϛϯάͰ͑ΔՄೳੑ͕͋Δ ݸਓతͳझຯ ❤
w੩తͳܕνΣοΫͱܕ҆શੑΛඋ͑ͨܕγεςϜ͕͋Δ wωΠςΟϒίʔυʹίϯύΠϧ͞ΕΔ wαʔόʔαΠυͷ8FCϓϩάϥϛϯάͰ͑ΔՄೳੑ͕͋Δ ݸਓతͳझຯ (PͰ͠ΐ
w੩తͳܕνΣοΫͱܕ҆શੑΛඋ͑ͨܕγεςϜ͕͋Δ wωΠςΟϒίʔυʹίϯύΠϧ͞ΕΔ wαʔόʔαΠυͷ8FCϓϩάϥϛϯάͰ͑ΔՄೳੑ͕͋Δ ݸਓతͳझຯ Swift
8):48*'5
ؔܕϓϩάϥϛϯάͱ4XJGU
ͦͦ
4XJGUͰ ؔܕϓϩάϥϛϯά ͱ͍͏ωλ͕
શ͘৽͍͠ͷͰͳ͍
͘Β͍͞Ε͖ͯͨ ͨͿΜ
ͳΜͰ·ͨ͋͑ͯ ͦΜͳΛ͍ͨ͠ͷ͔
"QQMF88%$ 4XJGUൃද
͕ؔϑΝʔετΫϥεʁ
pMUFSSFEVDFNBQqBU.BQ
0QUJPOBM
ͥΜͿ 0CKFDUJWF$ʹ ͳ͔ͬͨͧʁ
ؔܕͷಛΛඋ͑ͨݴޠ͕ "QQMFެࣜͰ ʮJ04NBD04։ൃʯ ͱ͍͏ڊେϚʔέοτʹݱΕͨ
ྑ͘ѱ͘ ؔܕʹରͯ͠ ͕ू·Δ ͭͷ͖͔͚ͬʹͳͬͨ
͡Ό͋
4XJGUؔܕϓϩ άϥϛϯάݴޠͳ ͷ͔ʁ
/0
4XJGUͰؔܕϓϩ άϥϛϯάՄೳ ͔ʁ
*%0/`55)*/,40 ؔܕϓϩάϥϛϯάίʔσΟϯάελΠϧʹա͗ͳ ͍ͱ͍͏ݟํ͋Δ͕ɺ ؔܕͬΆ͍ػೳख๏ΛҰ෦औΓೖΕͯ ͦΕ͚ͩͰؔܕϓϩάϥϛϯάʹͳΒͳ͍ͱࢥ͏ FH+BWB4DSJQU+BWB3VCZ$ -JOR FUD
ͦ͏ࢥͬͯͨ࣌ظ͕Ӡʑ
͋Εʁ Α͘ݟͯΈΔͱʜ 4XJGUࢥͬͨҎ্ʹ ؔܕϓϩάϥϛϯά͕ Γͦ͢͏ʹݟ͑·ͧ͢ʁʁ
4XJGUͰʜ w ؔΛͱͯ͠ѻ͑Δ w ؔϑΝʔετΫϥε w typealiasͰؔͷܕఆٛՄೳ MJLFtypeJO)BTLFMM
4XJGUͰʜ w .PEVMBCMFͳσβΠϯએݴతͳهड़ΛΓ͘͢͢ΔΈ͕͋Δ w 0QUJPOBM "SSBZͳͲ͕'VODUPS.POBEͱ࣮ͯ͠͞Ε͍ͯΔ ϝιουνΣʔϯͰ͖Δ w 1SPUPDPM1SPUPDPM&YUFOTJPOͰܕΛ֦ு MJLFUZQFDMBTTUZQF
DMBTTJOTUBODJBUJPO w ԋࢉࢠͷఆ͕ٛՄೳ <*> <^>)
w ෭࡞༻Λൃੜͤ͞ʹ͍͘ ࢀরಁաʹ͍͢͠ w σʔλܕ͕جຊJNNVUBCMFͳܕ w letʹΑΔଋറΛଅਐ͢Δݴޠ༷ w *NNVUBCMFͳίϯύΠϥʹΑͬͯ࠷దԽ͞ΕΔ
4XJGUͰʜ
wͨΓલͷΑ͏ʹΫϩʔδϟ͕͋Δ wͨΓલͷΑ͏ʹύλʔϯϚον͕͋Δ wͨΓલͷΑ͏ʹHVBSEจ͕͋Δ wͨΓલͷΑ͏ʹඌ࠶ؼ͕࠷దԽ͞ΕΔ wͨΓલͷΑ͏ʹΧϦʔԽʹઐ༻ͷߏจ͕͋Δ 4XJGUͰফ͑·ͨ͠ʂ 4XJGUͰʜ
ެࣜͳݟղ w"QQMFࣗ4XJGU͕ؔܕͩͱશ͘ݴͬͯͳ͍
ެࣜͳݟղ 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͔ΒͷύϥμΠϜγϑτΨν
ެࣜͳݟղ “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
'VDOUJPOBM4XJGU$PNNVOJUZ w'VODUJPOBM4XJGU$POGFSFODF IUUQGVOTXJGUDPOGDPN wUZQFMJGU4XJGU[ wUIPVHIUCPU3VOFT
ઐͰॻ੶ग़͍ͯΔ
͜Ε͏ ؔܕϓϩάϥϛϯά͕Ͱ͖Δͱ ݴ͍͍ͬͯΜ͡Όͳ͍Ͱ͔͢Ͷʜ
͋Εʁ
ͳΜͰ·ͨ͋͑ͯࠓߋ ʮ4XJGUͰؔܕΆΑΆΑʙʯ Έ͍ͨͳΛ͍ͨ͠ͷ͔ શવ͜ͷཧ༝ʹͳͬͯͳ͘ͳ͍ʁ
8):48*'5
4XJGU1SPHSBNNJOH-BOHVBHF&WPMVUJPO IUUQTHJUIVCDPNBQQMFTXJGUFWPMVUJPO 4XJGUJTFWPMWJOH UPP GBTU
&OVNͰ࠶ؼతͳσʔλߏΛ࡞Εͳ͍
4XJGUͰ࣮ࡁ enum List<T> { case null indirect case cons(T, List<T>)
} ˢ಄ͷϦετͷ࣮Ͱ࠶ؼ&OVNΛ͍ͬͯΔ
UZQFBMJBTʹHFOFSJDT͕͑ͳ͍
UZQFBMJBTʹHFOFSJDT͕͑ͳ͍ 4XJGUͰ͑ΔΑ͏ʹͳͬͨ
ଞʹʜ 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 }
ׂͱΨϯΨϯมΘͬͯΔ wωοτͷ࣍ใݹ͘ͳ͍ͬͯΔͷଟ͍ wؔܕϓϩάϥϛϯάͷจ຺Ͱͷมߋؚ·ΕΔ 4XJGUJTFWPMWJOH UPP GBTU ʮ4XJGUͰؔܕϓϩάϥϛϯάແཧʯ ͬͯࢥͬͨਓ࠷৽ใʹ৮ΕΔͱ໘ന͍͔ʁ