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
Kotlin/Swift 有理数クラスのすすめ
Search
Koichiro Oishi
August 10, 2017
Programming
0
1.2k
Kotlin/Swift 有理数クラスのすすめ
Koichiro Oishi
August 10, 2017
Tweet
Share
More Decks by Koichiro Oishi
See All by Koichiro Oishi
アプリのユニットテストについて初歩から考えてみる/ABC UnitTest
koishi
3
790
iOSアプリ開発者から見たKotlin
koishi
2
690
5分でわかるPassword_Autofill_for_Apps
koishi
0
1k
Other Decks in Programming
See All in Programming
Android 15 でアクションバー表示時にステータスバーが白くなってしまう問題
tonionagauzzi
0
150
Go言語でターミナルフレンドリーなAIコマンド、afaを作った/fukuokago20_afa
monochromegane
2
140
Realtime API 入門
riofujimon
0
120
Universal Linksの実装方法と陥りがちな罠
kaitokudou
1
230
PHP でアセンブリ言語のように書く技術
memory1994
PRO
1
150
Server Driven Compose With Firebase
skydoves
0
420
色々なIaCツールを実際に触って比較してみる
iriikeita
0
290
CSC305 Lecture 13
javiergs
PRO
0
130
EventSourcingの理想と現実
wenas
6
2.1k
[PyCon Korea 2024 Keynote] 커뮤니티와 파이썬, 그리고 우리
beomi
0
120
Honoの来た道とこれから
yusukebe
19
3.1k
Synchronizationを支える技術
s_shimotori
1
150
Featured
See All Featured
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
25
1.8k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.5k
Designing Experiences People Love
moore
138
23k
Product Roadmaps are Hard
iamctodd
PRO
48
10k
Navigating Team Friction
lara
183
14k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
48k
Practical Orchestrator
shlominoach
186
10k
Docker and Python
trallard
40
3.1k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
504
140k
Agile that works and the tools we love
rasmusluckow
327
21k
StorybookのUI Testing Handbookを読んだ
zakiyama
26
5.2k
Transcript
༗ཧΫϥεͷ͢͢Ί 2017/08/08 Swift/Kotlin߹ಉษڧձ @ΦΠγοΫευοτେגࣜձࣾ Koishi 1 Koichiro Oishi, 2017/08/08
ࣗݾհ େੴ ߂Ұ γεςϜɾΞφϥΠζגࣜձࣾ iOSΞϓϦ։ൃऀ ΞΧϯτؔ࿈ * @mego_ * koishi
2 Koichiro Oishi, 2017/08/08
ίϯςΩετ iOSΞϓϦΤϯδχΞͰ͢ɻ Objective-CͱSwiftࣝ͋Δ(͋Δఔ) JavaͳΜͱͳͬͯ͘Δɻ Kotlinೖத ! 3 Koichiro Oishi, 2017/08/08
ࢀߟจݙ Swift ࣮ફೖ ※ஶऀͷ1ਓɺੴ͞Μ͔Βα ΠϯΒ͍·ͨ͠ɻ 4 Koichiro Oishi, 2017/08/08
ࢀߟจݙ Kotlin ελʔτϒοΫ ௨শʮ͜ຊʯ ※ஶऀͷͨΖ͏͞Μ͔ΒαΠϯ Β͍·ͨ͠ɻ 5 Koichiro Oishi, 2017/08/08
͓ ༗ཧΫϥεͷ࣮ 6 Koichiro Oishi, 2017/08/08
ݩωλ ʮKotlin ελʔτϒοΫʯ • p.27ʙp.40 • ୈ3ষ KotlinΨΠυπΞʔ • KotlinͷεϚʔτͳݴޠ༷͕ѲͰ͖Δ༰
7 Koichiro Oishi, 2017/08/08
ݩʑͷग़య Scala εέʔϥϒϧ ϓϩάϥϛϯά • ௨শίοϓຊ 8 Koichiro Oishi, 2017/08/08
༗ཧΫϥε • ༗ཧͱࢠ(numerator)ͱ(denominator)Ͱ දݱ͢Δ • ༗ཧ()Λදݱ͢ΔΫϥεRationalͷ࣮ ͦΕͰߦͬͯΈ·͠ΐ͏ʂ 9 Koichiro Oishi,
2017/08/08
Ϋϥεͷఆٛ(Kotlin) ࢠͱΛϓϩύςΟͱͯ࣋ͭ͠ΫϥεRational Λఆٛ͢Δ class Rational(val numerator: Int, val denominator: Int)
• numeratorͱdenominator͜Ε͚ͩͰϓϩύςΟ ʹͳΔɻ • ͪͳΈʹvalఆɺvarม 10 Koichiro Oishi, 2017/08/08
Ϋϥεͷఆٛ(Swift) struct Rational { let numerator: Int let denominator: Int
} let rational = Rational(numerator: 1, denominator: 2) • ϓϩύςΟΛఆٛ͢ΕΠχγϟϥΠβͷҾͱ ͯͦ͠ͷ··͑Δ • ͪͳΈʹletఆɺvarม 11 Koichiro Oishi, 2017/08/08
ϝιουͷఆٛ(Kotlin) print(rational)ͨ͠ࡍʹͷදࣔΛͰ͖ΔΑ͏ʹ͢Δ Rational@279f2327 Ͱͳ͘ 1/2 ͳͲͱ͍ͨ͠ class Rational(val numerator: Int,
val denominator: Int) { override fun toString(): String = "${numerator}/${denominator}" } • toString()ΛΦʔόϥΠυ͢Δ 12 Koichiro Oishi, 2017/08/08
ϝιουͷఆٛ(Swift) extension Rational: CustomStringConvertible { var description: String { return
"\(numerator)/\(denominator)" } } • CustomStringConvertibleϓϩτίϧʹ४ڌͨ͠ extensionͰdescriptionϓϩύςΟΛ࣮ 13 Koichiro Oishi, 2017/08/08
ΠχγϟϥΠβ(Kotlin) ʹ0͕ೖ͞Εͨ߹Τϥʔͱ͢ΔͨΊΠχ γϟϥΠβͰ࣮͢Δ class Rational(val numerator: Int, val denominator: Int)
{ init { require(denominator != 0, {"denominator must not be null"}) } override fun toString(): String = "${numerator}/${denominator}" } • ؔrequireͰ͕0Ҏ֎Ͱ͋Δ͜ͱΛཁٻɺ ͨ͠߹ΤϥʔΛ͛Δ 14 Koichiro Oishi, 2017/08/08
ΠχγϟϥΠβ(Swift ͦͷ1) ࣦഊͨ͠Βྫ֎Λ͛Δύλʔϯ init(numerator: Int, denominator: Int) throws { if
denominator == 0 { throw NSError(domain: "denominator must not be null", code: -1, userInfo: nil) } self.numerator = numerator self.denominator = denominator } • ࠓճͪ͜ΒΛྫͰਐΊ·͢ 15 Koichiro Oishi, 2017/08/08
ΠχγϟϥΠβ(Swift ͦͷ2) ࣦഊͨ͠ΒnilΛฦ͢ύλʔϯ init?(numerator: Int, denominator: Int) { if denominator
== 0 { return nil } self.numerator = numerator self.denominator = denominator } • Γ͕Optionalܕʹ • ݺͼग़͠ଆͰUnwrap͕ඞཁ 16 Koichiro Oishi, 2017/08/08
ඇެ։ϓϩύςΟϝιου(Kotlin) ࠷େެ(greatest common divisor)ΛٻΊ͍ͨ ྫ͑ɺ4/6ͷ߹2/3ͱ͍ͨ͠ɻ tailrec private fun gcd(a: Int,
b: Int): Int = if (b == 0) a else gcd(b, a% b) • private͚ͭΕϓϥΠϕʔτ • ͞Βʹ࠶ؼؔʹtailrecΛ͚ͭΔ 17 Koichiro Oishi, 2017/08/08
ඇެ։ϓϩύςΟϝιου(Swift ͦͷ1) ಉ͘͡࠷େެ(greatest common divisor)ΛٻΊΔ ϝιου private func gcd(a: Int,
b: Int) -> Int { return b == 0 ? a : gcd(a: b, b: a % b) } • ϩδοΫࣗମಉ͡ 18 Koichiro Oishi, 2017/08/08
ඇެ։ϓϩύςΟϝιου(Swift ͦͷ2) extension Rational: CustomStringConvertible { var description: String {
let g = gcd(a: abs(numerator), b: abs(denominator)) return "\(numerator/g)/\(denominator/g)" } private func gcd(a: Int, b: Int) -> Int { return b == 0 ? a : gcd(a: b, b: a % b) } } ࣮લˠ10/6 ࣮ޙˠ5/3 19 Koichiro Oishi, 2017/08/08
ԋࢉࢠΦʔόʔϩʔυ(Kotlin ͦͷ1) ϝιουplusΛՃͯ͠Rationalಉ࢜ͷ͠ࢉΛͰ͖ ΔΑ͏ʹ͢Δ fun plus(that: Rational): Rational = Rational(
numerator * that.denominator + that.numerator * denominator, denominator * that.denominator ) • rational1.plus(rational2) ͱॻ͚ΔΑ͏ʹͳΔ 20 Koichiro Oishi, 2017/08/08
ԋࢉࢠΦʔόʔϩʔυ(Kotlin ͦͷ2) ͞Βʹrational1 + ratiolan2 Έ͍ͨʹॻ͖͍ͨ operator fun plus(that: Rational):
Rational = Rational( numerator * that.denominator + that.numerator * denominator, denominator * that.denominator ) • ԋࢉࢠΦʔόʔϩʔυ = operator overload Rational(10, 6) + Rational(1, 3) 2/1 ͱͰ͖Δ 21 Koichiro Oishi, 2017/08/08
ԋࢉࢠΦʔόʔϩʔυ(Swift) SwiftͰԋࢉࢠΛΦʔόʔϩʔυ͢Δͱ͜͏ͳΔ static func +(left: Rational, right: Rational) -> Rational
{ return try! Rational( numerator: left.numerator * right.denominator + right.numerator * left.denominator, denominator: left.denominator * right.denominator) } 22 Koichiro Oishi, 2017/08/08
ϝιουͷΦʔόʔϩʔυ(Kotlin) Rational(1, 2) + 1ͷΑ͏ͳܭࢉΛͰ͖ΔΑ͏ʹ͢Δɻ operator fun plus(n: Int): Rational
= Rational(numerator + n * denominator, denominator) Rational(10, 6) + 1 8/3 ͱͰ͖ΔΑ͏ʹ 23 Koichiro Oishi, 2017/08/08
ϝιουͷΦʔόʔϩʔυ(Swift) static func +(rational: Rational, int: Int) -> Rational {
return try! Rational(numerator: rational.numerator + int * rational.denominator, denominator: rational.denominator) } 24 Koichiro Oishi, 2017/08/08
֦ுؔ(Kotlin) • Intʹର͢Δ֦ுؔͱͯ͠RationalΛҾͱ͢Δ plusϝιουΛՃ operator fun Int.plus(r: Rational): Rational =
r + this 1 + Rational(10, 6) 8/3 ͱͰ͖ΔΑ͏ʹ 25 Koichiro Oishi, 2017/08/08
֦ுؔ(Swift) • +ʹҾͱͯ͠ࠨลInt + ӈลRationalΛ࣮ static func +(left: Int, right:
Rational) -> Rational { return right + left } 26 Koichiro Oishi, 2017/08/08
·ͱΊ ͬͨ͜ͱ • Ϋϥεͷఆٛ • ϓϩύςΟɺϝιουɺΠχγϟϥΠβͷఏࣔ • ؔΦʔόϩʔυ • طଘͷܕʹϝιουΛՃ(֦ுؔ)
ଞͷݴޠͰ༗ཧΫϥεͷ࣮Λͯ͠ΈΔͱษڧʹͳ Γͦ͏Ͱ͢Ͷʁ 27 Koichiro Oishi, 2017/08/08
͝հͨ͠Swiftͷ࣮ྫͪ͜Β • https:/ /gist.github.com/koishi -> SwiftRational.swift • https:/ /gist.github.com/koishi/ d54a38e3216aaaf87ac592465adb0695
ποίϛେܴͰ͢ 28 Koichiro Oishi, 2017/08/08
͝੩ௌ͋Γ͕ͱ͏ ͍͟͝·ͨ͠ɻ 29 Koichiro Oishi, 2017/08/08