0% found this document useful (0 votes)
104 views

Scala

Scala is a modern multi-paradigm programming language designed for functional programming and to express common programming patterns in a concise, elegant, and type-safe way. Scala runs on the Java Virtual Machine and interoperates seamlessly with existing Java code and libraries. It supports object-oriented and functional programming and provides features such as type safety, immutability, closures, and pattern matching.

Uploaded by

babjeereddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
104 views

Scala

Scala is a modern multi-paradigm programming language designed for functional programming and to express common programming patterns in a concise, elegant, and type-safe way. Scala runs on the Java Virtual Machine and interoperates seamlessly with existing Java code and libraries. It supports object-oriented and functional programming and provides features such as type safety, immutability, closures, and pattern matching.

Uploaded by

babjeereddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 11

scala is an acronym for scalable language

scala is a modern multi paradigm programming language designed to express common


programming patterns in a concise, elegant and type safe way

scala is written by Martin Odersky EPFL

Scala is typed
A programming language is said to use static typing when type checking is performed
during compile-time as opposed to run-time
Runs on JVM, full inter-op with java
object Oriented
Functional
Use existing Java libraries
Use existing java tools (Ant,Maven,JUnit,etc..)
Decent IDE Support(NetBeans,IntelliJ,Eclipse)

scala > 2+3

res0: Int = 5

scala> res0*2
res1: Int =10

scala > val msg = "Hellow World"


msg: String = Hellow World

scala> val msg1: java.lang.String ="Hellow World"


msg1: String = Hellow World

scala> val msg2: String ="Hellow World"


msg2: String = Hellow World

scala> println(msg)
Hellow World

Byte 8-bit signed 2�s complement integer. It has minimum value of �128 and a
maximum value of 127 (inclusive).
Short 16-bit signed 2�s complement integer. It has a minimum value of �32,768 and
maximum of 32,767 (inclusive).
Int 32-bit signed 2�s complement integer. It has a minimum value of �2,147,483,648
and a maximum value of 2,147,483,647
Long 64-bit signed 2�s complement integer. It has a minimum value of
-9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807
(inclusive).
Float A single-precision 32-bit IEEE 754 floating point.
Double A double-precision 64-bit IEEE 754 floating point.
Boolean Two possible values: true and false.
Char A single 16-bit Unicode character. I

scala> msg ="Good By guys"


<console>:12: error: reassignment to val
msg ="Good By guys"
^
scala> var greeting = "Hello, world!"
greeting: String = Hello, world!
scala> greeting = "Leave me alone, world!"

scala> val multiLine =

If you realize you have typed something wrong, but the interpreter is still
waiting for more input, you can escape by pressing enter twice:

scala> val oops =


|
|"This is the next line."

scala> def max(x: Int, y: Int): Int = {


if (x > y) x
else y
}

scala> def max(x: Int, y: Int) = {


if (x > y) x
else y
}

scala> def max2(x: Int, y: Int) = if (x > y) x else y

scala> def greet() = println("Hello, world!")

scala> :quit

Scala Scripts

println("hellow " +args(0)+"!" )

scala hellow.scala babjee

firstscript.scala

args.foreach(println)

scala firstscript.scala my first script

Array

val array = new Array[String](3)

scala> array(0) = "This"


scala> array(1) = "is"
scala> array(2) = "mutable"

scala> array

The Array is a mutable data structure;

res37: Array[String] = Array(This, is, mutable)


scala> array.foreach(println)
This
is
mutable

