Implicit conversions in Scala help with DRY

Image for post
Image for post
Photo by Chinh Le Duc on Unsplash
CopyOnWriteArrayList<File> recentFiles = new
CopyOnWriteArrayList<File>();
    public Fraction plus(Fraction addend) {
long interNumerA = this.fractNumer * addend.fractDenom;
long interNumerB = addend.fractNumer * this.fractDenom;
long newNumer = interNumerA + interNumerB;
long newDenom = this.fractDenom * addend.fractDenom;
return new Fraction(newNumer, newDenom);
}
public Fraction plus(int addend) {
Fraction tempAddend = new Fraction(addend, 1);
return this.plus(tempAddend);
}
package fractionsobject Fraction {  import scala.language.implicitConversions  implicit def IntToFraction(n: Int) = {
// This next line is for educational purposes only
System.out.println("Implicit conversion invoked")
// This next line does the actual conversion
new Fraction(n, 1)
}
}class Fraction(numerator: Long, denominator: Long = 1L) {
// TODO: Throw exception if denominator is 0
// TODO: Make sure denominator is positive
// TODO: Put fraction in lowest terms with gcdNumDen =
gcd(numerator, denominator)
val fractNumer = numerator / gcdNumDen
val fractDenom = denominator / gcdNumDen
// TODO: Override toString def +(addend: Fraction): Fraction = {
val interNumerA = this.fractNumer * addend.fractDenom
val interNumerB = addend.fractNumer * this.fractDenom
val newNumer = interNumerA + interNumerB
val newDenom = this.fractDenom * addend.fractDenom
new Fraction(newNumer, newDenom)
}
def unary_- = new Fraction(-this.fractNumer, this.fractDenom) def -(subtrahend: Fraction): Fraction = {
this + (-subtrahend)
}
def *(multiplicand: Fraction): Fraction = {
val newNumer = this.fractNumer * multiplicand.fractNumer
val newDenom = this.fractDenom * multiplicand.fractDenom
new Fraction(newNumer, newDenom)
}
def /(divisor: Fraction): Fraction = {
if (divisor.fractNumer == 0) {
val exceptionMessage = "Dividing " + this.toString
+ " by 0 results in an indeterminate number."
throw new IllegalArgumentException(exceptionMessage)
}
val newNumer = this.fractNumer * divisor.fractDenom
val newDenom = this.fractDenom * divisor.fractNumer
new Fraction(newNumer, newDenom);
}
def reciprocal: Fraction = {
if (this.fractNumer == 0) {
val exceptionMessage = "Reciprocal of 0 is indeterminate"
throw new IllegalArgumentException(exceptionMessage)
}
new Fraction(this.fractDenom, this.fractNumer)
}
}
scala> var numberA = true.hashCode
numberA: Int = 1231
scala> var numberB = new fractions.Fraction(7, 12)
numberB: fractions.Fraction = 7/12
scala> numberA + numberB
Implicit conversion invoked
res0: fractions.Fraction = 14779/12
scala> numberA - numberB
Implicit conversion invoked
res1: fractions.Fraction = 14765/12
var numberA = true.hashCode
var numberB = new Fraction(7, 12)
numberA + numberB
numberA - numberB
numberA + numberB  14779/12: Playground$Fraction
numberA - numberB 14765/12: Playground$Fraction
  def *(multiplicand: Int): Fraction = new Fraction(this.fractNumer
* multiplicand, this.fractDenom)
scala> numberA * numberB
Implicit conversion invoked
res2: fractions.Fraction = 8617/12
scala> numberB * numberA
res3: fractions.Fraction = 8617/12
scala> numberA / numberB
Implicit conversion invoked
res4: fractions.Fraction = 14772/7
scala> numberB / 0
Implicit conversion invoked
java.lang.IllegalArgumentException: Dividing 7/12 by 0 results in an indeterminate number.
at fractions.Fraction.$div(Fraction.scala:62)
... 28 elided
scala> numberB.reciprocal
res7: fractions.Fraction = 12/7
scala> numberA.reciprocal
<console>:13: error: value reciprocal is not a member of Int
numberA.reciprocal
^
class Fraction(numerator: Long, denominator: Long = 1L) {
...
}
scala> var numberC = new fractions.Fraction(47)
numberC: fractions.Fraction = 47
    public Fraction(long numerator) {
this(numerator, 1);
}

is a composer and photographer from Detroit, Michigan. He has been working on a Java program to display certain mathematical diagrams.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store