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
320
Rubyと戯れるOSS DTM
5t111111
4
740
Rubyの拡張をCrystalで書いてみる
5t111111
0
100
YARV INTRODUCTION
5t111111
0
86
Other Decks in Programming
See All in Programming
AI時代におけるSRE、 あるいはエンジニアの生存戦略
pyama86
6
1.2k
CSC509 Lecture 09
javiergs
PRO
0
140
カンファレンスの「アレ」Webでなんとかしませんか? / Conference “thing” Why don't you do something about it on the Web?
dero1to
1
110
[Do iOS '24] Ship your app on a Friday...and enjoy your weekend!
polpielladev
0
110
Arm移行タイムアタック
qnighy
0
340
NSOutlineView何もわからん:( 前編 / I Don't Understand About NSOutlineView :( Pt. 1
usagimaru
0
340
광고 소재 심사 과정에 AI를 도입하여 광고 서비스 생산성 향상시키기
kakao
PRO
0
170
Streams APIとTCPフロー制御 / Web Streams API and TCP flow control
tasshi
2
350
ペアーズにおけるAmazon Bedrockを⽤いた障害対応⽀援 ⽣成AIツールの導⼊事例 @ 20241115配信AWSウェビナー登壇
fukubaka0825
6
2k
Kaigi on Rails 2024 〜運営の裏側〜
krpk1900
1
240
WebフロントエンドにおけるGraphQL(あるいはバックエンドのAPI)との向き合い方 / #241106_plk_frontend
izumin5210
4
1.4k
ピラミッド、アイスクリームコーン、SMURF: 自動テストの最適バランスを求めて / Pyramid Ice-Cream-Cone and SMURF
twada
PRO
10
1.3k
Featured
See All Featured
How to Think Like a Performance Engineer
csswizardry
20
1.1k
How to train your dragon (web standard)
notwaldorf
88
5.7k
No one is an island. Learnings from fostering a developers community.
thoeni
19
3k
Designing the Hi-DPI Web
ddemaree
280
34k
Speed Design
sergeychernyshev
25
620
Embracing the Ebb and Flow
colly
84
4.5k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
28
2k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.7k
Designing for Performance
lara
604
68k
Optimising Largest Contentful Paint
csswizardry
33
2.9k
A Philosophy of Restraint
colly
203
16k
5 minutes of I Can Smell Your CMS
philhawksworth
202
19k
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Ͱؔܕϓϩάϥϛϯάແཧʯ ͬͯࢥͬͨਓ࠷৽ใʹ৮ΕΔͱ໘ന͍͔ʁ