Scastie provides an easy start for learning Scala

Image for post
Image for post
Photo by Lucas Davies on Unsplash
List("Hello", "World").mkString("", ", ", "!")
Hello, World!: java.lang.String
System.out.println("Hello, world!")
Hello, world!
  def euclideanGCD(a: Long, b: Long): Long = {
var currA, currB, currRemainder = 0L
if (Math.abs(a) < Math.abs(b)) {
currA = Math.abs(b)
currB = Math.abs(a)
} else {
currA = Math.abs(a)
currB = Math.abs(b)
}
while (currB != 0) {
currRemainder = currA % currB
currA = currB
currB = currRemainder
}
currA
}
(1 to 100).map(euclideanGCD(_, 12))
  (1 to 100).map(euclideanGCD(_, 12))
1.to(100)
1.to(100).length
  (1 to 100).map(euclideanGCD(_, 12))
Vector(1, 2, 3, 4, 1, 6, 1, 4, 3, 2, 1, 12, 1, 2, 3, 4, 1, 6, 1, 4, 3, 2, 1, 12, 1, 2, 3, 4, 1, 6, 1, 4, 3, 2, 1, 12, 1, 2, 3, 4, 1, 6, 1, 4, 3, 2, 1, 12, 1, 2, 3, 4, 1, 6, 1, 4, 3, 2, 1, 12, 1, 2, 3, 4, 1, 6, 1, 4, 3, 2, 1, 12, 1, 2, 3, 4, 1, 6, 1, 4, 3, 2, 1, 12, 1, 2, 3, 4, 1, 6, 1, 4, 3, 2, 1, 12, 1, 2, 3, 4): scala.collection.immutable.IndexedSeq
(1 to 100).filter(euclideanGCD(_, 12) == 1)
Vector(1, 5, 7, 11, 13, 17, 19, 23, 25, 29, 31, 35, 37, 41, 43, 47, 49, 53, 55, 59, 61, 65, 67, 71, 73, 77, 79, 83, 85, 89, 91, 95, 97): scala.collection.immutable.IndexedSeq
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 * -neuclideanGCD(-27, 18, negCube)
scastie: Sending task to the server.
scastie: Connected. Waiting for sbt
sbt: [info] Compiling 1 Scala source to /tmp/scastie8145071749968837443/target/scala-2.12/classes...
sbt: [info] Running Main
sbt: [error] (run-main-27) java.lang.ExceptionInInitializerError
...omitting a few lines...
at java.lang.reflect.Method.invoke(Method.java:498)
Caused by: java.lang.IllegalArgumentException: The function Playground$$Lambda$4732/440283381 is not a valid Euclidean function because it sometimes returns negative values.
at Playground.euclideanGCD(main.scala:28)

at Playground.<init>(main.scala:51)
at Main$.<init>(main.scala:54)
at Main$.<clinit>(main.scala)
at Main.main(main.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
...omitting a few more lines...
[trace] Stack trace suppressed: run 'last compile:run' for the full output.
sbt: [error] (compile:run) Nonzero exit code: 1
scastie: Closed.
  def invalidFunctionF(n: Long): Long = 10L
  euclideanGCD(-27, 18, invalidFunctionF)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
Caused by: Playground$NonEuclideanDomainException: Z is not Euclidean for the function f = Playground$$Lambda$8507/923259725 since f(9) = 10 but f(18) = 10.
at Playground.euclideanGCD(main.scala:35)
at Playground.<init>(main.scala:55)
at Main$.<init>(main.scala:58)
at Main$.<clinit>(main.scala)
at Main.main(main.scala)
  def square(n: Long): Long = n * n
  euclideanGCD(-27, 18, square)
9: Long

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