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
Go の GC の不得意な部分を克服したい
taiyow
2
770
rails statsで大解剖 🔍 “B/43流” のRailsの育て方を歴史とともに振り返ります
shoheimitani
2
930
Haze - Real time background blurring
chrisbanes
1
510
Асинхронность неизбежна: как мы проектировали сервис уведомлений
lamodatech
0
710
menu基盤チームによるGoogle Cloudの活用事例~Application Integration, Cloud Tasks編~
yoshifumi_ishikura
0
110
「Chatwork」Android版アプリを 支える単体テストの現在
okuzawats
0
180
20年もののレガシープロダクトに 0からPHPStanを入れるまで / phpcon2024
hirobe1999
0
410
Refactor your code - refactor yourself
xosofox
1
260
テストコード文化を0から作り、変化し続けた組織
kazatohiei
2
1.5k
テストコードのガイドライン 〜作成から運用まで〜
riku929hr
1
130
これでLambdaが不要に?!Step FunctionsのJSONata対応について
iwatatomoya
2
3.6k
良いユニットテストを書こう
mototakatsu
5
2k
Featured
See All Featured
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
8
1.2k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
It's Worth the Effort
3n
183
28k
How STYLIGHT went responsive
nonsquared
95
5.2k
Measuring & Analyzing Core Web Vitals
bluesmoon
4
170
Agile that works and the tools we love
rasmusluckow
328
21k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
2
170
The Cost Of JavaScript in 2023
addyosmani
45
7k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
28
900
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.9k
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Ͱؔܕϓϩάϥϛϯάແཧʯ ͬͯࢥͬͨਓ࠷৽ใʹ৮ΕΔͱ໘ന͍͔ʁ