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
800
iOSアプリ開発者から見たKotlin
koishi
2
690
5分でわかるPassword_Autofill_for_Apps
koishi
0
1k
Other Decks in Programming
See All in Programming
Jakarta EE meets AI
ivargrimstad
0
200
What’s New in Compose Multiplatform - A Live Tour (droidcon London 2024)
zsmb
1
480
最新TCAキャッチアップ
0si43
0
190
TypeScript Graph でコードレビューの心理的障壁を乗り越える
ysk8hori
2
1.1k
聞き手から登壇者へ: RubyKaigi2024 LTでの初挑戦が 教えてくれた、可能性の星
mikik0
1
130
距離関数を極める! / SESSIONS 2024
gam0022
0
280
Jakarta EE meets AI
ivargrimstad
0
650
受け取る人から提供する人になるということ
little_rubyist
0
230
Micro Frontends Unmasked Opportunities, Challenges, Alternatives
manfredsteyer
PRO
0
100
[Do iOS '24] Ship your app on a Friday...and enjoy your weekend!
polpielladev
0
110
WebフロントエンドにおけるGraphQL(あるいはバックエンドのAPI)との向き合い方 / #241106_plk_frontend
izumin5210
4
1.4k
C++でシェーダを書く
fadis
6
4.1k
Featured
See All Featured
Site-Speed That Sticks
csswizardry
0
27
A Tale of Four Properties
chriscoyier
156
23k
Documentation Writing (for coders)
carmenintech
65
4.4k
Mobile First: as difficult as doing things right
swwweet
222
8.9k
GitHub's CSS Performance
jonrohan
1030
460k
Building an army of robots
kneath
302
43k
The Art of Programming - Codeland 2020
erikaheidi
52
13k
Into the Great Unknown - MozCon
thekraken
32
1.5k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
232
17k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
16
2.1k
For a Future-Friendly Web
brad_frost
175
9.4k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
169
50k
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