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
3k
ケースに応じたUICollectionViewのレイアウト実装パターン
to4iki
1
4.5k
ビューインプレッションの計測方法
to4iki
1
1k
秘伝の `gitconfig`
to4iki
1
400
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
53
Other Decks in Programming
See All in Programming
cmp.Or に感動した
otakakot
3
160
WebフロントエンドにおけるGraphQL(あるいはバックエンドのAPI)との向き合い方 / #241106_plk_frontend
izumin5210
4
1.4k
3rd party scriptでもReactを使いたい! Preact + Reactのハイブリッド開発
righttouch
PRO
1
600
Better Code Design in PHP
afilina
PRO
0
130
LLM生成文章の精度評価自動化とプロンプトチューニングの効率化について
layerx
PRO
2
190
見せてあげますよ、「本物のLaravel批判」ってやつを。
77web
7
7.7k
Streams APIとTCPフロー制御 / Web Streams API and TCP flow control
tasshi
2
350
タクシーアプリ『GO』のリアルタイムデータ分析基盤における機械学習サービスの活用
mot_techtalk
4
1.4k
What’s New in Compose Multiplatform - A Live Tour (droidcon London 2024)
zsmb
1
470
.NET のための通信フレームワーク MagicOnion 入門 / Introduction to MagicOnion
mayuki
1
1.5k
Creating a Free Video Ad Network on the Edge
mizoguchicoji
0
120
AWS IaCの注目アップデート 2024年10月版
konokenj
3
3.3k
Featured
See All Featured
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.8k
Designing Experiences People Love
moore
138
23k
The Power of CSS Pseudo Elements
geoffreycrofte
73
5.3k
Rails Girls Zürich Keynote
gr2m
94
13k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
31
2.7k
Optimizing for Happiness
mojombo
376
70k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
506
140k
The Cost Of JavaScript in 2023
addyosmani
45
6.7k
The Invisible Side of Design
smashingmag
298
50k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
232
17k
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)
Α͔ͬͨΒ˒͚ͭͯԼ͍͞!
͋Γ͕ͱ͏͍͟͝·ͨ͠