Simply Scala

Implementation of programming language Scala

Simply Scala is browser-based implementation of Scala language. It was written in 2009 by A.J.Bagwell as an easy way to try Scala without having to install it. Server-side part of it is written in Scala, and client UI uses JavaScript.

Simply Scala works as an interactive interpreter: you enter the code in the input box, press “Evaluate” and watch the results of evaluation (output or messages about defined classes, modules etc.).

Examples:

Hello, World!:

Example for versions Simply Scala

In Simply Scala you can evaluate expressions without framing them as objects.

println("Hello, World!")

Factorial:

Example for versions Simply Scala

This examples shows how to define ! as a method applied to Int.

def factorial(n: Int): BigInt = 
    if (n == 0) 1 
           else factorial(n - 1) * n
class Factorizer(n: Int) {
    def ! = factorial(n)
}
implicit def int2fact(n: Int) = new Factorizer(n);	

for {i <- List.range(0, 17)} 
    println(i + "! = " + (i!))

Fibonacci numbers:

Example for versions Scala 2.7.7-final, Simply Scala

This example shows the usage of lazy evaluations and infinite lists in Scala. Infinite list of Fibonacci numbers is defined using functions .zip and .tail in the same way as in Haskell example.

lazy val fib: Stream[Int] = Stream.cons(1, Stream.cons(1, fib.zip(fib.tail).map(p => p._1 + p._2)))
fib.take(16).print

Quadratic equation:

Example for versions Simply Scala

This example shows that string processing and Math in Scala are implemented similarly to Java.

def output(real: Double, imag: Double): String = 
    if (imag == 0) ""+real
              else "("+real+","+imag+")"

def solve(A: Int, B: Int, C: Int)
{   if (A == 0) print("Not a quadratic equation.")
    else
    {   def D = B*B - 4*A*C
        if (D == 0) print("x = "+output(-B/2.0/A, 0))
        else if (D > 0) print("x1 = "+output((-B+Math.sqrt(D))/2.0/A, 0)+"\nx2 = "+output((-B-Math.sqrt(D))/2.0/A, 0))
             else print("x1 = "+output(-B/2/A, Math.sqrt(-D)/2.0/A)+"\nx2 = "+output(-B/2/A, -Math.sqrt(-D)/2.0/A))
    }
}