Scala 2.8.0-final
Version of implementation Scala of programming language ScalaA version of Scala.
Examples:
Hello, World! - Scala (141):
object Main {
def main(args: Array[String]) {
println("Hello, World!")
}
}
Quadratic equation - Scala (230):
This example expands the interactive one with variables input.
import java.io.{BufferedReader, InputStreamReader}
object Main {
def main(args: Array[String]) {
var stdin = new BufferedReader(new InputStreamReader(System.in));
var A = augmentString(stdin.readLine()).toInt;
var B = augmentString(stdin.readLine()).toInt;
var C = augmentString(stdin.readLine()).toInt;
solve(A,B,C);
}
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));
}
}
}
CamelCase - Scala (294):
This example uses regular expressions twice. First one, words
, describes the words in the text; each match of it is replaced with itself, converted to lower case and capitalized. Second one, separators
, describes the spaces between words; all matches of it are replaced with empty string, i.e., just removed from the string.
import java.io.{BufferedReader, InputStreamReader}
import scala.util.matching.Regex
object Main {
def main(args: Array[String]) {
var stdin = new BufferedReader(new InputStreamReader(System.in));
var text = stdin.readLine();
val words = """([a-zA-Z]+)""".r
text = words.replaceAllIn(text, m => m.matched.toLowerCase.capitalize)
val separators = """([^a-zA-Z]+)""".r
text = separators.replaceAllIn(text, "");
println(text);
}
}
Comments
]]>blog comments powered by Disqus
]]>