Professional Documents
Culture Documents
AMRITPAL SINGH
▪ If you ask any industry expert, what language should
you learn for big data, they would definitely suggest
you to start with Scala.
Scala Example: ▪ main method which should serve as the entry point for
an application.
Hello Scala
▪ In Java args contains the supplied command-line
arguments as an array of String objects.
▪ }
Hello Scala
▪ scalaExample // Calling of function
▪ object MainObject {
▪ def main(args: Array[String]) {
▪ var a = 1
▪ a match{
▪ }
▪ }
▪ Here, match using a variable named a.
Scala Pattern ▪ This variable matches with best available case and
prints output.
Matching
▪ Underscore (_) is used in the last case for making it
default case.
▪ while(boolean expression){
▪ // Statements to be executed
While loop ▪ }
▪ object MainObject {
▪ def main(args: Array[String]) {
▪ }
▪ for( i <- range){
▪ }
▪ object MainObject {
▪ def main(args: Array[String]) {
▪ for( a <- 1 to 10 ){
▪ }
▪ }
▪ object MainObject {
▪ def main(args: Array[String]) {
▪ }
▪ }
▪ object MainObject {
▪ def main(args: Array[String]) {
▪ }
▪ }
Scala for-loop in ▪ In scala, you can iterate collections like list, sequence
etc, either by using for each loop or for-
Collection comprehensions.
▪ object MainObject {
▪ def main(args: Array[String]) {
Iterating Collection ▪ }
▪
▪ }
▪ }
▪ object MainObject {
▪ def main(args: Array[String]) {
▪ for(i<-1 to 10 by 2){
Scala for-loop
▪ println(i)
Example using by
▪ }
keyword
▪ }
▪ }
▪ object MainObject {
▪ def main(args: Array[String]) {
▪ functionExample(10,20)
▪ }
▪ def functionExample(a:Int, b:Int) = {
Functions ▪ var c = a+b
▪ println(c)
▪ }
▪ }
▪ Lists are one of the most versatile data structure in
Scala.
Arrays in Scala ▪ You can access the element of an array by index. Lets
access the first element of array “name”. By giving
index 0. Index in Scala starts from 0.
▪ The biggest difference is in the idea of direct access Vs
sequential access.
Arrays in Scala
▪ Arrays allow both; direct and sequential access, while
lists allow only sequential access.
▪ At first glance, a Scala String appears to be just a Java
String. For instance, when you work in the Scala Read-
Evaluate-Print-Loop (REPL) environment and print the
Strings name of a String literal, the REPL feedback tells you the
type is java.lang.String.
▪ "Hello, world".getClass.getName
res0: String = java.lang.String
Strings ▪ Indeed, a Scala String is a Java String, so you can use all
the normal Java string methods.
Strings
▪ You can concatenate strings:
▪ val s ="Hello" + " world“
▪ scala> "scala".drop(2).take(2).capitalize
Strings
▪ Testing String Equality
▪ String"""
▪ Splitting Strings
Strings :
String
interpolation
▪ scala> println(s"Age next year: ${age + 1}")
Age next year: 34
Strings : String
interpolation ▪ scala> println(s"You are 20 years old: ${age == 33}")
▪ You are 20 years old: false
Strings : String
interpolation
Strings
▪ Processing a String One Character at a Time
Strings
▪ How to extract parts of a string that match a regex ?
Input from User ▪ def readLine(text: String, args: Any*): String Prints
formatted text to stdout and reads a full line from stdin .
▪ It's like the for loop has a buffer you can’t see, and for
Scala for/yield each iteration of your for loop another item is added to
that buffer.