scala> val array =Array("hai","how","are","you')

scala > array.foreach(println)t

List List is immutable

scala>val fruit: List[String] = List("apples", "oranges", "pears")

scala>val nums: List[Int] = List(1, 2, 3, 4)

scala > val myList = List("This", "is", "immutable")


scala > myList(0)
scala> myList.foreach(println)

scala> val oldList = List(1, 2)

scala> val newList = 3 :: oldList

scala> val newList = oldList :+ 3

// Two dimensional list


scala > val dim: List[List[Int]] =
List(
List(1, 0, 0),
List(0, 1, 0),
List(0, 0, 1)
)

scala> dim.foreach(_.foreach(println))

All lists can be defined using two fundamental building blocks, a tail Nil and ::,
which is pronounced cons.

val fruit = "apples" :: ("oranges" :: ("pears" :: Nil))

list methods

head :This method returns the first element of a list.

tail:This method returns a list consisting of all elements except the first.

isEmpty:This method returns true if the list is empty otherwise false.

scala> fruit.head
scala>fruit.tail
scala>fruit.isEmpty
scala> val fruit = List.fill(3)("apples") // Repeats apples three times.

scala> val num = List.fill(10)(2) // Repeats 2, 10 times.

// Creates 5 elements using the given function.


scala> val squares = List.tabulate(6)(n => n * n)

scala> val mul = List.tabulate( 4,5 )( _ * _ )


Reverse the list

scala> val fruit =List("apple","mango","pinaple","gova")


scala> fruit.reverse

set

a Set is a collection that contains no duplicate elements. There are two kinds of
Sets, the immutable and the mutable.
Set is not an ordered collection - you can't get element by index.

scala > var s : Set[Int] = Set(1,3,5,7)

scala> var s=set(1,2,3,4)

methods
head
tail
isEmpty
concatination set

scala> val fruit1 = Set("apples", "oranges", "pears")


scala> val fruit2 = Set("mangoes", "banana")

// use two or more sets with ++ as operator


scala> var fruit = fruit1 ++ fruit2
scala> println( "fruit1 ++ fruit2 : " + fruit )

min and max in set

scalas> val num = Set(5,6,9,20,30,45)

// find min and max of the elements

scalas> println( "Min element in Set(5,6,9,20,30,45) : " + num.min )

scalas> println( "Max element in Set(5,6,9,20,30,45) : " + num.max )

set.intersect method

scala> al num1 = Set(5,6,9,20,30,45)


scala> val num2 = Set(50,60,9,20,35,55)

// find common elements between two sets


scala> println( "num1.intersect(num2) : " + num1.intersect(num2) )
Map
Scala map is a collection of key/value pairs. Any value can be retrieved based on
its key.

ifelse

object IfElse {
def main(args:Array[String]){
val x:Int =20
var res =""
if (x ==20)
{
res="x==20"
} else{
res="x !=20"
}
println(res)
}
}

object IfElse {
def main(args:Array[String]){
val x =20
var res= ""
if (x ==20)
{
res="x==20"
} else{
res="x!=20"
}
println(res)
}
}

object IfElse {
def main(args:Array[String]){
val x =20
val y=30
var res= ""
if (x >y) {
res="x > 5"
}
else if(x < y) {
res="x <y"
} else {
res ="x ==y"
}
println(res)
}
}

object IfElse {
def main(args:Array[String]){
val x =20
val y=30
var res= ""
if (x >y) res="x > 5"
else if(x < y) res="x <y"
else res ="x ==y"
println(res)
}
}

object IfElse {
def main(args:Array[String]){
val x =30
val y=30
var res= ""
if (x ==20 && y==30 ) res="x ==20 && y==30"

else res ="x !=20 && y !=30"


println(res)
}
}

Loops
while loop

object IfElse {
def main(args:Array[String]){
var x =0
while (x <10){
println("x= "+x)
x+=1
}

}
}

object IfElse {
def main(args:Array[String]){
var x =10
while (x >0){
println("x= "+x)
x-=1
}
}
}

ForLoop

object ForLoop {
def main(args:Array[String]){
for(x <- 1 to 10){
println("x="+x)
}

object ForLoop {
def main(args:Array[String]){
for(x <- 1 until 10){
println("x="+x)
}

object ForLoop {
def main(args:Array[String]){
for(x <- 1 to 3 ; y <- 1 to 3 ){
println("value of x "+x)
println("value of y "+y)

object ForLoop {
def main(args:Array[String]){
for(x <- 1 to 3 ){

for (y <- 1 to 3){


print( y *x )
print(" ")
}
println()
}

object ForLoop {
def main(args:Array[String]){
for(x <- 1 to 3 ){

for (y <- 1 to x){


print( "*" )
print(" ")
}
println()

loop with collection

object ForLoop {
def main(args:Array[String]){

val numlist =List(1,2,3,4,5,6,7)

for(x <- numlist ){

println(x)

}
}

loop with filter

object ForLoop {
def main(args:Array[String]){

val numlist =List(1,2,3,4,5,6,7)

for(x <- numlist if x !=3 ; if x <6){

println(x)

}
}
import scala.util.control._

object ForLoop {
def main(args:Array[String]){

val loop = new Breaks

loop.breakable{
for (x <- 1 to 10){

println(x)
if (x==5){
loop.break
}
}

continue example

object ForLoop {
def main(args:Array[String]){

val loop = new Breaks;

val searchMe = "peter piper picked a peck of pickled peppers"


var numPs = 0
for (i <- 0 until searchMe.length) {
loop.breakable {
if (searchMe.charAt(i) != 'p') {
Breaks // break out of the 'breakable', continue the outside loop
} else {
numPs += 1
}
}
}
println(numPs)
}

case statement

object demo {
def main(args:Array[String]){
val i =4
var month=""
i match {
case 1 => month="January"
case 2 => month="February"
case 3 => month="March"
case 4 => month="April"
case 5 => month="May"
case 6 => month="June"
case 7 => month="July"
case 8 => month="August"
case 9 => month="September"
case 10 => month="October"
case 11 => month="November"
case 12 => month="December"
case _ => month="Invalid month" // the default, catch-all
}

println(month)
}

object demo {
def main(args:Array[String]){
val i =2

val month = i match {


case 1 => "January"
case 2 => "February"
case 3 => "March"
case 4 => "April"
case 5 => "May"
case 6 => "June"
case 7 => "July"
case 8 => "August"
case 9 => "September"
case 10 => "October"
case 11 => "November"
case 12 => "December"
case _ => "Invalid month" // the default, catch-all
}

println(month)
}

}
import scala.util.Random

object demo{
def main(args:Array[String]){
val x: Int = Random.nextInt(10)

val res =x match {


case 0 => "zero"
case 1 => "one"
case 2 => "two"
case _ => "many"
}
println(res)
}

You might also like