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.3k
Kotlin/Swift 有理数クラスのすすめ
Koichiro Oishi
August 10, 2017
Tweet
Share
More Decks by Koichiro Oishi
See All by Koichiro Oishi
気がついたら子供が社会人になって 自分と同じモバイルアプリエンジニアになった件 / Parent-Child Engineers
koishi
0
88
アプリのユニットテストについて初歩から考えてみる/ABC UnitTest
koishi
3
820
iOSアプリ開発者から見たKotlin
koishi
2
700
5分でわかるPassword_Autofill_for_Apps
koishi
0
1k
Other Decks in Programming
See All in Programming
CSS Linter による Baseline サポートの仕組み
ryo_manba
1
160
バイセルでの AI を用いた開発の取り組み ~ Devin, Cursor の活用事例・知見共有 ~
umaidashi
0
110
メンテが命: PHPフレームワークのコンテナ化とアップグレード戦略
shunta27
0
330
PHPのバージョンアップ時にも役立ったAST
matsuo_atsushi
0
230
5分で理解する SOLID 原則 #phpcon_nagoya
shogogg
1
420
Jasprが凄い話
hyshu
0
190
バッチを作らなきゃとなったときに考えること
irof
2
560
「個人開発マネタイズ大全」が教えてくれたこと
bani24884
1
310
Learning Kotlin with detekt
inouehi
1
220
AIプログラミング雑キャッチアップ
yuheinakasaka
20
5.4k
はじめてのIssueOps - GitHub Actionsで実現するコメント駆動オペレーション
tmknom
4
760
DRFを少しずつ オニオンアーキテクチャに寄せていく DjangoCongress JP 2025
nealle
2
300
Featured
See All Featured
It's Worth the Effort
3n
184
28k
Bootstrapping a Software Product
garrettdimon
PRO
307
110k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
32
2.2k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
30
2.3k
The Language of Interfaces
destraynor
156
24k
Producing Creativity
orderedlist
PRO
344
40k
The World Runs on Bad Software
bkeepers
PRO
67
11k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
366
25k
A Modern Web Designer's Workflow
chriscoyier
693
190k
Being A Developer After 40
akosma
89
590k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.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