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-either-type
Search
to4iki
March 31, 2015
Programming
0
130
swift-either-type
https://github.com/to4iki/EitherSwift
to4iki
March 31, 2015
Tweet
Share
More Decks by to4iki
See All by to4iki
suspend-view-controller-sample
to4iki
0
3.1k
ケースに応じたUICollectionViewのレイアウト実装パターン
to4iki
1
4.6k
ビューインプレッションの計測方法
to4iki
1
1k
秘伝の `gitconfig`
to4iki
1
420
Abema iOS Architecture
to4iki
12
3.3k
timetable-bot
to4iki
0
14k
BLoC Pattern Introduction with Swift
to4iki
2
1.2k
nel
to4iki
0
140
[iOS] ビデオチームのスモールスクラム
to4iki
0
60
Other Decks in Programming
See All in Programming
Serverless Rust: Your Low-Risk Entry Point to Rust in Production (and the benefits are huge)
lmammino
1
170
ABEMA iOS 大規模プロジェクトにおける段階的な技術刷新 / ABEMA iOS Technology Upgrade
akkyie
1
250
たのしいSocketのしくみ / Socket Under a Microscope
coe401_
8
1.4k
Datadog DBMでなにができる? JDDUG Meetup#7
nealle
0
160
機能が複雑化しても 頼りになる FactoryBotの話
tamikof
1
250
SwiftUI Viewの責務分離
elmetal
PRO
2
280
ソフトウェアエンジニアの成長
masuda220
PRO
12
2.2k
Django NinjaによるAPI開発の効率化とリプレースの実践
kashewnuts
1
300
The Clean ArchitectureがWebフロントエンドでしっくりこないのは何故か / Why The Clean Architecture does not fit with Web Frontend
twada
PRO
56
18k
Google Cloudとo11yで実現するアプリケーション開発者主体のDB改善
nnaka2992
1
130
技術を改善し続ける
gumioji
0
180
Duke on CRaC with Jakarta EE
ivargrimstad
0
180
Featured
See All Featured
Measuring & Analyzing Core Web Vitals
bluesmoon
6
260
Speed Design
sergeychernyshev
28
820
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
30
2.3k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
120k
Adopting Sorbet at Scale
ufuk
75
9.2k
Faster Mobile Websites
deanohume
306
31k
Product Roadmaps are Hard
iamctodd
PRO
51
11k
StorybookのUI Testing Handbookを読んだ
zakiyama
28
5.5k
Into the Great Unknown - MozCon
thekraken
35
1.7k
Thoughts on Productivity
jonyablonski
69
4.5k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5.2k
Fantastic passwords and where to find them - at NoRuKo
philnash
51
3k
Transcript
(Ծ)SwiftͰܕΛॻ ͍ͯΈͨ 2015-03-28 Meetup toshiki takezawa @to4iki
୭ʁ • ᖒढ़ق • 2012/4ೖࣾ • ث: Ruby, Scala, Swift
• ઌ30cm΄ͲΛͬͯผਓʹ • ઌ৽װઢʹͬͨ
ԿͬͯΜͷɺͬͯͨʁ • ιγϟήͷAPI࡞ • ిࢠϚϯΨͷϓϥοτϑΥʔϜΛ࡞͍ͬͯ· ͢ • ઌiOSΞϓϦΛϦϦʔε͠·ͨ͠
λΠτϧҒͦ͏͕ͩɺͷ ࣗ࡞ϥΠϒϥϦͷհ
ܕ
ܕ ͱݺΕΔͷ͕͋ΔΒ͍͠ݴޠ • Scala • Haskell • TypeScript 1.4 Union
Type
ྫ. Α͋͘Δྫ֎ॲཧ (scalaͰ)
try ~ catch ~ finally Javaͱಉ͡ try {
f() // throw new Exception() } catch { case e: Exception1 => ... case e: Exception2 => ... } finally { ... }
ؔܕϓϩάϥϛϯάͰ ྫ֎ͷΘΓʹ ࣦഊΛදݱ͢Δܕͱ ͦͷΛ༻͍Δ
Option nullճආ • ͕ͳ͍͜ͱΛܕͰදݱɻശͱߟ͑Δ • Some(A) or None • nullࢀর
Λഉআͯ͠Optional Λಋೖ͢Δ͜ͱͰɺ ϓϩάϥϚʹΤϥʔॲཧΛڧ੍͠ɺͦͷΑ͏ͳό άΛະવʹ͙͜ͱ͕Ͱ͖Δɻ • Promiseಉ͡(কདྷతʹத()͕ಘΒΕΔശ)
def head[A](xs: List[A]) = if(xs.isEmpty) None else
Some(xs.head) head(List()) // None head(List(1,2,3)) // Some(1) head(List(1,2,3)).map { x => x + 12 } // Some(13) head(List(1,2,3)).getOrElse(0) // 1 head(List()).getOrElse(0) // 0
Either[A, B] ܕ • ;ͨͭͷܕͷΛͭՄೳੑ͕͋Δ͜ͱΛࣔ ͢ܕ • ޭͷใΛRightͰɺࣦഊͷใΛLeftͰද ݱ͢Δ
def parseInt(s: String): Either[Exception, Int] = try Right(s.toInt)
catch { case e:Exception => Left(e) } parseInt(“256") // Right(256) parseInt(“abc") // Left(java.lang.NumberFormatException: For input string: “abc") parseInt("256").right map { _.toFloat } // Right(256.0) parseInt("abc").right map { _.toFloat } // ݁Ռ͕Left(Exception)ͷ߹ɺtoFloat࣮ߦ͞Εͣʹɺ ExceptionΛ͢Δ
ଞʹ • Try[A] • ྫ֎Λ͛ΔՄೳੑͷ͋ΔॲཧΛநԽ • https://gist.github.com/j5ik2o/5471851 • scalaz.Validation[E, A]
• Τϥʔͷू߹ or ݁Ռͷσʔλ
SwiftͰ͍͍ͨͳ…
લఏ • try ~ catch: ແ͍ɻiOSͰྫ֎ൃੜͨ͠Βࡴ ͢จԽ • Option: nullͷΘΓʹΤϥʔใΛ࣋ͯͳ͍
• ErrorΛtupleͰฦ͢ͷ؆қతʹΞϦ
ఆٛ
• ੵ • ͲͪΒ͔ΒɺΛऔΕΔ → tuple • • ͲͪΒ͔͔ΒɺΛऔΔ
→ enum • ΤϥʔॲཧΛͯ͠ཉ͍͠ͱ͍͏ҙਤΛܕͱͯ͠໌ࣔͰ͖Δ • OptionͷNullνΣοΫΛͯ͠ཉ͍͠ࣄΛܕͰදݱग़དྷΔͷͱಉ͡ • tupleͱҧ͍ਖ਼ৗܥͱΤϥʔܥͷذΛࣗવʹॻ͚Δ
ScalaͷeitherΛॻ͍ͯΈͨ!
https://github.com/to4iki/EitherSwift
// example let result: Either<Error, Int> = parseInt(“2”)
// fold -‐ ਖ਼ৗܥͱΤϥʔܥͷذΛࣗવʹ result.fold( { (e: Error) -‐> String in "left" }, { (s: Int) -‐> String in "right"} ) // right // getOrElse(&&) result.right.getOrElse { 0 } // 2 result.right ?? 0 // 2 // map result.left.map { $0.reason + "left" } // .Left() result.right.map { $0 + 2 } // .Right(4) // flatMap result.left.flatMap { _ in Either<Error, Int>(right: 12) } // .Left() result.right.flatMap { _ in Either<Error, Int>(right: 12) } // .Right(12)
// example let result: Either<Error, Int> = parseInt(“2”)
// chain // Like null Coalescing Operator result.chain { (i: Int) -‐> Either<Error, Int> in Either(right: i * i) } // .Right(4) parseInt("a").chain { (i: Int) -‐> Either<Error, Int> in Either(right: i * i) } //.Left("parse error") // recover / recoverWith // like null Coalescing Operator result.recoverWith { Either(right: 0) } // .Right(2) parseInt("a").recoverWith { Either(right: 0) } // .Right(0) parseInt("a").recoverWith({ parseInt("b") }).recoverWith({ parseInt("3") }) // .Right(3) parseInt("a").recover { 1 } // .Right(1)
Α͔ͬͨΒ˒͚ͭͯԼ͍͞!
͋Γ͕ͱ͏͍͟͝·ͨ͠