Scastie now makes it even easier to save Scala snippets

Image for post
Image for post
Photo by Micheile Henderson on Unsplash
class NonEuclideanDomainException(exceptionMessage: String, a: Long, b: Long, eucFn: Long => Long) extends Exception(exceptionMessage: String) {
val causingA = a
val causingB = b
val causingFn = eucFn
}
def euclideanGCD(a: Long, b: Long, eucFn: Long => Long): Long = {
var currA = Math.abs(a)
var currB = Math.abs(b)
var currRemainder = 0L
if (eucFn(currA) < eucFn(currB)) {
currA = Math.abs(b)
currB = Math.abs(a)
}
while (eucFn(currB) != 0) {
currRemainder = currA % currB
if (eucFn(currA) < 0 || eucFn(currB) < 0 || eucFn(currRemainder) < 0) {
val excMsg = "The function " + eucFn.getClass.getName + " is not a valid Euclidean function because it sometimes returns negative values."
throw new IllegalArgumentException(excMsg)
}
if (eucFn(currRemainder) >= eucFn(currB)) {
val excMsg = "Z is not Euclidean for the function f = " + eucFn.getClass.getName + " since f(" + currRemainder + ") = " + eucFn(currRemainder) + " but f(" + currB + ") = " + eucFn(currB) + "."
throw new NonEuclideanDomainException(excMsg, a, b, eucFn)
}
currA = currB
currB = currRemainder
}
currA
}
def negCube(n: Long): Long = n * n * -ndef invalidFunctionF(n: Long): Long = 10Ldef square(n: Long): Long = n * neuclideanGCD(-27, 18, square)

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