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
420
Keep Calm and Enjoy Gachima
5t111111
3
350
Middleman on Production
5t111111
0
160
名古屋Ruby会議03 高速一言感想
5t111111
3
1k
ドキュメント翻訳懺悔のLT ~進捗ダメです~
5t111111
1
590
UFO演算子について
5t111111
0
320
Rubyと戯れるOSS DTM
5t111111
4
740
Rubyの拡張をCrystalで書いてみる
5t111111
0
110
YARV INTRODUCTION
5t111111
0
88
Other Decks in Programming
See All in Programming
Fibonacci Function Gallery - Part 1
philipschwarz
PRO
0
220
htmxって知っていますか?次世代のHTML
hiro_ghap1
0
340
たのしいparse.y
ydah
3
120
「とりあえず動く」コードはよい、「読みやすい」コードはもっとよい / Code that 'just works' is good, but code that is 'readable' is even better.
mkmk884
3
480
競技プログラミングへのお誘い@阪大BOOSTセミナー
kotamanegi
0
360
Security_for_introducing_eBPF
kentatada
0
110
Kaigi on Railsに初参加したら、その日にLT登壇が決定した件について
tama50505
0
100
アクターシステムに頼らずEvent Sourcingする方法について
j5ik2o
4
290
ある日突然あなたが管理しているサーバーにDDoSが来たらどうなるでしょう?知ってるようで何も知らなかったDDoS攻撃と対策 #phpcon.2024
akase244
1
140
Effective Signals in Angular 19+: Rules and Helpers @ngbe2024
manfredsteyer
PRO
0
140
RWC 2024 DICOM & ISO/IEC 2022
m_seki
0
210
テスト自動化失敗から再挑戦しチームにオーナーシップを委譲した話/STAC2024 macho
ma_cho29
1
1.3k
Featured
See All Featured
Speed Design
sergeychernyshev
25
670
Designing Dashboards & Data Visualisations in Web Apps
destraynor
229
52k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.9k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
28
2.1k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
How to Ace a Technical Interview
jacobian
276
23k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
330
21k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
Side Projects
sachag
452
42k
A Philosophy of Restraint
colly
203
16k
Faster Mobile Websites
deanohume
305
30k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
17
2.3k
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Ͱؔܕϓϩάϥϛϯάແཧʯ ͬͯࢥͬͨਓ࠷৽ใʹ৮ΕΔͱ໘ന͍͔ʁ