From 6bffe17cb037b5c102cfca1dfb75376625784743 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Tue, 9 Apr 2013 22:41:26 +0200 Subject: Scala : add private gcd in rational constructor --- Scala/sandbox/3-rationals.scala | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Scala/sandbox/3-rationals.scala b/Scala/sandbox/3-rationals.scala index 387e091..3eec82c 100644 --- a/Scala/sandbox/3-rationals.scala +++ b/Scala/sandbox/3-rationals.scala @@ -2,8 +2,10 @@ object Rationals { class Rational(x :Int, y: Int) { - def numer = x - def denom = y + private def gcd(a: Int, b: Int): Int = if (b == 0) a else gcd(b, a % b) + private val g = gcd(x, y) + def numer = x / g + def denom = y / g def neg = new Rational(-numer, denom) -- cgit v1.1-2-g2b99