Introduction to Scala

Download Introduction to Scala

Post on 28-Nov-2014




1 download

Embed Size (px)


A Short presentation on Scala for beginners with Introduction to Functional Programming, Basics of Scala, Use-casses and Examples


<ul><li> 1. Introduction to Scala August 2014 Meetup Rahul Jain @rahuldausa </li> <li> 2. Agenda Introduction to Functional Programming Basics of Scala Scala Use-casses Examples Code Samples/Walk-through 2 </li> <li> 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. </li> <li> 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 </li> <li> 5. Scala IDE Eclipse: IntelliJIdea : ml </li> <li> 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. or-tech-companies-are-using-Scala </li> <li> 7. Credit: </li> <li> 8. Popular Frameworks built on Scala Akka Play framework Lift web Apache Kafka Scalding (from twitter) Gizzard (from twitter) Kestrel and many more </li> <li> 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 </li> <li> 10. How to Install </li> <li> 11. Setting up the development Environment C:&gt;scala -version Scala code runner version 2.10.4 -- Copyright 2002-2013, LAMP/EPFL C:&gt;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&gt; println("Hello, Scala!"); Hello, Scala! </li> <li> 12. Compile and Run Compile scalac HelloWorld.scala C:&gt; scalac HelloWorld.scala Run C:&gt; scala HelloWorld Hello, World! </li> <li> 13. Sample Program object Test { def main(args: Array[String]){ println("hello world"); } def Hello(args:Array[String]){ println("hello scala"); } this.Hello(null); } </li> <li> 14. Function object extends App { def (var_name: , var_name: ): = { } } 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)) } </li> <li> 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)) } </li> <li> 16. Units 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 } </li> <li> 17. object &amp; 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. </li> <li> 18. object &amp; 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. scala-difference-between-object-and-class </li> <li> 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 - : = =&gt;</li></ul>