Scala match tuples helps cut down on unwieldy nested ifs

Image for post
Image for post
Photo by Dean Brierley on Unsplash
  if (myVar == constA) {
actionA();
} else if (myVar == constB) {
actionB();
} else if (myVar == constC) {
actionC();
} else if ...
// and so on and so forth
}
  switch (myVar) {
case constA: actionA();
break;
case constB: actionB();
break;
case constC: actionC();
break;
case ... // and so on and so forth
break;
default: throw someError;
}
  myVar match {
case constA => actionA
case constB => actionB
case constC => actionC
... // etc.
case _ => throw new Exception("Unexpected case")
}
def fizzBuzz(n: Int): Any = (n % 15) match {
case 0 => "FizzBuzz"
case 3 | 6 | 9 | 12 => "Fizz"
case 5 | 10 => "Buzz"
case _ => n
}
(1 to 100).map(fizzBuzz(_))
def fizzBuzz(n: Int): Any = (n % 3, n % 5) match {
case (0, 0) => "FizzBuzz"
case (0, _) => "Fizz"
case (_, 0) => "Buzz"
case _ => n
}
(1 to 100).map(fizzBuzz(_))
  @throws(classOf[NotDivisibleException])
def /(divisor: QuadInt): QuadInt =
if (this.ring == divisor.ring) {
if (divisor.norm == 0) {
throw new IllegalArgumentException("Division by zero")
}
val dividendRegFract = new Fraction(this.regPart,
this.denominator)
val dividendSurdFract = new Fraction(this.surdPart,
this.denominator)
val divisorRegFract = new Fraction(divisor.regPart,
divisor.denominator)
val divisorSurdFract = new Fraction(divisor.surdPart,
divisor.denominator)
val quotientRegFract = (dividendRegFract * divisorRegFract
- this.ring.radicand * dividendSurdFract
* divisorSurdFract)/divisor.norm
val quotientSurdFract = (dividendSurdFract * divisorRegFract
- dividendRegFract * divisorSurdFract)/divisor.norm
val notDivisibleFlag = (this.ring.hasHalfIntegers,
quotientRegFract.denominator,
quotientSurdFract.denominator) match {
case (_, 1, 1) => false
case (true, 2, 2) => false
case _ => true
}

if (notDivisibleFlag) {
throw new NotDivisibleException("Not divisible",
this, divisor, fractArray, this.ring)
}
QuadInt(quotientRegFract.numerator.toInt,
quotientSurdFract.numerator.toInt, this.ring,
quotientRegFract.denominator.toInt)
} else {
throw new AlgebraicDegreeOverflowException
}
scala> val ring = new algebraics.quadratics.RealQuadRing(2)
ring: algebraics.quadratics.RealQuadRing = Z[sqrt(2)]
scala> val ramifier = new algebraics.quadratics.RealQuadInt(0, 1, ring)
ramifier: algebraics.quadratics.RealQuadInt = sqrt(2)
scala> var numberA = new algebraics.quadratics.RealQuadInt(7, 0, ring)
numberA: algebraics.quadratics.RealQuadInt = 7
scala> var numberB = new algebraics.quadratics.RealQuadInt(3, 1, ring)
numberB: algebraics.quadratics.RealQuadInt = 3 + sqrt(2)
scala> numberA / numberB
res5: algebraics.quadratics.QuadInt = 3 - sqrt(2)
scala> numberA / ramifier
algebraics.NotDivisibleException: 7 divided by sqrt(2) is 0 + 7/2 * sqrt(2), which is not an algebraic integer
at algebraics.quadratics.QuadInt.$div(QuadInt.scala:193)
... 28 elided
scala>

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