2. Agenda
• Introduction to Functional
Programming
• Basics of Scala
• Scala Use-casses
• Examples
• Code Samples/Walk-through
2
3. Function Programming
• programs are executed by evaluating expressions, in
contrast with imperative programming where
programs are composed of statements which change
global state when executed. Functional programming
typically avoids using mutable state.
• Functional programming requires that functions
are first-class, which means that they are treated like
any other values and can be passed as arguments to
other functions or be returned as a result of a function.
• Being first-class also means that it is possible to define
and manipulate functions from within other functions.
4. About Scala
• Scala, short for Scalable Language, is a hybrid
functional programming language
• created by Martin Odersky and it was first
released in 2003.
• features of object-oriented and functional
languages
• run on the Java Virtual Machine
• Not a statically typed language
• www.scala-lang.org
5. Scala IDE
• Eclipse:
– http://scala-ide.org/
• IntelliJIdea :
– http://www.jetbrains.com/idea/features/scala.ht
ml
6. Scala in the Enterprise
• The Scala programming language is used by many
companies to develop commercial software and
production systems
• For e.g. :
– LinkedIn, EDFT,Twitter, Novell, the
Guardian, Xebia, Xerox, FourSquare, Sony, Siemens, Th
atcham, OPower, GridGain, AppJet, Reaktorand
many others.
• http://www.scala-lang.org/old/node/1658.html
• http://www.quora.com/Startups/What-startups-
or-tech-companies-are-using-Scala
8. Popular Frameworks built on Scala
• Akka
• Play framework
• Lift web
• Apache Kafka
• Scalding (from twitter)
• Gizzard (from twitter)
• Kestrel
• and many more…
9. Data Types
Byte : 8 bit signed value. Range from -128 to 127
Short : 16 bit signed value. Range -32768 to 32767
Int : 32 bit signed value. Range -2147483648 to 2147483647
Long : 64 bit signed value. -9223372036854775808 to 9223372036854775807
Float : 32 bit IEEE 754 single-precision float
Double : 64 bit IEEE 754 double-precision float
Char : 16 bit unsigned Unicode character. Range from U+0000 to U+FFFF
String : A sequence of Chars
Boolean : Either the literal true or the literal false
Unit: Corresponds to no value : void
Null: null or empty reference
Nothing : The subtype of every other type; includes no values
Any: The supertype of any type; any object is of type Any : Java's Object class
AnyRef: The supertype of any reference type
11. Setting up the development
Environment
• http://www.scala-lang.org/download/
• C:>scala -version
Scala code runner version 2.10.4 -- Copyright 2002-2013, LAMP/EPFL
• C:>scala
Welcome to Scala version 2.10.4 (Java HotSpot(TM) Client VM, Java
1.7.0_51).
Type in expressions to have them evaluated.
Type :help for more information.
• scala> println("Hello, Scala!");
Hello, Scala!
12. Compile and Run
• Compile
– scalac HelloWorld.scala
– C:> scalac HelloWorld.scala
• Run
– C:> scala HelloWorld
– Hello, World!
13. Sample Program
object Test {
def main(args: Array[String]){
println("hello world");
}
def Hello(args:Array[String]){
println("hello scala");
}
this.Hello(null);
}
14. Function
object <name> extends App {
def <function_name>(var_name: <var_type>, var_name: <var_type>):
<return_type>= {
}
}
object Sum extends App {
def sumInt(x: Int, y: Int): Int = {
x + y //return the sum of two values
}
println("Sum of 1 and 2: " + sumInt(1, 2))
}
15. Sum
object Sum extends App {
def sumInt(x: Int, y: Int): Int = {
x + y //return the sum of two values
}
println("Sum of 1 and 2: " + sumInt(1, 2))
}
16. Unit’s Example
object UnitTest extends App {
def greetMe(): Unit = {
println("Greetings !!!")
}
def greetMeByName(name: String): Unit = {
println("Hey " + name)
}
greetMe // print Greetings !!!
greetMeByName("John") // print Hey John
}
17. object & class
• class
– A class is a definition, a description. It defines a type in
terms of methods and composition of other types.
• Object
– An object is a singleton -- an instance of a class which is
guaranteed to be unique. For every object in the code, an
anonymous class is created, which inherits from whatever
classes you declared object to implement. This class
cannot be seen from Scala source code -- though you can
get at it through reflection.
• You can think of the "object" keyword creating a
Singleton object of a class, that is defined implicitly.
18. object & class
e.g.
• object A extends B with C
– This will declare an anonymous class which
extends B with the trait C and create a single
instance of this class named A.
• http://stackoverflow.com/questions/1755345/
scala-difference-between-object-and-class
19. Scala Keywords (Reserved)
• Can not be used as constant or variable or any other
identifier names.
abstract case catch class
def do else extends
false final finally for
forSome if implicit import
lazy match new null
object override package private
protected return sealed super
this throw trait try
true type val var
while with yield
- : = =>
<- <: <% >:
#@
20. Scala packages import
• import scala.xml._
– imports the contents of the scala.xml package
• import scala.collection.mutable.HashMap
– You can import a single class and object, for example,
HashMap from the scala.collection.mutable package
• import scala.collection.immutable.{TreeMap, TreeSet}
– You can import more than one class or object from a single
package, for example, TreeMap and TreeSet from the
scala.collection.immutable package:
21. Multi line Strings
• multi-line string literal is a sequence of
characters enclosed in triple quotes """ ... ""“
• For e.g. :
– """the present string
spans three
lines."""
22. var vs val
• var refers to a variable that can change value
– mutable variable
– var myVar : String = "Foo"
• val refers to a variable that can not change
value
– Immutable variable
– val myVal : String = "Foo“
– Equivalent to Java’s final
23. Scala’s Null
• null value is of type scala.Null
• compatible with every reference type.
• denotes a reference value which refers to a
special "null" object.
24. Scala vs Java
• All types are objects.
• Type inference.
• Nested Functions.
• Functions are objects.
• Domain specific language (DSL) support.
• Traits.
• Closures.
• Concurrency support inspired by Erlang.
25. Advanced Concepts
• Pattern matching
• Tail recursion
• Large library of list functions
• Functional dictionary class
• Automatic currying
• Concise way to compose functions
• Lazy lists
27. Thanks!
@rahuldausa on twitter and slideshare
http://www.linkedin.com/in/rahuldausa
Interested in Search/Information Retrieval ?
Join us @ http://www.meetup.com/Hyderabad-Apache-Solr-Lucene-Group/
27