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.1k
Kotlin/Swift 有理数クラスのすすめ
Koichiro Oishi
August 10, 2017
Tweet
Share
More Decks by Koichiro Oishi
See All by Koichiro Oishi
アプリのユニットテストについて初歩から考えてみる/ABC UnitTest
koishi
3
740
iOSアプリ開発者から見たKotlin
koishi
2
680
5分でわかるPassword_Autofill_for_Apps
koishi
0
950
Other Decks in Programming
See All in Programming
MetricKitで予期せぬ終了を検知する話 / Detect unexpected termination with MetricKit
nekowen
1
200
Goのmultiple errorsについて (2024年4月版)
syumai
4
1.3k
Fragment Composition of GraphQL
quramy
13
1.6k
Build Apps for iOS, Android & Desktop in 100% Kotlin With Compose Multiplatform (mDevCamp 2024)
zsmb
0
480
Going beyond Apache Parquet's default settings
xhochy
0
140
Timeline エディター拡張入門
yucchiy
0
300
『Railsオワコン』と言われる時代に、なぜブルーモ証券はRailsを選ぶのか
free_world21
2
400
Elm Form Validation
bkuhlmann
0
520
Kotlin Multiplatform at Stable and Beyond (Android Makers 2024)
zsmb
0
550
Tailwind CSSを本気でカスタマイズする方法
fsubal
15
5.6k
パフォーマンスを求めてDBに機能を寄せる戦略
aoyagikouhei
0
110
Criando a Woovi em uma semana
daniloab
0
100
Featured
See All Featured
[RailsConf 2023] Rails as a piece of cake
palkan
28
4k
Why You Should Never Use an ORM
jnunemaker
PRO
51
8.7k
The Cult of Friendly URLs
andyhume
74
5.7k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
188
16k
KATA
mclloyd
16
12k
The MySQL Ecosystem @ GitHub 2015
samlambert
244
12k
Fireside Chat
paigeccino
22
2.6k
Build your cross-platform service in a week with App Engine
jlugia
226
17k
Teambox: Starting and Learning
jrom
128
8.4k
Clear Off the Table
cherdarchuk
85
310k
Reflections from 52 weeks, 52 projects
jeffersonlam
345
19k
Docker and Python
trallard
35
2.7k
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