accu 2013 taking scala into the enterpise
DESCRIPTION
Talk at ACCU 2013 in BristolTRANSCRIPT
Peter Pilgrim
Oracle Java Champion
ACCU Conference 2013
Taking Scala into the Enterprise
About the Speaker
• Java Champion
• Independent Contractor
• Java since 1998
• Scala since 2010
13 April 2013 Xenonique ©2013 2
Agenda
• What’s New in Scala 2.10?
• Play Framework
• AKKA Framework
13 April 2013 Xenonique ©2013 3
TypeSafe
13 April 2013 Xenonique ©2013 4
Simplicity
13 April 2013 Xenonique ©2013 5
“I want a language with simple means to do interesting things, which is not the same as a language to do only simple things.” Prof. Martin Odersky
TypeSafe: The Company
• Founded in Summer 2011
• Based in Switzerland and headquartered in New York, USA
• Scala, Akka and Play Framework
• Professional Support for Scala
13 April 2013 Xenonique ©2013 6
Growth of Scala Training
• Scala Solutions
• Dick Wall and Bill Venners, Escalate Solutions
• ScalaDays conference in London 2012
• Scala content at JavaOne 2012
13 April 2013 Xenonique ©2013 7
Scala Knowledge
• Hunger for more know-how
• Growing Awareness in Functional Programming
• Object Oriented Languages embrace FP principles
• JDK 8 Lambdas
• Clojure jobs at CitiBank, London & NY
13 April 2013 Xenonique ©2013 8
Scala in Enterprise
• Morgan Stanley
• HSBC
• Guardian UK
• Linked-In
• Four Square
13 April 2013 Xenonique ©2013 9
13 April 2013 Xenonique ©2013 10
Scala Revision Demonstration of the simplicity
Scalable Language
4/13/2013 XeNoNiQUe.co.uk (c) 2011 11
Still has a very bright future
Purely Object-Oriented Statically-typed
Functional
JVM Language
Typing Derived from “Pascal” Tree of Computer Language
<variableName> [: [ <Type> ]
personName: String
taxRate: Float
density: Double
found: False
persion: Person
13 April 2013 Xenonique ©2013 12
Variables and Values
• Assignment less programming
• Prefer val over var
var x = 10.0; x = 10 + x
val y = 10.0
val z: Float = x
var t: Int = 42; t = t * 2
13
Scala Class
class Person (
val firstName: String
val lastName: String,
val height: Float,
val age: Int ) {
// Write your definition here
}
13 April 2013 Xenonique ©2013 14
Instances of Scala Classes
val p1 = new Person( "Billy", "Mitchell", 5.10F, 42 )
val p2 = new Person( "Kat", "Moon", 5.8F, 39 )
13 April 2013 Xenonique ©2013 15
Companion Objects
object Person {
private records = List[Person]()
def apply(fn: String, ln: String,
h: Float, a: Int): Person = {
val p = new Person(fn, ln, h, a );
records = p :: records.reverse // O(1)
return p
}
def recordCount() = records.size
}
13 April 2013 Xenonique ©2013 16
Case Classes
class Transmission( driveTrain: String )
13 April 2013 Xenonique ©2013 17
val isEven: (Int => Boolean) = (k: Int) => k % 2 == 0
• Functions are values, values are object
• Ergo, functions are objects in Scala
Scala Functions
13 April 2013 Xenonique ©2013 18
Scala Code
def uniqueSorted[Symbol]( p: List[Symbol] ): List[Symbol] = {
val myOrdering =
Ordering.fromLessThan[Symbol](
_.toString < _.toString )
var acc = SortedSet.empty(myOrdering)
def compress0( q: List[Symbol] ): Unit = {
q match {
case Nil => Nil
case x :: xs => { acc += x ; compress0(xs) }
}
}
compress0( p )
acc.toList
}
13 April 2013 Xenonique ©2013 19
Functions are First Class
• In Scala, functions are first class citizens
• Functions can return functions
13 April 2013 Xenonique ©2013 20
SBT
• SBT is the de-facto build tool
• Works with Maven
• Incremental Compilation +1
• DSL written in Scala +1
• Plugins Available +1
• Complex to Understand -1
13 April 2013 Xenonique ©2013 21
Gradle
• Gradle is written in Groovy
• Gradle is a DSL too +1
• Easier to Grok +1
• Since v1.4 Gradle support incremental compilation through Zinc
• Not the de-facto standard -1
13 April 2013 Xenonique ©2013 22
String Interpolation
• SIP 12: String interpolation
• S”Book written by $author”
• Arbitary identifier: StringContext
• StringContext(“Books written by”).id(author)
13 April 2013 Xenonique ©2013 23
Value Classes
• Implicit Classes
• Classes that extend AnyVal
• Unboxed value classes
• Language imports
• import language.implicitConversions
13 April 2013 Xenonique ©2013 24
Value Classes
Elegant wrappers around simple types
• A Value class is treated as another type
• A value value is assigned to an array
• Doing runtime type tests, such as pattern matching
13 April 2013 Xenonique ©2013 25
Incremental Compilation
• Incremental compilation, Zinc
• Language with simple means to do interesting things
13 April 2013 Xenonique ©2013 26
Scala Test
• The de-facto testing framework
• Created Bill Venners, JVM Book
• DSL unit testing language
• Behaviour Driven Development +1
• Test Driven Development
13 April 2013 Xenonique ©2013 27
13 April 2013 Xenonique ©2013 28
Play Framework Non Java EE web framework
Play Framework 2.0
• Non Java EE – No Servlet
• Built for Web Development
• Direct manipulation of HTTP
• Asynchronous Input and Output
• Scalable Vertically
29
Play Framework Architecture
13 April 2013 Xenonique ©2013 30
Web Client
Play Framework
Netty (Java NIO Client Server Framework)
Network
Play Advantages
• Highly productive web development
• Work with Web Designers
• Feels of “Rails”
• Compiler type checked
• Model, View and Controller
31
Play Disadvantages
• Completed encapsulated environment
• Unlike JavaEE and WAR file deployment to application server
• No WAR file (yet) as of Play 2.1.x
• Ironically, WAR files need JavaEE 7 (WebSocket and Async Servlet 3.1)
32
Play Style
• Dispatch through a Router file
• Directly manipulation of the HTTP response, Ok(200), Error(404)
• Control HTTP interface: GET, PUT, POST, DELETE
• Model, View and Controller scala objects
• Asynchronuous process API
33
Play Persistence
• Play has its own persistence provider Anorm: a direct SQL framework
• Play can also use Squeryl: an Object-Relational Mapper DSL with type safety.
34
Developer Summary
What You Will Do Tomorrow?
Scala 2.10
Why FP?
Gradle / SBT
Play / Akka
Scala Test
13 April 2013 Xenonique ©2013 37
Game Over
38
Professional Services
4/13/2013
Contract Software Development Scala, JavaEE, JavaFX, TDD, Gradle W: http://www.xenonique.co.uk/blog/ E: [email protected]
39
Professional Services
4/13/2013
[email protected] Scala, JavaEE 7, JavaFX Contracting Software Development
Attributions
• The author would very like to attribute these pleasurable Creative Commons License 3.0 photographers
• Brush Metal Elevator door By Jerry "Eyes" Ranch; West Des Moines, IA, USA; http://www.flickr.com/photos/ranchjp/3684969194/
• 19/365 Game Over by Mykl Roventine; http://www.flickr.com/photos/myklroventine/3210068573
• Experiment in abstract lightning by Bob Doran; Walnut Creek, Arcata, USA ; http://www.flickr.com/photos/humblog/4522984790/in/photostream/
40
Attributions #2
• Study in Math exam photo by Steve S; http://www.flickr.com/photos/scubasteveo/296747958/
41