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
120
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
2.6k
ケースに応じたUICollectionViewのレイアウト実装パターン
to4iki
1
4.2k
ビューインプレッションの計測方法
to4iki
0
900
秘伝の `gitconfig`
to4iki
1
400
Abema iOS Architecture
to4iki
12
3.1k
timetable-bot
to4iki
0
14k
BLoC Pattern Introduction with Swift
to4iki
2
1.1k
nel
to4iki
0
120
[iOS] ビデオチームのスモールスクラム
to4iki
0
48
Other Decks in Programming
See All in Programming
Ruby Function Composition
bkuhlmann
1
330
HUIT新歓2024「競技プログラミング、やってみませんか?」
slephy2784
1
250
[技育CAMPアカデミア]アイディアを形に!【超入門】スマホアプリ開発〜リリースまでの流れをご紹介
teamlab
PRO
0
350
ADRを一年運用してみた/adr_after_a_year
hanhan1978
7
2.2k
スクラムチームと認知負荷 - ニフティのスクラムトーク Vol2. / NIFTY Tech Talk #18
niftycorp
PRO
1
120
CircleCIを活用して AWSへの継続的デリバリーを 実践する
coconala_engineer
1
230
Folding Cheat Sheet #2
philipschwarz
PRO
0
110
Hanami and htmx
bkuhlmann
0
190
try! Swift Tokyo 初参加報告LT
hinakko2
0
190
Git Lint
bkuhlmann
4
740
CQRS/ES avec Symfony, c’est (trop) bien !
jeremyfreeagent
1
630
チーム力を高めるスクラム実践法:カンバン公開と課題攻略について - ニフティのスクラムトーク Vol. 2 - NIFTY Tech Talk #18
niftycorp
PRO
1
110
Featured
See All Featured
Understanding Cognitive Biases in Performance Measurement
bluesmoon
6
990
Creatively Recalculating Your Daily Design Routine
revolveconf
209
11k
How to name files
jennybc
64
92k
Reflections from 52 weeks, 52 projects
jeffersonlam
344
19k
What's new in Ruby 2.0
geeforr
337
31k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
15
1.4k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
115
18k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
13
1.5k
Gamification - CAS2011
davidbonilla
76
4.6k
GitHub's CSS Performance
jonrohan
1023
450k
Design by the Numbers
sachag
274
18k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
1
3.4k
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)
Α͔ͬͨΒ˒͚ͭͯԼ͍͞!
͋Γ͕ͱ͏͍͟͝·ͨ͠