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
tidymodelsによるtidyな生存時間解析 / Japan.R2024
dropout009
1
750
事業成長を爆速で進めてきたプロダクトエンジニアたちの成功談・失敗談
nealle
3
1.4k
Fibonacci Function Gallery - Part 1
philipschwarz
PRO
0
210
From Translations to Multi Dimension Entities
alexanderschranz
2
130
CSC305 Lecture 26
javiergs
PRO
0
140
急成長期の品質とスピードを両立するフロントエンド技術基盤
soarteclab
0
920
103 Early Hints
sugi_0000
1
220
Cloudflare MCP ServerでClaude Desktop からWeb APIを構築
kutakutat
1
530
数十万行のプロジェクトを Scala 2から3に完全移行した
xuwei_k
0
260
なまけものオバケたち -PHP 8.4 に入った新機能の紹介-
tanakahisateru
1
120
range over funcの使い道と非同期N+1リゾルバーの夢 / about a range over func
mackee
0
110
今からはじめるAndroidアプリ開発 2024 / DevFest 2024
star_zero
0
1k
Featured
See All Featured
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.9k
A Modern Web Designer's Workflow
chriscoyier
693
190k
Site-Speed That Sticks
csswizardry
2
190
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
How GitHub (no longer) Works
holman
311
140k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
The Cost Of JavaScript in 2023
addyosmani
45
7k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
247
1.3M
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Designing Experiences People Love
moore
138
23k
Building an army of robots
kneath
302
44k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
6
510
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