Aaqib Pervaiz MS(CS) FAST-NUCS Peshawar Introduction to
Birth of • Scala means Scalable Language (Grow with demand of User and Industry) • Martin Odersky worked on JAVA in late 1990s • He later on found that JAVA had various issues baked into it which were unlikely to change ever. • He found a better way to develop a language, which works like JAVA but in a much efficient way. • Building a new language from scratch needed a lot of time and effort so Martin build Scala on top of same principals of JAVA but with different nature of code and it’s implementation. • That’s why SCALA runs purely on JVM(Java Virtual Machine). Scala can import any Java libraries which are already written for Java. • Scala code compiles into byte code (just as java) so any machine which can be programmed with Java can be programmed with Scala.
What is 1) Scala is a Hybrid language. 2) Scala was first released in 2003 in attempt to replace JAVA and it has seen numerous upgrades between all these years. 3) Scala has been receiving a rapid adoption rate in last 3 years due to it’s new builds which make it robust and faster than JAVA and productive than many programing languages in the market.
Why 1) It’s a functional/Object Oriented/internet programing language at the same time. 2) Code writability of Scala is up to modern standards which means minimal code and more functionality. Scala even Kicks off the use of Semicolon in the end of each statement which are heavily used in C++ C# Java etc. 3) Scala Code Compiles faster than JAVA or C
How to Install and start coding 1) You can either install Scala package in system binaries and then compile code in cmd. The coding for Scala can be done in any coding program such Sublime or notepad++ etc. • LINK>> http://www.scala-lang.org/download/ 2) Or you can use an Eclipse IDE for Scala to program Scala code easier) • LINK>> http://scala-ide.org/ 3) Online Compiler at CodingGround • LINK>> https://www.tutorialspoint.com/compile_scala_online.php
Web API • Play Framework was developed for Scala and Java. Play has vast amount of Web API’s. Scala can exchange data code with JSON & XML API’s which JAVA can’t.
is a functional language in OOP Paradigm • In heart Scala is pure object oriented which means any thing in Scala is an object even the main() function. That's how it can do functional programing in a Object Oriented environment. We can even pass functions to other functions as objects. • But Scala is the only language which works both ways Object Oriented and Functional (the ability to do programing without considering state) • World is shifting towards Functional Programing and in coming years Scala will gain more popularity due to its unique nature.
is a functional language in OOP Paradigm
Hello World! • object Sacala { • def main(args: Array[String]) { • println (“Hello World”) • } Declaration of the Object and then Object name which will be our source code/file name Definition of the function main() and some arguments to be passed to it’s object Printing something as simple as one command and passing variables/constants/strings Note: There is no colon ; needed to end of the statement.
Hello World in Java public class HelloJava { public static void main(String[] args) { System.out.println("Hello World!"); } } Hello World in Scala object HelloScala { def main(args: Array[String]){ println("Hello World!") } }
Not Data but new Variable Type is what we use in Imperative programing for declaring variables for all data types. Var can be changed in run-time. • E.g. var a: Int = 30 • Var a=30 • is used for the variables which hold constant values. The values stored in Var cannot be changed during run-time. • E.g. val b: int=20 • Val b=20 This will decide the data type itself
Unary Increment / Decrement is gone • There is no such thing like Post-Pre Increment/Decrement Operators. • ++a or a++ • We have to overload the operator manually for doing the increments. • a+=1 or a=a+1
Importing Java Libraries into Scala • Imorting a library is simple! Just type import scala.NameOfYouLibrary._ • Import scala.math._ • Now we can use functions like abs(-4) cbrt(4) round(5.45) pow(2,2) sqrt(2) min(4,19) max (5,59) log(23343) toRadians(90) toDegres(1.34343) (random * (11-1) + 1).toInt
Operators • Conditional Operators: • Logical Operators: == != >= <= && || !
Returnary if statement • Returnary If statements in Scala are just like if statement in but they return a value in similar way a Java's retunrary operator does but in Scala it is smart and can decide what to return in one line. • Returnary If Condition: Int age=8 val canVote= if(age>=18) "yes" else "no"
if statement • The standart If statements in Scala is same as you will find in Java or C++ or any other language. • var age=18 • if((age>=5) && (age<=6)) • println("Go to Prep Class") • else if ((age>6) && (age<=7)) • println("Go to Class 1") • else • println("Go to Class " + (age - 5))
Arrays • Array in Scala is same as Java and C++ in functionality although the syntax is a little changed. Java Scala C++ int[] myIntArray = new int[3]; int[] myIntArray = {1,2,3}; var z:Array[String] = new Array[String](3) z(0) = “Emad"; z(1) = “Noyan"; z(2) = “Saif“ Or var z = Array(“Zubair", “Anwar", “Malik") int n[ 10 ] n={1,2,3,4,5,6,7,8,9,0}
While and DoWhile Loop • While Loop While(i<=10) println(i) i+=1 • Do While Loop Do Println(i) i+=1 While(i <= 20)
For Loop • For Loop is a kind of redesigned in Scala to make it more easy to write and more easy to read i=0 for (i<-1 to 10) println(i) Note: We don’t need to use Curly Braces for the body of loops in Scala { }
Lists • Lists in Scala are implemented in a very simple and neat way • val aList = List (1,2,3,4,5) • for( i <- aList) { • println("List items" + i) • } Retrieving values from list one by one using for loop Declaration of the List
Introduction to Scala language

Introduction to Scala language

  • 1.
  • 2.
    Birth of • Scalameans Scalable Language (Grow with demand of User and Industry) • Martin Odersky worked on JAVA in late 1990s • He later on found that JAVA had various issues baked into it which were unlikely to change ever. • He found a better way to develop a language, which works like JAVA but in a much efficient way. • Building a new language from scratch needed a lot of time and effort so Martin build Scala on top of same principals of JAVA but with different nature of code and it’s implementation. • That’s why SCALA runs purely on JVM(Java Virtual Machine). Scala can import any Java libraries which are already written for Java. • Scala code compiles into byte code (just as java) so any machine which can be programmed with Java can be programmed with Scala.
  • 3.
    What is 1) Scalais a Hybrid language. 2) Scala was first released in 2003 in attempt to replace JAVA and it has seen numerous upgrades between all these years. 3) Scala has been receiving a rapid adoption rate in last 3 years due to it’s new builds which make it robust and faster than JAVA and productive than many programing languages in the market.
  • 4.
    Why 1) It’s afunctional/Object Oriented/internet programing language at the same time. 2) Code writability of Scala is up to modern standards which means minimal code and more functionality. Scala even Kicks off the use of Semicolon in the end of each statement which are heavily used in C++ C# Java etc. 3) Scala Code Compiles faster than JAVA or C
  • 5.
    How to Installand start coding 1) You can either install Scala package in system binaries and then compile code in cmd. The coding for Scala can be done in any coding program such Sublime or notepad++ etc. • LINK>> http://www.scala-lang.org/download/ 2) Or you can use an Eclipse IDE for Scala to program Scala code easier) • LINK>> http://scala-ide.org/ 3) Online Compiler at CodingGround • LINK>> https://www.tutorialspoint.com/compile_scala_online.php
  • 6.
    Web API • PlayFramework was developed for Scala and Java. Play has vast amount of Web API’s. Scala can exchange data code with JSON & XML API’s which JAVA can’t.
  • 7.
    is a functionallanguage in OOP Paradigm • In heart Scala is pure object oriented which means any thing in Scala is an object even the main() function. That's how it can do functional programing in a Object Oriented environment. We can even pass functions to other functions as objects. • But Scala is the only language which works both ways Object Oriented and Functional (the ability to do programing without considering state) • World is shifting towards Functional Programing and in coming years Scala will gain more popularity due to its unique nature.
  • 8.
    is a functionallanguage in OOP Paradigm
  • 9.
    Hello World! • objectSacala { • def main(args: Array[String]) { • println (“Hello World”) • } Declaration of the Object and then Object name which will be our source code/file name Definition of the function main() and some arguments to be passed to it’s object Printing something as simple as one command and passing variables/constants/strings Note: There is no colon ; needed to end of the statement.
  • 10.
    Hello World inJava public class HelloJava { public static void main(String[] args) { System.out.println("Hello World!"); } } Hello World in Scala object HelloScala { def main(args: Array[String]){ println("Hello World!") } }
  • 11.
    Not Data butnew Variable Type is what we use in Imperative programing for declaring variables for all data types. Var can be changed in run-time. • E.g. var a: Int = 30 • Var a=30 • is used for the variables which hold constant values. The values stored in Var cannot be changed during run-time. • E.g. val b: int=20 • Val b=20 This will decide the data type itself
  • 12.
    Unary Increment /Decrement is gone • There is no such thing like Post-Pre Increment/Decrement Operators. • ++a or a++ • We have to overload the operator manually for doing the increments. • a+=1 or a=a+1
  • 13.
    Importing Java Librariesinto Scala • Imorting a library is simple! Just type import scala.NameOfYouLibrary._ • Import scala.math._ • Now we can use functions like abs(-4) cbrt(4) round(5.45) pow(2,2) sqrt(2) min(4,19) max (5,59) log(23343) toRadians(90) toDegres(1.34343) (random * (11-1) + 1).toInt
  • 14.
    Operators • Conditional Operators: •Logical Operators: == != >= <= && || !
  • 15.
    Returnary if statement •Returnary If statements in Scala are just like if statement in but they return a value in similar way a Java's retunrary operator does but in Scala it is smart and can decide what to return in one line. • Returnary If Condition: Int age=8 val canVote= if(age>=18) "yes" else "no"
  • 16.
    if statement • Thestandart If statements in Scala is same as you will find in Java or C++ or any other language. • var age=18 • if((age>=5) && (age<=6)) • println("Go to Prep Class") • else if ((age>6) && (age<=7)) • println("Go to Class 1") • else • println("Go to Class " + (age - 5))
  • 17.
    Arrays • Array inScala is same as Java and C++ in functionality although the syntax is a little changed. Java Scala C++ int[] myIntArray = new int[3]; int[] myIntArray = {1,2,3}; var z:Array[String] = new Array[String](3) z(0) = “Emad"; z(1) = “Noyan"; z(2) = “Saif“ Or var z = Array(“Zubair", “Anwar", “Malik") int n[ 10 ] n={1,2,3,4,5,6,7,8,9,0}
  • 18.
    While and DoWhileLoop • While Loop While(i<=10) println(i) i+=1 • Do While Loop Do Println(i) i+=1 While(i <= 20)
  • 19.
    For Loop • ForLoop is a kind of redesigned in Scala to make it more easy to write and more easy to read i=0 for (i<-1 to 10) println(i) Note: We don’t need to use Curly Braces for the body of loops in Scala { }
  • 20.
    Lists • Lists inScala are implemented in a very simple and neat way • val aList = List (1,2,3,4,5) • for( i <- aList) { • println("List items" + i) • } Retrieving values from list one by one using for loop Declaration of the List