object HelloWorld { def main(args: Array[String]): Unit = { val ages = Seq(42, 75, 29, 64) println(s"The oldest person is ${ages.max}") /* def func1 (x: Int) : Int = { if (x % 2 != 0) x + 1 else x * 2 } def func2 (fn1: Int => Int, b :Int) = fn1(b) println(func2(func1, 7)) println(func2(func1, 6)) val a: Int = 9 val b: Int = 4 assert(func2(func1,a) == a + 1) assert(func2(func1,b) == b * 1) */ /* def checkEvenOdd (j : List[Int]): Unit ={ println("Even list = " + j.filter(x => x % 2 == 0)) println("Odd list = " + j.filter(x => x % 2 != 0)) } val i = (1 to 10).toList checkEvenOdd(i) */ /* def checkEven(f: Int): Boolean = { if (f % 2 == 0) true else false } println(checkEven(5)) */ /* def modN(n: Int)(x: Int) = n % x def modFunction = modN(2)(_) val nums = List(1, 2, 3, 4) println( nums.map { x => modFunction(x)}) */ /* def multiply(x: Int)(implicit y: Int) = x * y implicit val y: Int = 10 println(multiply(2)) */ /* def error(msg: String): Unit = println(msg) implicit def intToString(i: Int): String = i.toString error(400) */ /* def add(a: Int)(b: Int) : Int = a + b println(add(1)_) */ /* def gcd(a: Int, b: Int): Int = { if (b == 0) a else gcd(b, a % b) } println(gcd(18, 27)) */ /* val list1 = List(1,2) val list2 = List(10,20) val newList = for (i <- list1; j <- list2) yield (i, j) println(newList) */ /* object CurryingExample extends App{ def concatenator(w1: String)(w2: String) = w1 + " " + w2 val heyWord = _concatenator("Currying") println(heyWord("Demo")) } */ /* case class Student(val name: String, val score: Int) val studentList = List(new Student("John", 9), new Student("Thomas", 5), new Student("Jennifer", 7), new Student("Lora", 8)) val result = for { s <- studentList n = s.name if (n startsWith "T") } yield n result.foreach(name => println(name)) */ /* def findEven(input1: Int) { val numberList :List[Int]=(1 to input1).flatMap((x:Int)=>List(x)).toList println (numberList) def findevent (x:Int):Option[Boolean] = { if (x%2==0) Some(true) else None } for (n <- numberList) { print(n) findevent(n) match { case Some(true) => println(" is even.") case None => println(" is odd.") } } } findEven(5) */ /* def implicitVariables(input1: Int) { implicit val input2 = 5 //val list1 = (1 to input1).toList //println(input2) //println(list1) def curfun(x: Int)(implicit y: Int) = { val list2 = (1 to y).toList println(list2) //for (x <- x) } //curfun(input1)(input2) curfun(input1) } implicitVariables(7) */ //def CurryFunction(input1: Int, input2: Int) { /* //Define the method factorial def factorial (n: Int) : Int = if (n == 0) 1 else (n * factorial(n-1)) // Define the add function here. def customAdd (x : Int) (y: Int) = { val x1 = factorial(x) val y1 = factorial(y) println(x) println(y) println(x1) println(y1) val z = x1 + y1 println(z) } //val customAdd = add(input1) //println(customAdd(4)(3)) customAdd(5)(4) */ /* val stringList :List[String] = List("Scala", "Ruby", "Python", "C#", "Java", "Groovy", "JavaScript", "PHP", "Haskell") for (y <- stringList) { //if (y.substring(1,2) == "P") println(y) println(y.substring(0,1)) } */ /* val pv = Vector(1,2,3,4,5,6,7,8,9).par println(pv) */ /* val f = List.fill(5) ("TCS") println(f) */ //@tailrec //def factorial(n: Int): Int = if (n == 0) 1 else n * factorial(n – 1) /*def concatenator(w1: String)(w2: String) = w1 + " " + w2 val heyWord = concatenator("Currying")_ println(heyWord("Demo")) */ /* val capitals = Map("France" -> "Paris", "Japan" -> "Tokyo") println("India: " + show(capitals.get( "India")) ) println("Japan: " + show(capitals.get( "Japan")) ) def show(x: Option[String]) = x match { case Some(s) => s case None => "?" } */ /* val list = List(1,2,3,4,5) val newlist = for (i <- list) yield i*i println (newlist) */ /* case class Student(val name: String, val score: Int) val studentList = List(new Student("John", 9), new Student("Thomas", 5), new Student("Jennifer", 7), new Student("Lora", 8)) val topScorers = for (student <- studentList if (student.score >8)) yield student.name topScorers.foreach(name => println(name)) */ /* val list = List(1, 2, 3, 4, 5) val j = for (i <- list) yield i+10 j.foreach(println) val k = for (i <- list if i % 2 != 0) yield i k.foreach(println) println(k) */ /* case class Language(name: String, article: Int) val LanguageBase = List(Language("Scala", 26), Language("Csharp", 32), Language("Perl", 42), Language("Java", 22)) val MoreThanTwenty = for (language <- LanguageBase if (language.article >=20 && language.article < 30)) yield language.name // MoreThanTwenty.foreach(name => println(name)) MoreThanTwenty.foreach(println) */ /* println("Hello, World!") def prnt = {print("scala"); 1} def fun(a:Int,b: => Int) = print(a) println(fun(prnt,5)) val s = List (10, 11, 12) val result = s.flatMap(i => List(i-1, i, i +1)) println(result) val salary = 3000.00 */ /* val list1 = (1 to 10).toList println (list1) val numberList = (1 to 4).toList val listGenerator = numberList.flatMap(x => List.range(1, x+1)) println(listGenerator) */ /* val i = 10.0 val l = List(1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 5, 5, 8) println(l.drop(10)) val s = 20 println(s"value of s is $s") val pl = List(1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 5, 5, 8) println(pl.take(3)) val n = List(1,2,3,4,5) println(n.map(_ % 2)) */ } }
Write, Run & Share Scala code online using OneCompiler's Scala online compiler for free. It's one of the robust, feature-rich online compilers for Scala language, running on the latest version 2.13.8. Getting started with the OneCompiler's Scala compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as Scala
and start coding.
OneCompiler's Scala online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample Scala program which takes name as input and prints hello message with your name.
object Hello {
def main(args: Array[String]): Unit = {
val name = scala.io.StdIn.readLine() // Read input from STDIN
println("Hello " + name )
}
}
Scala is both object-oriented and functional programming language by Martin Odersky in the year 2003.
Variable is a name given to the storage area in order to identify them in our programs.
var or val Variable-name [: Data-Type] = [Initial Value];
If, If-else, Nested-Ifs are used when you want to perform a certain set of operations based on conditional expressions.
if(conditional-expression){
//code
}
if(conditional-expression) {
//code if condition is true
} else {
//code if condition is false
}
if(condition-expression1) {
//code if above condition is true
} else if (condition-expression2) {
//code if above condition is true
}
else if(condition-expression3) {
//code if above condition is true
}
...
else {
//code if all the above conditions are false
}
For loop is used to iterate a set of statements based on a criteria.
for(index <- range){
// code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while(condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition)
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity.
def functionname(parameters : parameters-type) : returntype = { //code
}
You can either use =
or not in the function definition. If =
is not present, function will not return any value.