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
86
Swift Concurrencyを利用したUIViewController表示の排他制御の実装
to4iki
0
3.3k
ケースに応じたUICollectionViewのレイアウト実装パターン
to4iki
1
4.8k
ビューインプレッションの計測方法
to4iki
1
1.1k
秘伝の `gitconfig`
to4iki
1
450
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
Quand Symfony, ApiPlatform, OpenAI et LangChain s'allient pour exploiter vos PDF : de la théorie à la production…
ahmedbhs123
0
210
Rubyでやりたい駆動開発 / Ruby driven development
chobishiba
1
740
AI駆動のマルチエージェントによる業務フロー自動化の設計と実践
h_okkah
0
190
#kanrk08 / 公開版 PicoRubyとマイコンでの自作トレーニング計測装置を用いたワークアウトの理想と現実
bash0c7
1
880
Google Agent Development Kit でLINE Botを作ってみた
ymd65536
2
260
AIプログラマーDevinは PHPerの夢を見るか?
shinyasaita
1
230
코딩 에이전트 체크리스트: Claude Code ver.
nacyot
0
840
脱Riverpod?fqueryで考える、TanStack Queryライクなアーキテクチャの可能性
ostk0069
0
320
ペアプロ × 生成AI 現場での実践と課題について / generative-ai-in-pair-programming
codmoninc
2
20k
20250704_教育事業におけるアジャイルなデータ基盤構築
hanon52_
5
880
dbt民主化とLLMによる開発ブースト ~ AI Readyな分析サイクルを目指して ~
yoshyum
3
1.1k
スタートアップの急成長を支えるプラットフォームエンジニアリングと組織戦略
sutochin26
1
6.5k
Featured
See All Featured
Side Projects
sachag
455
42k
Java REST API Framework Comparison - PWX 2021
mraible
31
8.7k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
50k
[RailsConf 2023] Rails as a piece of cake
palkan
55
5.7k
Fantastic passwords and where to find them - at NoRuKo
philnash
51
3.3k
StorybookのUI Testing Handbookを読んだ
zakiyama
30
5.9k
Making the Leap to Tech Lead
cromwellryan
134
9.4k
Building Adaptive Systems
keathley
43
2.7k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
31
1.3k
Testing 201, or: Great Expectations
jmmastey
43
7.6k
The Art of Programming - Codeland 2020
erikaheidi
54
13k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
281
13k
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)
Α͔ͬͨΒ˒͚ͭͯԼ͍͞!
͋Γ͕ͱ͏͍͟͝·ͨ͠