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
140
swift-either-type
https://github.com/to4iki/EitherSwift
to4iki
March 31, 2015
Tweet
Share
More Decks by to4iki
See All by to4iki
Claude Code の活用事例
to4iki
0
78
Swift Concurrencyを利用したUIViewController表示の排他制御の実装
to4iki
0
3.3k
ケースに応じたUICollectionViewのレイアウト実装パターン
to4iki
1
4.7k
ビューインプレッションの計測方法
to4iki
1
1.1k
秘伝の `gitconfig`
to4iki
1
440
Abema iOS Architecture
to4iki
12
3.4k
timetable-bot
to4iki
0
14k
BLoC Pattern Introduction with Swift
to4iki
2
1.3k
nel
to4iki
0
160
Other Decks in Programming
See All in Programming
プロダクト志向ってなんなんだろうね
righttouch
PRO
0
170
AWS CDKの推しポイント 〜CloudFormationと比較してみた〜
akihisaikeda
3
320
GraphRAGの仕組みまるわかり
tosuri13
8
500
“いい感じ“な定量評価を求めて - Four Keysとアウトカムの間の探求 -
nealle
0
340
設計やレビューに悩んでいるPHPerに贈る、クリーンなオブジェクト設計の指針たち
panda_program
6
1.8k
Select API from Kotlin Coroutine
jmatsu
1
200
Rubyでやりたい駆動開発 / Ruby driven development
chobishiba
1
520
Quand Symfony, ApiPlatform, OpenAI et LangChain s'allient pour exploiter vos PDF : de la théorie à la production…
ahmedbhs123
0
110
Team topologies and the microservice architecture: a synergistic relationship
cer
PRO
0
1.1k
技術同人誌をMCP Serverにしてみた
74th
1
450
Deep Dive into ~/.claude/projects
hiragram
10
2.1k
システム成長を止めない!本番無停止テーブル移行の全貌
sakawe_ee
1
150
Featured
See All Featured
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
252
21k
jQuery: Nuts, Bolts and Bling
dougneiner
63
7.8k
Automating Front-end Workflow
addyosmani
1370
200k
What's in a price? How to price your products and services
michaelherold
246
12k
We Have a Design System, Now What?
morganepeng
53
7.7k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
20
1.3k
Six Lessons from altMBA
skipperchong
28
3.9k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
Intergalactic Javascript Robots from Outer Space
tanoku
271
27k
Building an army of robots
kneath
306
45k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
48
2.9k
Large-scale JavaScript Application Architecture
addyosmani
512
110k
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)
Α͔ͬͨΒ˒͚ͭͯԼ͍͞!
͋Γ͕ͱ͏͍͟͝·ͨ͠