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
400
Keep Calm and Enjoy Gachima
5t111111
3
350
Middleman on Production
5t111111
0
150
名古屋Ruby会議03 高速一言感想
5t111111
3
1k
ドキュメント翻訳懺悔のLT ~進捗ダメです~
5t111111
1
560
UFO演算子について
5t111111
0
310
Rubyと戯れるOSS DTM
5t111111
4
720
Rubyの拡張をCrystalで書いてみる
5t111111
0
100
YARV INTRODUCTION
5t111111
0
82
Other Decks in Programming
See All in Programming
2024-10-02 dev2next - Application Observability like you've never heard before
jonatan_ivanov
0
160
C#および.NETに対する誤解をひも解く
ymd65536
0
230
What is TDD?
urakawa_jinsei
1
200
実践Dash - 手を抜きながら本気で作るデータApplicationの基本と応用 / Dash for Python and Baseball
shinyorke
2
230
pytest プラグインを開発して DRY に自動テストを書こう
inuatsu
2
250
Removing Corepack
yosuke_furukawa
PRO
9
1.1k
NANIMACHI
naokiito
0
930
NEWTにおけるiOS18対応の進め方
ryu1sazae
0
190
App Router 悲喜交々
quramy
7
370
Micro Frontends for Java Microservices - dev2next 2024
mraible
PRO
0
190
ECS向けのドリフト検知機構を実装してみた
tkikuc
0
270
[KR] Server Driven Compose With Firebase
skydoves
2
160
Featured
See All Featured
Become a Pro
speakerdeck
PRO
24
4.9k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
231
17k
Keith and Marios Guide to Fast Websites
keithpitt
408
22k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
44
2k
Java REST API Framework Comparison - PWX 2021
mraible
PRO
28
7.5k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
41
9.2k
Designing for Performance
lara
604
68k
Designing on Purpose - Digital PM Summit 2013
jponch
114
6.9k
Designing Experiences People Love
moore
138
23k
Thoughts on Productivity
jonyablonski
67
4.2k
Product Roadmaps are Hard
iamctodd
PRO
48
10k
Raft: Consensus for Rubyists
vanstee
136
6.6k
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Ͱؔܕϓϩάϥϛϯάແཧʯ ͬͯࢥͬͨਓ࠷৽ใʹ৮ΕΔͱ໘ന͍͔ʁ