Koichiro Oishi
August 10, 2017
710

# Kotlin/Swift 有理数クラスのすすめ

August 10, 2017

## Transcript

2. ### ࣗݾ঺հ େੴ ߂Ұ࿠ γεςϜɾΞφϥΠζגࣜձࣾ iOSΞϓϦ։ൃऀ ΞΧ΢ϯτؔ࿈ * @mego_ * koishi

2 Koichiro Oishi, 2017/08/08

7. ### ݩωλ ʮKotlin ελʔτϒοΫʯ • p.27ʙp.40 • ୈ3ষ KotlinΨΠυπΞʔ • KotlinͷεϚʔτͳݴޠ࢓༷͕೺ѲͰ͖Δ಺༰

7 Koichiro Oishi, 2017/08/08

2017/08/08
10. ### Ϋϥεͷఆٛ(Kotlin) ෼ࢠͱ෼฼ΛϓϩύςΟͱͯ࣋ͭ͠ΫϥεRational Λఆٛ͢Δ class Rational(val numerator: Int, val denominator: Int)

• numeratorͱdenominator͸͜Ε͚ͩͰϓϩύςΟ ʹͳΔɻ • ͪͳΈʹval͸ఆ਺ɺvar͸ม਺ 10 Koichiro Oishi, 2017/08/08
11. ### Ϋϥεͷఆٛ(Swift) struct Rational { let numerator: Int let denominator: Int

} let rational = Rational(numerator: 1, denominator: 2) • ϓϩύςΟΛఆٛ͢Ε͹ΠχγϟϥΠβͷҾ਺ͱ ͯͦ͠ͷ··࢖͑Δ • ͪͳΈʹlet͸ఆ਺ɺvar͸ม਺ 11 Koichiro Oishi, 2017/08/08
12. ### ϝιουͷఆٛ(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
13. ### ϝιουͷఆٛ(Swift) extension Rational: CustomStringConvertible { var description: String { return

"\(numerator)/\(denominator)" } } • CustomStringConvertibleϓϩτίϧʹ४ڌͨ͠ extensionͰdescriptionϓϩύςΟΛ࣮૷ 13 Koichiro Oishi, 2017/08/08
14. ### ΠχγϟϥΠβ(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
15. ### ΠχγϟϥΠβ(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
16. ### ΠχγϟϥΠβ(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
17. ### ඇެ։ϓϩύςΟϝιου(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
18. ### ඇެ։ϓϩύςΟϝιου(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
19. ### ඇެ։ϓϩύςΟϝιου(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
20. ### ԋࢉࢠΦʔόʔϩʔυ(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
21. ### ԋࢉࢠΦʔόʔϩʔυ(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
22. ### ԋࢉࢠΦʔόʔϩʔυ(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
23. ### ϝιουͷΦʔόʔϩʔυ(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
24. ### ϝιουͷΦʔόʔϩʔυ(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
25. ### ֦ுؔ਺(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
26. ### ֦ுؔ਺(Swift) • +ʹҾ਺ͱͯ͠ࠨลInt + ӈลRationalΛ࣮૷ static func +(left: Int, right:

Rational) -> Rational { return right + left } 26 Koichiro Oishi, 2017/08/08
27. ### ·ͱΊ ΍ͬͨ͜ͱ • Ϋϥεͷఆٛ • ϓϩύςΟɺϝιουɺΠχγϟϥΠβͷఏࣔ • ؔ਺Φʔόϩʔυ • طଘͷܕʹϝιουΛ௥Ճ(֦ுؔ਺)

ଞͷݴޠͰ΋༗ཧ਺Ϋϥεͷ࣮૷Λͯ͠ΈΔͱษڧʹͳ Γͦ͏Ͱ͢Ͷʁ 27 Koichiro Oishi, 2017/08/08
28. ### ͝঺հͨ͠Swiftͷ࣮૷ྫ͸ͪ͜Β • https:/ /gist.github.com/koishi -> SwiftRational.swift • https:/ /gist.github.com/koishi/ d54a38e3216aaaf87ac592465adb0695

ποίϛେ׻ܴͰ͢ 28 Koichiro Oishi, 2017/08/08