Download - An Introduction to Scala
Discussion document – Strictly Confidential & Proprietary
An Introduction to ScalaDallas, TXMarch 07, 2012
An Introduction to Scala
2March 7, 2012
Agenda …
The four W’s plus an H
• What: Scala Intro
• When: Scala History
• Who: Scala Implementations
• Why: A Case for Scala
• How: The Basics of Scala
– Getting Up and Running
– Classes and Objects
– Functional Programming
– Traits and Mixins
– Java in Scala
– Scala in Java
An Introduction to Scala
3March 7, 2012
Agenda …
Goals
• Have an understanding of what Scala is
• Have an interest in learning more
• Go install Scala!
An Introduction to Scala
4March 7, 2012
What: Scala Intro …
What is Scala?
Scala is a general purpose programming language designed to express common programming patterns in a concise, elegant and type-safe way. It smoothly integrates features of object-oriented and functional languages, enabling Java and other programmers to be more productive. Code sizes are typically reduced by a factor of two to three when compared to an equivalent Java application.
• Origin of name is two-fold
– Scala is Italian for staircase. Scala is considered a “step up” from other languages
– SCAlable Language
• Seamless integration with Java. Runs in the JVM
• Proven performer in high transaction, highly scalable environments
• Developers can follow an imperative or functional style
• ThoughtWorks has put Scala in the “trial” radar category (July 2011)
An Introduction to Scala
5March 7, 2012
When: Scala History …
Scala History … 1995 to 2012
Brief history of Scala• Developed by Martin Odersky
– 1995 he learned of Java and wrote functional language that compiled to Java bytecode - Pizza
– Pizza evolved into what we now recognize as Java generics
• Sun approached Odersky in 1997 to write Java 1.1 compiler
• Odersky led javac development from Java 1.1 through 1.4
• In 1999, Odersky joined EPFL to conduct research into improving functional and OO languages
• Design of Scala began in 2001 and first release was in 2003
• Early releases of compiler written in Java
• Version 2.0.0 introduced a completely rewritten compiler in Scala
• Current version 2.9.1 released in August 2011
An Introduction to Scala
6March 7, 2012
Who: Scala Implementations …
Scala is finding a home in high transaction environments
• Twitter Kestrel – Twitter’s message queue server
– Ported from RoR
• FourSquare – Message queue, website, mobile website and RESTful API
• LinkedIn – Public and backend RESTful API’s
– Ported from RoR
• Novell – Pulse, a cloud-based, real-time collaboration platform for the enterprise
An Introduction to Scala
7March 7, 2012
Why: A Case for Scala …
Is Scala a fit?
Upside of Scala• Ease of integration with existing Java environments
• When a functional style is utilized, scales easily
• Performance is equivalent, and in some cases, better than Java
• Good candidate for medium risk greenfield projects
• Learning curve is not massive, as Scala follows Java syntax
Downside of Scala• Toolset is immature
– IDE integration is weak, but improving
– Java profiling tools can be used, but difficult
• Overcoming Java footprint will be difficult. It’s comfortable and works. It’s the COBOL/C+ of our generation
• User acceptance still low due to lack of knowledge and reasons listed above
An Introduction to Scala
8March 7, 2012
How: The Basics of Scala ...
What you need to know to get started!
• Getting Up and Running
• Building Blocks
• Classes and Objects
• Functional Programming
• Traits and Mixins
• Java in Scala
• Scala in Java
An Introduction to Scala
9March 7, 2012
How: The Basics of Scala …
Getting up and running
Required• Java 1.5 or greater
• Scala 2.9.1 distribution
Optional• SBT – Simple Build Tool
• IDE Plugin
– ScalaIDE (Eclipse – must use Helios)
– Scala Plugin for IntelliJ IDEA
– Scala Plugin for NetBeans
Notes:
1) Installation of Scala and SBT involve expanding compressed file and adding to PATH
2) IDE installation varies by tool; some dependency on IDE release number
3) ScalaIDE officially supported by Typesafe
An Introduction to Scala
10March 7, 2012
How: The Basics of Scala …
Building Blocks
Declarations - var• A var is similar to a non-final variable in Java
• If type is not declared, it will be inferred from the assigned object
• Once initialized, it can be reassigned (mutable), but type cannot change
• Object can be assigned at any time
class VarA(n: Int) { var value = n} class VarB(n: Int) { val value = new VarA(n)} object ExampleVar { var x = new VarB(5) x = new VarB(6) // x.value = new VarA(7) x.value.value = 7}
Example
An Introduction to Scala
11March 7, 2012
How: The Basics of Scala …
Building Blocks
Declarations - val• A val is similar to a final variable in Java
• Once initialized, it can not be reassigned (immutable)
• Object must be initialized at declaration
• Object cannot be reassigned, but internal state can be modified
class ValA(n: Int) { var value = n} class ValB(n: Int) { val value = new ValA(n)} object ExampleVar { val x = new ValB(5) x.value.value = 6}
Example
An Introduction to Scala
12March 7, 2012
How: The Basics of Scala …
Building Blocks
Declarations - def• A def is used to define a function
• A comma-separated list of parameters in parentheses follows the name
• The type of the return value must be defined following the parameters
• If a return type is not included, you are defining a “procedure”
• The equals sign hints to the fact a function defines an expression resulting in a value
• The final value of the control block is the return value
class ExampleDef(n: Int, a: Int, b: Int) { val value = n + max(a,b) def max(x: Int, y: Int): Int = { if (x > y) x else y }}
Example
An Introduction to Scala
13March 7, 2012
How: The Basics of Scala … Classes and Objects …
Classes
Class• A class in Scala looks similar to a class in Java – minus the boilerplate code
• Public by default
• Getters and setters defined by variable declaration
class Coordinate() { var degrees = 0.0 var minutes = 0.0 var seconds = 0.0 def asDegrees = degrees + minutes/60.0 + seconds/60.0/60.0 def asRadians = asDegrees.toRadians} object Example extends App { val latitude = new Coordinate latitude.degrees = 32.0 latitude.minutes = 57.0 latitude.seconds = 30.762}
Example
An Introduction to Scala
14March 7, 2012
How: The Basics of Scala … Classes and Objects …
Classes
Class – Primary Constructor• More concise
• val has getter only
• Primary constructor creates the fields
class Coordinate(val degrees: Double, val minutes: Double, val seconds: Double) { def asDegrees = degrees + minutes/60.0 + seconds/60.0/60.0 def asRadians = asDegrees.toRadians} object Example extends App { val latitude = new Coordinate(32.0, 57.0, 30.762) val longitude = new Coordinate(96.0, 49.0, 25.1076) println(latitude.degrees) // prints 32.0}
Example
An Introduction to Scala
15March 7, 2012
How: The Basics of Scala … Classes and Objects …
Classes
Class – Auxiliary Constructor• Auxiliary constructor is created as a def this
• Must start with a call to a previously defined auxiliary or primary constructor
class Coordinate(val degrees: Double, val minutes: Double, val seconds: Double) { def this(degrees: Double, minutes: Double) = this(degrees, minutes, 0.0) def this(degrees: Double) = this(degrees, 0.0) def this() = this(0.0) def asDegrees = degrees + minutes/60.0 + seconds/60.0/60.0 def asRadians = asDegrees.toRadians} object Example extends App { val latitude = new Coordinate(32.0) val longitude = new Coordinate(96.0, 49.0) println(latitude.minutes) // prints 0.0 println(longitude.minutes) // prints 49.0}
Example
An Introduction to Scala
16March 7, 2012
How: The Basics of Scala … Classes and Objects …
Objects
Object• Creates a singleton of a class
• Can be used as a home for miscellaneous functions
• No constructor parameters
object CrederaLatitude extends Coordinate { this.degrees = 32.0 this.minutes = 57.0 this.seconds = 30.762 def printCoordinate = println(this.asDegrees)}class Coordinate(val degrees: Double, val minutes: Double, val seconds: Double) { def this(degrees: Double, minutes: Double) = this(degrees, minutes, 0.0) def this(degrees: Double) = this(degrees, 0.0) def this() = this(0.0)
def asDegrees = degrees + minutes/60.0 + seconds/60.0/60.0 def asRadians = asDegrees.toRadians} object Example extends App { CrederaLatitude.printCoordinate // prints 32.958545}
Example
An Introduction to Scala
17March 7, 2012
How: The Basics of Scala … Functional Programming …
Functional programming… defined
Definition• Treats computations as the evaluation of mathematical functions
• Avoids state and mutable data
• Calling a function twice, with the same arguments, should produce the same results both times
• First-class functions are functions that either take other functions as arguments or return them as results
• In Scala, a function literal (the definition) is compiled to a functional value (a class)
• Functional values can be assigned to a val or var. If assigned to a var, it is mutable.
var increase = (x: Int) => x + 1val result0 = increase(10) // results in 11
Example
An Introduction to Scala
18March 7, 2012
How: The Basics of Scala … Functional Programming …
Functional programming… defined
Definition• Treats computations as the evaluation of mathematical functions
• Avoids state and mutable data
• Calling a function twice, with the same arguments, should produce the same results both times
• First-class functions are functions that either take other functions as arguments or return them as results
• In Scala, a function literal (the definition) is compiled to a functional value (a class)
• Functional values can be assigned to a val or var. If assigned to a var, it is mutable.
var increase = (x: Int) => x + 1val result0 = increase(10) // results in 11 increase = (x: Int) => x + 10val result1 = increase(10) //results in 20
Example
An Introduction to Scala
19March 7, 2012
How: The Basics of Scala … Functional Programming …
Passing functional values
Passing as a value• Functional values can be passed as a value into another function
• Common use is to make calls on the immutable collection List
val someNumbers = List(-22,-19,-12,-9,-2,0,1,5,9,32)val printList = (x: Int) => println(x)println("print all numbers: ")someNumbers.foreach(printList) println("-----------------------------------")
Example
An Introduction to Scala
20March 7, 2012
How: The Basics of Scala … Functional Programming …
Passing functional values
Passing as a value• Functional values can be passed as a value into another function
• Common use is to make calls on the immutable collection List
val someNumbers = List(-22,-19,-12,-9,-2,0,1,5,9,32)val printList = (x: Int) => println(x)println("print all numbers: ")someNumbers.foreach(printList) println("-----------------------------------")println("print only positive numbers:") val filterList = (x: Int) => x > 0someNumbers.filter(filterList).foreach(printList)
Example
An Introduction to Scala
21March 7, 2012
How: The Basics of Scala … Traits and Mixins …
Traits and Mixins
Trait• Scala developers saw inherent problems with Java interfaces
• Scala solution is a combination of Java interfaces and Ruby mixins called traits
• Like an object, traits do not have constructors
• Added to class via the extends keyword
• Additional traits can be “mixed in” via the with keyword
An Introduction to Scala
22March 7, 2012
How: The Basics of Scala … Java in Scala …
Integration
Java in Scala• Add jar to your CLASSPATH
• Import package or class
• Implement an object using the class
import org.joda.time._ trait CurrentTime { val now = new DateTime()}
An Introduction to Scala
23March 7, 2012
How: The Basics of Scala … Java in Scala … Scala in Java …
Integration
Java in Scala• Add jar to your CLASSPATH
• Import package or class
• Implement an object using the class
Scala in Java• Compile to a jar
• Add jar to your CLASSPATH
• Import package or class
• Implement an object using the class
import com.credera.tech.scala.*; public class Hello { public static void main(String[] args) { System.out.println("Hello, this is java."); CrederaOffice.printOfficeLocation(); }}
import org.joda.time._ trait CurrentTime { val now = new DateTime()}
An Introduction to Scala
24
Web Resources
March 7, 2012
Appendix … Web References ...
Resources
Description Link
Scala http://www.scala-lang.org
SBT – Simple Build Tool http://github.com/harrah/xsbt
ScalaIDE for Eclipse http://scala-ide.org
Scala Plugin for IntelliJ IDEA http://confluence.jetbrains.net/display/SCA/Scala_Plugin+for+IntelliJ+IDEA
Scala Plugin for NetBeans http://wiki.netbeans.org/Scala
ThoughtWorks Technology Radar http://thoughtworks.com/radar
Indeed.com Scala Job Trends http://indeed.com/jobtrends?q=scala&l=&relative=1
Twitter’s Scala Experience http://web2expo.com/webexsf2009/public/schedule/detail/6110
LinkedIn’s Scala Experience http://infoq.com/articles/linkedin-scala-jruby/voldement
Java Interfaces Discussion http://www.codecommit.com/blog/scala/scala-for-java-refugees-part-5
An Introduction to Scala
25March 7, 2012
Contact ...
Contact Me!
@brentlemons
slideshare.net/brentlemons