2. What is Scala?
• Scala(ska-lah) stands for Scalable Language
• Combines functional programming with
Object Oriented Programming
• Growing as per the demand of the users
• Write small scripts as well as build larger
systems
2
3. What is Scala? Contd.
• Runs on the java platform(Java Virtual Machine)
as well as Dot net platform
• Designed by Martin Odersky in 2001 at EPFL,
who was also one of the creators of javac
compiler and generic java
• Open source
• Released in 2004
• Second version v 2.0 in 2006
• Current version 2.11.5
3
4. What makes Scala scalable?
• Combining OOP with functional programming
4
5. Why OOP?
• Purely OO
• Every operation is method
• Every value is an object
• No primitive types, everything is an object
• 1+2 invokes a method + defined in the class
Int
5
6. What is Functional Programming(FP)?
• “In computer science, functional programming is a programming
paradigm, a style of building the structure and elements of computer
programs, that treats computation as the evaluation of mathematical
functions and avoids changing-state and mutable data.”—Wikipedia
• Basic fundamental is to evaluate an expression and use its result for some
other expression
• Made up of function that always return a value
• No side effects state does not change
• Immutable data: cannot change the value once set;no need to lock the
access of shared data
• Reuse same code in different parts of program
• Free order of code execution
6
7. What is FP in Scala like?
• Supports mutable and immutable data
• Consists of variables and values
• Pass functions as arguments
• Return functions as results from functions
• Define functions inside other function
• Store functions in variables
• Functions are fist class values just like integer
or string
7
8. Syntax
• No semicolons
• No datatype specification required explicitly,
compiler will determine from context
• Variable definitions start with “var” and
definitions of values (i.e. read only variables)
start with “val”. Val is immutable where var is
mutable
8
9. Comparison with Java
• Compatible with existing java programs and
we can use all java libraries
• Operates similar to Java. Its compiler
generates byte code that is similar to java
bytecode
• Runs on Eclipse,IntelliJ and Netbeans just as
Java
• Only two things in Scala that are absent in
Java are richer type systems and Functional
programming
9
12. Why Scala?
• Since Scala compiles to Java bytecode and runs on the
JVM, programs written in Scala can benefit from the huge
amount of library code already written in Java. However, by
using Scala you get the following benefits:
– Mandatory boilerplate code is gone – no getters and setters, no
checked exceptions.
– More powerful constructs, that allow you to do more with less
code, such as case classes, option and tuples.
– More powerful code reuse – the elements of code that you can
reuse are smaller. Rather than classes with single inheritance,
you have traits and functions.
http://hedleyproctor.com/2012/04/why-java-developers-should-be-
learning-scala/
12
13. A typical Java class vs. Scala class
example
• Java class
Class Person {
Private String name;
Private int age;
Person(String name,int age) {
this.name = name;
this.age = age;
}
Public void setAge(int age) {
this.age = age;
}
Public void setName(String name) {
this.name = name;
}
Public int getAge() {
Return this.age;
}
Public String getName() {
return this.name;
}
}
• Scala class
Public class Person {
Var Name = “”
Private Var theAge = 0
Def age = theAge
Def age_= (newAge: Integer): Unit = {if(newAge > 0) theAge = newAge}
}
13
14. Why Scala contd…
• Scala is compatible
• Scala is concise
• Scala is high level
• Scala is statically typed
14
15. Scala features in Java8
• Lambda expressions
• Higher order functions
• Parallel collections
• Function chaining
www.infoq.com/articles/java-8-vs-scala
15
16. Lambda Expressions
• Function literal
• A function with input parameters and function
body
• (type parameter)-> function body
• Eg: (String s1,String s2) -> s1.length() –
s2.length()
16
17. Limitations
• Syntax
Eg: val <identifier>[: <type>] = <data>
Val name :String = “foo”
Difficult to understand as reading backwards. The return
value is at the end of the declaration,and the type comes
after the name.
• Slow compilation and interpreter
Takes much time to compile and run.We can now use the
FSC (Fast Scala compiler),which essentially stays in
memory.
• Steep learning curve
17
21. Homework
• In the Scala REPL, convert the temperature value of 22.5 Centigrade to Fahrenheit. The conversion formula
is cToF(x) = (x * 9/5) + 32.
• Write a new centigrade-to-fahrenheit conversion (using the formula (x * 9/5) + 32), saving each step of the
conversion into separate values. What do you expect the type of each value will be?
• Using the input string "Frank,123 Main,925-555-1943,95122" and regular expression matching, retrieve the
telephone number. Can you convert each part of the telephone number to its own integer value? How would
you store this in a tuple?
• There is a popular coding interview question I’ll call "typesafe", in which the numbers 1 - 100 must be
printed one per line. The catch is that multiples of 3 must replace the number with the word "type", while
multiples of 5 must replace the number with the word "safe". Of course, multiples of 15 must print
"typesafe".
21
Notes de l'éditeur
Its object-oriented constructs make it easy to structure larger systems and to adapt them to new demands. The combination of both styles in Scala makes it possible to express new kinds of programming patterns and component abstractions. It also leads to a legible and concise programming style.
Scala heavily reuses Java, it allows you to add value to existing code since it designed to work seamlessly with Java
Scala code can call Java methods, access Java fields inherit Java classes, and implement Java interfaces
- Scala is quicker to write, easier to read, most importantly less error prone than java
No need to repetitively provide types to the variables,Scala’s type inference is a major contributor to Scala’s conciseness
Scala helps you manage complexity by letting you raise the level of ab- straction in the interfaces you design and use.
A static type system classifies variables and expressions according to the kinds of values they hold and compute. it addresses nicely two of the usual concerns about static typing: verbosity is avoided through type inference and flexibility is gained through pattern matching and several new ways to write and compose types. With these impediments out of the way, the classical benefits of static type systems can be better appreciated.