SlideShare a Scribd company logo
1 of 46
Download to read offline
Intro to Scala for Java Devs

      Sungard– 4/17/2012
Intro
•  Eric Pederson
  –  eric@cpextechnology.com
  –  Twitter @ericacm
  –  Sourcedelica.com/blog
  –  Background in Java, Groovy, Javascript, PHP, etc.


•  Using Scala for last two years
•  Two Scala apps developed for NYSE in
   Production
Platform at NYSE
•    Scala 2.9.1
•    JDK 1.6
•    Tomcat / JBoss
•    Maven
•    Using lots of Java libraries
     –  Spring, Hibernate, CXF, Mule, ApacheMQ
     –  Bouncycastle, OpenSAML, Velocity, etc, etc.
What is Scala?
•  Hybrid Object-Functional language
•  Statically typed
•  Developed by Martin Odersky
  –  Java Generics
  –  Java Compiler (1.3+)
•  First release in 2003
What is Scala?
•  Designed for general purpose
   programming
•  Performance on par with Java*
•  Scalable
       –  Designed to write programs ranging from
          scripts up to huge systems
       –  You don’t have to use all of the features to be
          productive

*	
  There	
  are	
  some	
  gotchas	
  you	
  have	
  to	
  watch	
  out	
  for	
  
Why Should I Use Scala?
•  Allows you to write very concise code
  –  Productivity on the level of Groovy / Ruby
•  Concurrency-ready
•  Excellent interoperability with Java code
•  Lots of other reasons…
Conciseness
•  Code size reduced by 2-3x compared to
   Java
•  Less code == easier to read
•  Less code == fewer bugs
Conciseness
•    Type Inference
•    Expressions, not statements
•    Higher-ordered functions
•    Case classes
•    Pattern matching
Type Inference
•  Variables
  val	
  subscrip7onEvents	
  =	
  foo()	
  
  	
  

•  Method return types
  def	
  listOfPeople	
  =	
  List(“Paul”,	
  “Eric”,	
  “John”,	
  “Mar7n”)	
  
  	
  


•  Generic type parameters
  case	
  class	
  MyPair[A,	
  B](x:	
  A,	
  y:	
  B)	
  
  val	
  p	
  =	
  MyPair(1,	
  “foo”)	
  	
  	
  	
  	
  //	
  p	
  is	
  MyPair[Int,	
  String]	
  
Type inference
•  Java
  HashMap<String,	
  Customer>	
  customers	
  =	
  	
  
  	
  	
  	
  	
  	
  	
  	
  new	
  HashMap<String,	
  Customer>();	
  
  customers.add("id1",	
  	
  
                         	
   	
  new	
  Customer("Eric",	
  "917-­‐444-­‐1234");	
  
  customers.add("id2",	
  	
  
                         	
   	
  new	
  Customer("Paul",	
  "718-­‐666-­‐9876");	
  
•  Scala
  val	
  customers	
  =	
  HashMap(	
  
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  "id1"-­‐>Customer("Eric",	
  "917-­‐434-­‐1852"),	
  	
  
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  "id2"-­‐>Customer("Paul",	
  "718-­‐666-­‐9876"))	
  	
  
Expressions, not statements
val	
  server	
  =	
  if	
  (environment	
  ==	
  "development”)	
  {	
  
	
  	
  	
  	
  	
  	
  val	
  factory	
  =	
  new	
  MBeanServerFactoryBean	
  
	
  	
  	
  	
  	
  	
  factory.aberProper7esSet()	
  
	
  	
  	
  	
  	
  	
  factory.getObject.asInstanceOf[MbeanServer]	
  
}	
  else	
  {	
  
	
  	
  	
  	
  	
  	
  val	
  clazz	
  =	
  	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  Class.forName("org.jboss.mx.u7l.MBeanServerLocator")	
  
	
  	
  	
  	
  	
  	
  clazz.getMethod("locateJBoss”).invoke(null)	
  
}	
  
	
  
//	
  server is assigned one of the bold values	
  
Expressions, not statements
val	
  n	
  =	
  try	
  {	
  
	
  	
  	
  	
  userInput.toInt	
  
}	
  catch	
  {	
  
	
  	
  	
  	
  case	
  _	
  =>	
  0	
  
}	
  
	
  
//	
  n	
  is	
  an	
  Int,	
  0	
  if	
  unable	
  to	
  parse	
  userInput	
  
Collections API
•  Very comprehensive
  –  For example, over 200 methods on List	
  
•  Higher ordered functions
  –  foreach,	
  map,	
  flatMap,	
  exists,	
  forall,	
  find,	
  findAll,	
  
     filter,	
  groupBy,	
  par77on, etc.
•  Concise literals
•  Immutable and mutable variations
Collection Literals
•  val	
  l1	
  =	
  List(1,	
  2,	
  3)	
  

•  val	
  m1	
  =	
  Map(“name”	
  -­‐>	
  “Eric”,	
  “city”	
  -­‐>	
  “NYC”)	
  

•  val	
  s1	
  =	
  Set(Car(“Ford”),	
  Car(“Isuzu”),	
  Car(“VW”))	
  
Collections API
•  Scala
       val	
  groups	
  =	
  	
  
          	
  subscrip7onEvents.groupBy(e	
  =>	
  e.subscrip7on.id)	
  
	
  

•  Java
       Map<String,	
  List<SubscripLonEvent>>	
  groups	
  =	
  	
  
       	
  	
  	
  	
  new	
  HashMap<String,	
  ArrayList<Subscrip7onEvent>();	
  
       for	
  (Subscrip7onEvent	
  se	
  :	
  subscrip7onEvents)	
  {	
  
       	
  	
  	
  	
  ArrayList<Subscrip7onEvent>	
  seList	
  =	
  groups.get(se.subscrip7on.id)	
  
       	
  	
  	
  	
  if	
  (seList	
  ==	
  null)	
  {	
  
       	
  	
  	
  	
  	
  	
  	
  	
  seList	
  =	
  new	
  ArrayList<Subscrip7onEvent>();	
  
       	
  	
  	
  	
  	
  	
  	
  	
  groups.put(se.subscrip7on.id,	
  seList)	
  
       	
  	
  	
  	
  }	
  
       	
  	
  	
  	
  seList.add(se);	
  
       }	
  
       	
  
Count characters in documents
Try #1 – Java-esque Scala
	
  	
  	
  	
  var	
  total	
  =	
  0	
  
	
  	
  	
  	
  for	
  (doc	
  <-­‐	
  docs)	
  {	
  
	
  	
  	
  	
  	
  	
  total	
  +=	
  doc.length	
  
	
  	
  	
  	
  }	
  
Try #2 – Use higher order functions
	
  	
  	
  	
  var	
  total	
  =	
  0	
  
	
  	
  	
  	
  docs.foreach(doc	
  =>	
  total	
  +=	
  doc.length)	
  
Count characters in documents
Try #3 – Use fold
               docs.foldLeb(0)((accum,	
  current)	
  =>	
  accum	
  +	
  current.length)	
  
	
  	
  	
  	
  



Try #4 – Use type-classes (eg. Numeric)
	
  	
  	
  	
  docs.map(_.length).sum	
  

Try #5 – Use the 'view' method to turn multiple
  passes into one
	
  	
  	
  	
  docs.view.map(_.length).sum	
  
Variables
scala>	
  var	
  i	
  =	
  0	
  
i:	
  Int	
  =	
  0	
  
	
  
scala>	
  i	
  =	
  2	
  
i:	
  Int	
  =	
  2	
  
	
  
scala>	
  val	
  j	
  =	
  0	
  
j:	
  Int	
  =	
  0	
  
	
  
scala>	
  j	
  =	
  3	
  
<console>:8:	
  error:	
  reassignment	
  to	
  val	
  
	
  
scala>	
  lazy	
  val	
  l	
  =	
  expensiveComputa7on()	
  
l:	
  Double	
  =	
  <lazy>	
  
	
  
Uniform Access Principle
scala>	
  object	
  Ints	
  {	
  
	
  	
  	
  	
  	
  |	
  	
  	
  	
  	
  	
  	
  	
  	
  var	
  i	
  =	
  1	
  
	
  	
  	
  	
  	
  |	
  	
  	
  	
  	
  	
  	
  	
  	
  val	
  j	
  =	
  2	
  
	
  	
  	
  	
  	
  |	
  	
  	
  	
  	
  	
  	
  	
  	
  def	
  k	
  =	
  i	
  +	
  j	
  
	
  	
  	
  	
  	
  |	
  }	
  
	
  
scala>	
  Ints.i	
  
res8:	
  Int	
  =	
  1	
  
	
  
scala>	
  Ints.j	
  
res9:	
  Int	
  =	
  2	
  
	
  
scala>	
  Ints.k	
  
res10:	
  Int	
  =	
  3	
  
Uniform Access Principle
scala>	
  object	
  M	
  {	
  
	
  	
  	
  	
  	
  |	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  private	
  var	
  pm	
  =	
  0	
  
	
  	
  	
  	
  	
  |	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  def	
  m	
  =	
  pm	
  
	
  	
  	
  	
  	
  |	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  def	
  m_=(in:	
  Int)	
  {	
  pm	
  =	
  in	
  }	
  
	
  	
  	
  	
  	
  |	
  }	
  
scala>	
  import	
  M._	
  
	
  
scala>	
  m	
  
res5:	
  Int	
  =	
  0	
  
	
  
scala>	
  m	
  =	
  5	
  
m:	
  Int	
  =	
  5	
  
	
  
Case Classes
•  Scala
 scala>	
  case	
  class	
  Car(make:	
  String,	
  model:	
  String,	
  mpg:	
  Int)	
  
 defined	
  class	
  Car	
  
 	
           	
  
 scala>	
  	
  val	
  c	
  =	
  Car("Honda",	
  "Civic",	
  40)	
  
              	
  
 c:	
  Car	
  =	
  Car(Honda,Civic,40)	
  
Case Classes
•  Java
    public	
  class	
  Car	
  implements	
  scala.Product,	
  scala.Serializable	
  	
  {	
  
    	
  	
  	
  	
  final	
  private	
  String	
  make,	
  model;	
  
    	
  	
  	
  	
  final	
  private	
  int	
  mpg;	
  
    	
  	
  	
  	
  Car(String	
  make,	
  String	
  model,	
  int	
  mpg)	
  {	
  
    	
  	
  	
  	
  	
  	
  	
  	
  this.make	
  =	
  make;	
  	
  this.model	
  =	
  model;	
  this.mpg	
  =	
  mpg;	
  
    	
  	
  	
  	
  }	
  
    	
  	
  	
  	
  public	
  String	
  getMake()	
  {	
  return	
  make;	
  }	
  
    	
  	
  	
  	
  public	
  String	
  getModel()	
  {	
  return	
  model;	
  }	
  
    	
  	
  	
  	
  public	
  int	
  getMpg()	
  {	
  return	
  mpg;	
  }	
  
    	
  	
  	
  	
  public	
  String	
  toString()	
  {	
  return	
  “Car(	
  “	
  +	
  make	
  +	
  ….	
  }	
  
    	
  	
  	
  	
  public	
  boolean	
  equals(Object	
  that)	
  {	
  if	
  (that	
  instanceOf	
  Car)	
  &&	
  ……	
  }	
  
    	
  	
  	
  	
  public	
  int	
  hashCode()	
  {	
  return	
  19	
  +	
  ……	
  }	
  
    	
  	
  	
  	
  public	
  Car	
  copy(String	
  make,	
  String	
  model,	
  int	
  mpg)	
  {	
  …..	
  }	
  
    	
  	
  	
  	
  //	
  plus	
  9	
  other	
  Scala-­‐specific	
  methods	
  
    }	
  	
  
    	
  
Case Classes
       •  Case classes can also have mutable fields
          and methods
case	
  class	
  Car(make:	
  String,	
  model:	
  String,	
  mpg:	
  Int,	
  var	
  odometer)	
  {	
  
	
  	
  	
  	
  def	
  driveMiles(miles:	
  Int)	
  {	
  odometer	
  +=	
  miles	
  }	
  
}	
  
	
  
	
  
       •  In Scala you can define multiple classes
          per source file
Pattern Matching
•  Case Classes
  // Class hierarchy:
  trait Expr
  case class Num(value : int) extends Expr
  case class Var(name : String) extends Expr
  case class Mul(left : Expr, right : Expr) extends Expr

  // Simplification rule:
  e match {
     case Mul(x, Num(1)) ⇒ x
     case _ ⇒ e
  }
Pattern Matching
•  Match on constants

     	
  def	
  describe(x:	
  Any)	
  =	
  x	
  match	
  {	
  	
  
 	
   	
     	
  case	
  5	
  =>	
  "five"	
  	
  
 	
   	
     	
  case	
  true	
  =>	
  "truth"	
  	
  
 	
   	
     	
  case	
  "hello"	
  =>	
  "hi!”	
  
 	
   	
     	
  case	
  Nil	
  =>	
  "the	
  empty	
  list"	
  	
  
 	
   	
     	
  case	
  _	
  =>	
  "something	
  else”	
  
 	
  }	
  
Pattern Matching
•  Typed patterns

     def	
  generalSize(x:	
  Any)	
  =	
  x	
  match	
  {	
  
 	
  	
   	
  case	
  s:	
  String	
  =>	
  s.length	
  	
  
 	
  	
   	
  case	
  m:	
  Map[_,	
  _]	
  =>	
  m.size	
  	
  
 	
  	
   	
  case	
  _	
  =>	
  -­‐1	
  
 	
  }	
  
No Checked Exceptions
//	
  Look	
  ma,	
  no	
  throws	
  clause!	
  
def	
  foo()	
  {	
  
	
  	
  	
  	
  throw	
  new	
  java.lang.Excep7on	
  
}	
  
Concurrency-readiness
•  The future present is many cores
•  Writing thread-safe code in Java is very
   difficult
  –  Mostly due to shared,
    mutable state
Concurrency-readiness
•  Scala
  –  Excellent support for immutability
  –  Actors / Futures
  –  Parallel collections
Immutability
•  Case classes
•  Immutable collections are default
  –  Copies of collections share data
•  val vs. var, val is encouraged
•  Method parameters are vals
Actors
•  Included in standard Scala library
•  Simplified multithreading and
   coordination
•  Based on message passing
  –  Each actor has a mailbox queue of messages
•  Implementation based on Erlang
Actors
	
  	
  	
  	
  
object	
  Coun7ngActor	
  extends	
  Actor	
  {	
  	
  
	
  	
  	
  	
  def	
  act()	
  {	
  	
  
	
  	
  	
  	
  	
  	
  	
  	
  for	
  (i	
  <-­‐	
  1	
  to	
  10)	
  {	
  	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  println("Number:	
  "+i)	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  Thread.sleep(1000)	
  	
  
	
  	
  	
  	
  	
  	
  	
  	
  }	
  	
  
	
  	
  	
  	
  }	
  	
  
}	
  	
  
	
  
Coun7ngActor.start()	
  
Actors
import	
  scala.actors.Actor._	
  	
  
	
  
val	
  echoActor	
  =	
  actor	
  {	
  
	
  	
  	
  	
  while	
  (true)	
  {	
  
	
  	
  	
  	
  	
  	
  	
  	
  receive	
  {	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  case	
  msg	
  =>	
  println("received:	
  ”	
  +	
  msg)	
  
	
  	
  	
  	
  	
  	
  	
  	
  }	
  
	
  	
  	
  	
  }	
  
}	
  
echoActor	
  !	
  "hello"	
  	
  
echoActor	
  !	
  "world!"	
  	
  
Futures
	
  
Return a Future immediately, run func in new thread
	
  
scala>	
  future	
  {	
  Thread.sleep(10000);	
  println("hi");	
  10	
  }	
  
res2:	
  scala.actors.Future[Int]	
  =	
  <	
  func7on0>	
  
	
  
	
  	
  
Use the Future apply()	
  method to get the result
	
  
scala>	
  res2()	
  	
  	
  	
  	
  //	
  blocks	
  wai7ng	
  for	
  sleep()	
  to	
  finish	
  
hi	
  
res3:	
  Int	
  =	
  10	
  
Actors / Futures / STM
•  Akka provides more robust Actors and
   Futures
•  Also provides
  –  Distributed (Remote) Actors
  –  Software Transactional Memory
  –  Java API
Parallel Collections
 •  Add .par to collection to get parallel
    version
 •  Uses JDK7 fork-join framework
 •  Example:

myData.par.filter(_.expensiveTest()).map(_.expensiveComputa7on())	
  

     –  Filter is run in parallel, results are collected,
        then map is run in parallel
Interoperability with Java
•  Scala classes are Java classes
•  You can pass Scala objects to Java
   methods and vice-versa
•  For the most part, seamless interop
  –  Cannot use Scala-only features from Java
Java Interop Example
@En7ty	
  
class	
  Subscrip7onEvent	
  {	
  
	
  	
  	
  	
  @Id	
  @GeneratedValue	
  
	
  	
  	
  	
  var	
  id:	
  Long	
  =	
  _	
  
	
  
	
  	
  	
  	
  @ManyToOne(op7onal=false)	
  
	
  	
  	
  	
  var	
  subscrip7on:	
  Subscrip7on	
  =	
  _	
  
	
  
	
  	
  	
  	
  var	
  address:	
  String	
  =	
  _	
  
	
  
	
  	
  	
  	
  @Index(name="Subscrip7onEventStatus")	
  
	
  	
  	
  	
  private	
  var	
  status:	
  String	
  =	
  _	
  
	
  	
  	
  	
  def	
  deliveryStatus	
  =	
  DeliveryStatus.withName(status)	
  
	
  	
  	
  	
  def	
  deliveryStatus_=(s:	
  DeliveryStatus)	
  {	
  status	
  =	
  s.toString	
  }	
  
}	
  
Java Interop Example
@Controller	
  
@RequestMapping(Array("/report"))	
  
class	
  ReportController	
  {	
  
	
  
	
  	
  class	
  MessageDto(message:	
  Message)	
  {	
  
	
  	
  	
  	
  @BeanProperty	
  val	
  id	
  =	
  message.id	
  
	
  	
  	
  	
  @BeanProperty	
  val	
  address	
  =	
  message.address	
  
	
  	
  	
  	
  //	
  …	
  
	
  	
  }	
  	
  	
  	
  	
  
	
  	
  
	
  	
  @RequestMapping(Array("/messages"))	
  
	
  	
  def	
  messages(@RequestParam(value="fromDate”)	
  from:	
  String,	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  map:	
  ExtendedModelMap):	
  String	
  =	
  {	
  	
  	
  
	
  	
  	
  	
  	
  	
  	
  //…	
  
	
  	
  	
  	
  	
  	
  	
  map.put(“messages”,	
  asJavaCollec7on(messageDtos))	
  
	
  
	
  	
  	
  	
  	
  	
  	
  “report/messages”	
  	
  	
  	
  	
  	
  	
  
}	
  
	
  
JavaConversions
•  Add Scala collection API methods to Java
   collections
                	
  import	
  collec7on.JavaConversions._	
  
	
  	
  	
  	
  	
  import	
  collec7on.Iterable	
  
	
  	
  	
  	
  	
  import	
  java.u7l.{List=>JList}	
  
	
  
                	
  def	
  goodStudents(students:	
  JList[Student]):	
  Iterable[String]	
  =	
  
                	
  	
  	
  	
  	
  	
  students.filter(_.score	
  >	
  5).map(_.name)	
  
	
  
	
  
Named and Default Params
•  Named parameters
 def	
  resize(width:	
  Int,	
  height:	
  Int)	
  =	
  {	
  ...	
  }	
  
 resize(width	
  =	
  120,	
  height	
  =	
  42)	
  

•  Default parameters
 def	
  f(elems:	
  List[Int],	
  x:	
  Int	
  =	
  0,	
  cond:	
  Boolean	
  =	
  true)	
  
 f(List(1))	
  
 f(Nil,	
  cond	
  =	
  false)	
  
By-name Parameters
•  Method parameters can be lazily evaluated

          class	
  Logger	
  {	
  
          	
  	
  	
  	
  def	
  debug(msg:	
  =>	
  String)	
  {	
  
                          	
  	
  	
  	
  	
  if	
  (isDebug)	
  doLog(DEBUG,	
  msg)	
  
          	
  	
  	
  	
  }	
  
          }	
  
          	
  
          log.debug(“this	
  “	
  +	
  “	
  is	
  “	
  +	
  “expensive”)	
  
   	
  
Type Conveniences
•  Type Aliases
          type	
  MyMap	
  =	
  	
  
          	
  	
  	
  	
  mutable.HashMap[String,	
  mutable.HashMap[String,	
  Int]]	
  
   	
  
•  Import Aliases
	
  	
  	
  	
  	
  	
  import	
  com.nyx.domain.no7fica7on.{Topic=>DomainTopic}	
  	
  
Mixins
•  Multiple implementation inheritance
	
  
trait	
  UserIden7fierCmd	
  extends	
  ApiKeyCmd	
  {	
  	
  
	
  	
  	
  	
  var	
  userId:	
  String	
  =	
  _	
  
	
  	
  	
  	
  def	
  getUser	
  =	
  {…}	
  
}	
  
	
  
trait	
  RoleIdCmd	
  extends	
  ApiKeyCmd	
  {	
  var…	
  	
  def…	
  }	
  
	
  
object	
  cmd	
  extends	
  UserIden7fierCmd	
  with	
  RoleIdCmd	
  {..}	
  
Duck Typing
type	
  Closeable	
  =	
  {	
  def	
  close():	
  Unit	
  }	
  
	
  
def	
  using[T	
  <:	
  Closeable,	
  S]	
  (obj:	
  T)(func:	
  T	
  =>	
  S):	
  S	
  =	
  {	
  	
  
	
  	
  	
  	
  val	
  result	
  =	
  func	
  (obj)	
  	
  
	
  	
  	
  	
  obj.close()	
  	
  
	
  	
  	
  	
  result	
  
}	
  
	
  
val	
  fis	
  =	
  new	
  FileInputStream(“data.txt”)	
  
	
  
using(fis)	
  {	
  f	
  =>	
  
	
  	
  	
  	
  while	
  (f.read()	
  !=	
  -­‐1)	
  {}	
  
}	
  
	
  
More Information
•  My Scala Links gist
  –  https://gist.github.com/1249298

More Related Content

What's hot

Programming in Scala: Notes
Programming in Scala: NotesProgramming in Scala: Notes
Programming in Scala: NotesRoberto Casadei
 
SE 20016 - programming languages landscape.
SE 20016 - programming languages landscape.SE 20016 - programming languages landscape.
SE 20016 - programming languages landscape.Ruslan Shevchenko
 
Scala introduction
Scala introductionScala introduction
Scala introductionvito jeng
 
Introduction to Functional Programming with Scala
Introduction to Functional Programming with ScalaIntroduction to Functional Programming with Scala
Introduction to Functional Programming with Scalapramode_ce
 
Few simple-type-tricks in scala
Few simple-type-tricks in scalaFew simple-type-tricks in scala
Few simple-type-tricks in scalaRuslan Shevchenko
 
Ankara Jug - Practical Functional Programming with Scala
Ankara Jug - Practical Functional Programming with ScalaAnkara Jug - Practical Functional Programming with Scala
Ankara Jug - Practical Functional Programming with ScalaEnsar Basri Kahveci
 
Scala collections api expressivity and brevity upgrade from java
Scala collections api  expressivity and brevity upgrade from javaScala collections api  expressivity and brevity upgrade from java
Scala collections api expressivity and brevity upgrade from javaIndicThreads
 
Introduction to Scala for Java Developers
Introduction to Scala for Java DevelopersIntroduction to Scala for Java Developers
Introduction to Scala for Java DevelopersMichael Galpin
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdfHiroshi Ono
 
Scaladroids: Developing Android Apps with Scala
Scaladroids: Developing Android Apps with ScalaScaladroids: Developing Android Apps with Scala
Scaladroids: Developing Android Apps with ScalaOstap Andrusiv
 
Scala at HUJI PL Seminar 2008
Scala at HUJI PL Seminar 2008Scala at HUJI PL Seminar 2008
Scala at HUJI PL Seminar 2008Yardena Meymann
 
Scala @ TechMeetup Edinburgh
Scala @ TechMeetup EdinburghScala @ TechMeetup Edinburgh
Scala @ TechMeetup EdinburghStuart Roebuck
 
Starting with Scala : Frontier Developer's Meetup December 2010
Starting with Scala : Frontier Developer's Meetup December 2010Starting with Scala : Frontier Developer's Meetup December 2010
Starting with Scala : Frontier Developer's Meetup December 2010Derek Chen-Becker
 
Scala Back to Basics: Type Classes
Scala Back to Basics: Type ClassesScala Back to Basics: Type Classes
Scala Back to Basics: Type ClassesTomer Gabel
 

What's hot (20)

Programming in Scala: Notes
Programming in Scala: NotesProgramming in Scala: Notes
Programming in Scala: Notes
 
SE 20016 - programming languages landscape.
SE 20016 - programming languages landscape.SE 20016 - programming languages landscape.
SE 20016 - programming languages landscape.
 
Scala introduction
Scala introductionScala introduction
Scala introduction
 
Introduction to Functional Programming with Scala
Introduction to Functional Programming with ScalaIntroduction to Functional Programming with Scala
Introduction to Functional Programming with Scala
 
Few simple-type-tricks in scala
Few simple-type-tricks in scalaFew simple-type-tricks in scala
Few simple-type-tricks in scala
 
Scala jargon cheatsheet
Scala jargon cheatsheetScala jargon cheatsheet
Scala jargon cheatsheet
 
Scala coated JVM
Scala coated JVMScala coated JVM
Scala coated JVM
 
Scalaz
ScalazScalaz
Scalaz
 
Scala on Android
Scala on AndroidScala on Android
Scala on Android
 
Ankara Jug - Practical Functional Programming with Scala
Ankara Jug - Practical Functional Programming with ScalaAnkara Jug - Practical Functional Programming with Scala
Ankara Jug - Practical Functional Programming with Scala
 
Scala collections api expressivity and brevity upgrade from java
Scala collections api  expressivity and brevity upgrade from javaScala collections api  expressivity and brevity upgrade from java
Scala collections api expressivity and brevity upgrade from java
 
Introduction to Scala for Java Developers
Introduction to Scala for Java DevelopersIntroduction to Scala for Java Developers
Introduction to Scala for Java Developers
 
camel-scala.pdf
camel-scala.pdfcamel-scala.pdf
camel-scala.pdf
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdf
 
Scaladroids: Developing Android Apps with Scala
Scaladroids: Developing Android Apps with ScalaScaladroids: Developing Android Apps with Scala
Scaladroids: Developing Android Apps with Scala
 
Scala at HUJI PL Seminar 2008
Scala at HUJI PL Seminar 2008Scala at HUJI PL Seminar 2008
Scala at HUJI PL Seminar 2008
 
Scala @ TechMeetup Edinburgh
Scala @ TechMeetup EdinburghScala @ TechMeetup Edinburgh
Scala @ TechMeetup Edinburgh
 
Starting with Scala : Frontier Developer's Meetup December 2010
Starting with Scala : Frontier Developer's Meetup December 2010Starting with Scala : Frontier Developer's Meetup December 2010
Starting with Scala : Frontier Developer's Meetup December 2010
 
Scala for curious
Scala for curiousScala for curious
Scala for curious
 
Scala Back to Basics: Type Classes
Scala Back to Basics: Type ClassesScala Back to Basics: Type Classes
Scala Back to Basics: Type Classes
 

Viewers also liked

Scala in the Enterprise
Scala in the EnterpriseScala in the Enterprise
Scala in the EnterpriseEric Pederson
 
2004 Survey of Experiences with Primary Health Care in US, UK, CA, AU, NZ
2004 Survey of Experiences with Primary Health Care in US, UK, CA, AU, NZ2004 Survey of Experiences with Primary Health Care in US, UK, CA, AU, NZ
2004 Survey of Experiences with Primary Health Care in US, UK, CA, AU, NZEric Pederson
 
Learn BEM: CSS Naming Convention
Learn BEM: CSS Naming ConventionLearn BEM: CSS Naming Convention
Learn BEM: CSS Naming ConventionIn a Rocket
 
Lightning Talk #9: How UX and Data Storytelling Can Shape Policy by Mika Aldaba
Lightning Talk #9: How UX and Data Storytelling Can Shape Policy by Mika AldabaLightning Talk #9: How UX and Data Storytelling Can Shape Policy by Mika Aldaba
Lightning Talk #9: How UX and Data Storytelling Can Shape Policy by Mika Aldabaux singapore
 
How to Build a Dynamic Social Media Plan
How to Build a Dynamic Social Media PlanHow to Build a Dynamic Social Media Plan
How to Build a Dynamic Social Media PlanPost Planner
 
SEO: Getting Personal
SEO: Getting PersonalSEO: Getting Personal
SEO: Getting PersonalKirsty Hulse
 

Viewers also liked (7)

Scala in the Enterprise
Scala in the EnterpriseScala in the Enterprise
Scala in the Enterprise
 
2004 Survey of Experiences with Primary Health Care in US, UK, CA, AU, NZ
2004 Survey of Experiences with Primary Health Care in US, UK, CA, AU, NZ2004 Survey of Experiences with Primary Health Care in US, UK, CA, AU, NZ
2004 Survey of Experiences with Primary Health Care in US, UK, CA, AU, NZ
 
Learn BEM: CSS Naming Convention
Learn BEM: CSS Naming ConventionLearn BEM: CSS Naming Convention
Learn BEM: CSS Naming Convention
 
Lightning Talk #9: How UX and Data Storytelling Can Shape Policy by Mika Aldaba
Lightning Talk #9: How UX and Data Storytelling Can Shape Policy by Mika AldabaLightning Talk #9: How UX and Data Storytelling Can Shape Policy by Mika Aldaba
Lightning Talk #9: How UX and Data Storytelling Can Shape Policy by Mika Aldaba
 
How to Build a Dynamic Social Media Plan
How to Build a Dynamic Social Media PlanHow to Build a Dynamic Social Media Plan
How to Build a Dynamic Social Media Plan
 
SEO: Getting Personal
SEO: Getting PersonalSEO: Getting Personal
SEO: Getting Personal
 
Succession “Losers”: What Happens to Executives Passed Over for the CEO Job?
Succession “Losers”: What Happens to Executives Passed Over for the CEO Job? Succession “Losers”: What Happens to Executives Passed Over for the CEO Job?
Succession “Losers”: What Happens to Executives Passed Over for the CEO Job?
 

Similar to Intro to Scala for Java Devs: Concise Overview of Scala Features for Java Developers

The Scala Programming Language
The Scala Programming LanguageThe Scala Programming Language
The Scala Programming Languageleague
 
Short intro to scala and the play framework
Short intro to scala and the play frameworkShort intro to scala and the play framework
Short intro to scala and the play frameworkFelipe
 
Scala uma poderosa linguagem para a jvm
Scala   uma poderosa linguagem para a jvmScala   uma poderosa linguagem para a jvm
Scala uma poderosa linguagem para a jvmIsaias Barroso
 
Functional programming with Scala
Functional programming with ScalaFunctional programming with Scala
Functional programming with ScalaNeelkanth Sachdeva
 
Functional Programming With Scala
Functional Programming With ScalaFunctional Programming With Scala
Functional Programming With ScalaKnoldus Inc.
 
BCS SPA 2010 - An Introduction to Scala for Java Developers
BCS SPA 2010 - An Introduction to Scala for Java DevelopersBCS SPA 2010 - An Introduction to Scala for Java Developers
BCS SPA 2010 - An Introduction to Scala for Java DevelopersMiles Sabin
 
An Introduction to Scala for Java Developers
An Introduction to Scala for Java DevelopersAn Introduction to Scala for Java Developers
An Introduction to Scala for Java DevelopersMiles Sabin
 
Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?Jesper Kamstrup Linnet
 
Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Martin Odersky
 
Scala for the doubters
Scala for the doubtersScala for the doubters
Scala for the doubtersMax Klyga
 
Stepping Up : A Brief Intro to Scala
Stepping Up : A Brief Intro to ScalaStepping Up : A Brief Intro to Scala
Stepping Up : A Brief Intro to ScalaDerek Chen-Becker
 
Scala: Object-Oriented Meets Functional, by Iulian Dragos
Scala: Object-Oriented Meets Functional, by Iulian DragosScala: Object-Oriented Meets Functional, by Iulian Dragos
Scala: Object-Oriented Meets Functional, by Iulian Dragos3Pillar Global
 
(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?Tomasz Wrobel
 
Scala and jvm_languages_praveen_technologist
Scala and jvm_languages_praveen_technologistScala and jvm_languages_praveen_technologist
Scala and jvm_languages_praveen_technologistpmanvi
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdfHiroshi Ono
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdfHiroshi Ono
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdfHiroshi Ono
 

Similar to Intro to Scala for Java Devs: Concise Overview of Scala Features for Java Developers (20)

The Scala Programming Language
The Scala Programming LanguageThe Scala Programming Language
The Scala Programming Language
 
Short intro to scala and the play framework
Short intro to scala and the play frameworkShort intro to scala and the play framework
Short intro to scala and the play framework
 
Scala uma poderosa linguagem para a jvm
Scala   uma poderosa linguagem para a jvmScala   uma poderosa linguagem para a jvm
Scala uma poderosa linguagem para a jvm
 
Functional programming with Scala
Functional programming with ScalaFunctional programming with Scala
Functional programming with Scala
 
Scala in a nutshell by venkat
Scala in a nutshell by venkatScala in a nutshell by venkat
Scala in a nutshell by venkat
 
Functional Programming With Scala
Functional Programming With ScalaFunctional Programming With Scala
Functional Programming With Scala
 
BCS SPA 2010 - An Introduction to Scala for Java Developers
BCS SPA 2010 - An Introduction to Scala for Java DevelopersBCS SPA 2010 - An Introduction to Scala for Java Developers
BCS SPA 2010 - An Introduction to Scala for Java Developers
 
An Introduction to Scala for Java Developers
An Introduction to Scala for Java DevelopersAn Introduction to Scala for Java Developers
An Introduction to Scala for Java Developers
 
Scala - core features
Scala - core featuresScala - core features
Scala - core features
 
Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?
 
Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009
 
Scala ntnu
Scala ntnuScala ntnu
Scala ntnu
 
Scala for the doubters
Scala for the doubtersScala for the doubters
Scala for the doubters
 
Stepping Up : A Brief Intro to Scala
Stepping Up : A Brief Intro to ScalaStepping Up : A Brief Intro to Scala
Stepping Up : A Brief Intro to Scala
 
Scala: Object-Oriented Meets Functional, by Iulian Dragos
Scala: Object-Oriented Meets Functional, by Iulian DragosScala: Object-Oriented Meets Functional, by Iulian Dragos
Scala: Object-Oriented Meets Functional, by Iulian Dragos
 
(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?
 
Scala and jvm_languages_praveen_technologist
Scala and jvm_languages_praveen_technologistScala and jvm_languages_praveen_technologist
Scala and jvm_languages_praveen_technologist
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdf
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdf
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdf
 

Recently uploaded

Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 

Recently uploaded (20)

Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 

Intro to Scala for Java Devs: Concise Overview of Scala Features for Java Developers

  • 1. Intro to Scala for Java Devs Sungard– 4/17/2012
  • 2. Intro •  Eric Pederson –  eric@cpextechnology.com –  Twitter @ericacm –  Sourcedelica.com/blog –  Background in Java, Groovy, Javascript, PHP, etc. •  Using Scala for last two years •  Two Scala apps developed for NYSE in Production
  • 3. Platform at NYSE •  Scala 2.9.1 •  JDK 1.6 •  Tomcat / JBoss •  Maven •  Using lots of Java libraries –  Spring, Hibernate, CXF, Mule, ApacheMQ –  Bouncycastle, OpenSAML, Velocity, etc, etc.
  • 4. What is Scala? •  Hybrid Object-Functional language •  Statically typed •  Developed by Martin Odersky –  Java Generics –  Java Compiler (1.3+) •  First release in 2003
  • 5. What is Scala? •  Designed for general purpose programming •  Performance on par with Java* •  Scalable –  Designed to write programs ranging from scripts up to huge systems –  You don’t have to use all of the features to be productive *  There  are  some  gotchas  you  have  to  watch  out  for  
  • 6. Why Should I Use Scala? •  Allows you to write very concise code –  Productivity on the level of Groovy / Ruby •  Concurrency-ready •  Excellent interoperability with Java code •  Lots of other reasons…
  • 7. Conciseness •  Code size reduced by 2-3x compared to Java •  Less code == easier to read •  Less code == fewer bugs
  • 8. Conciseness •  Type Inference •  Expressions, not statements •  Higher-ordered functions •  Case classes •  Pattern matching
  • 9. Type Inference •  Variables val  subscrip7onEvents  =  foo()     •  Method return types def  listOfPeople  =  List(“Paul”,  “Eric”,  “John”,  “Mar7n”)     •  Generic type parameters case  class  MyPair[A,  B](x:  A,  y:  B)   val  p  =  MyPair(1,  “foo”)          //  p  is  MyPair[Int,  String]  
  • 10. Type inference •  Java HashMap<String,  Customer>  customers  =                  new  HashMap<String,  Customer>();   customers.add("id1",        new  Customer("Eric",  "917-­‐444-­‐1234");   customers.add("id2",        new  Customer("Paul",  "718-­‐666-­‐9876");   •  Scala val  customers  =  HashMap(                      "id1"-­‐>Customer("Eric",  "917-­‐434-­‐1852"),                        "id2"-­‐>Customer("Paul",  "718-­‐666-­‐9876"))    
  • 11. Expressions, not statements val  server  =  if  (environment  ==  "development”)  {              val  factory  =  new  MBeanServerFactoryBean              factory.aberProper7esSet()              factory.getObject.asInstanceOf[MbeanServer]   }  else  {              val  clazz  =                            Class.forName("org.jboss.mx.u7l.MBeanServerLocator")              clazz.getMethod("locateJBoss”).invoke(null)   }     //  server is assigned one of the bold values  
  • 12. Expressions, not statements val  n  =  try  {          userInput.toInt   }  catch  {          case  _  =>  0   }     //  n  is  an  Int,  0  if  unable  to  parse  userInput  
  • 13. Collections API •  Very comprehensive –  For example, over 200 methods on List   •  Higher ordered functions –  foreach,  map,  flatMap,  exists,  forall,  find,  findAll,   filter,  groupBy,  par77on, etc. •  Concise literals •  Immutable and mutable variations
  • 14. Collection Literals •  val  l1  =  List(1,  2,  3)   •  val  m1  =  Map(“name”  -­‐>  “Eric”,  “city”  -­‐>  “NYC”)   •  val  s1  =  Set(Car(“Ford”),  Car(“Isuzu”),  Car(“VW”))  
  • 15. Collections API •  Scala val  groups  =      subscrip7onEvents.groupBy(e  =>  e.subscrip7on.id)     •  Java Map<String,  List<SubscripLonEvent>>  groups  =            new  HashMap<String,  ArrayList<Subscrip7onEvent>();   for  (Subscrip7onEvent  se  :  subscrip7onEvents)  {          ArrayList<Subscrip7onEvent>  seList  =  groups.get(se.subscrip7on.id)          if  (seList  ==  null)  {                  seList  =  new  ArrayList<Subscrip7onEvent>();                  groups.put(se.subscrip7on.id,  seList)          }          seList.add(se);   }    
  • 16. Count characters in documents Try #1 – Java-esque Scala        var  total  =  0          for  (doc  <-­‐  docs)  {              total  +=  doc.length          }   Try #2 – Use higher order functions        var  total  =  0          docs.foreach(doc  =>  total  +=  doc.length)  
  • 17. Count characters in documents Try #3 – Use fold docs.foldLeb(0)((accum,  current)  =>  accum  +  current.length)           Try #4 – Use type-classes (eg. Numeric)        docs.map(_.length).sum   Try #5 – Use the 'view' method to turn multiple passes into one        docs.view.map(_.length).sum  
  • 18. Variables scala>  var  i  =  0   i:  Int  =  0     scala>  i  =  2   i:  Int  =  2     scala>  val  j  =  0   j:  Int  =  0     scala>  j  =  3   <console>:8:  error:  reassignment  to  val     scala>  lazy  val  l  =  expensiveComputa7on()   l:  Double  =  <lazy>    
  • 19. Uniform Access Principle scala>  object  Ints  {            |                  var  i  =  1            |                  val  j  =  2            |                  def  k  =  i  +  j            |  }     scala>  Ints.i   res8:  Int  =  1     scala>  Ints.j   res9:  Int  =  2     scala>  Ints.k   res10:  Int  =  3  
  • 20. Uniform Access Principle scala>  object  M  {            |                    private  var  pm  =  0            |                    def  m  =  pm            |                    def  m_=(in:  Int)  {  pm  =  in  }            |  }   scala>  import  M._     scala>  m   res5:  Int  =  0     scala>  m  =  5   m:  Int  =  5    
  • 21. Case Classes •  Scala scala>  case  class  Car(make:  String,  model:  String,  mpg:  Int)   defined  class  Car       scala>    val  c  =  Car("Honda",  "Civic",  40)     c:  Car  =  Car(Honda,Civic,40)  
  • 22. Case Classes •  Java public  class  Car  implements  scala.Product,  scala.Serializable    {          final  private  String  make,  model;          final  private  int  mpg;          Car(String  make,  String  model,  int  mpg)  {                  this.make  =  make;    this.model  =  model;  this.mpg  =  mpg;          }          public  String  getMake()  {  return  make;  }          public  String  getModel()  {  return  model;  }          public  int  getMpg()  {  return  mpg;  }          public  String  toString()  {  return  “Car(  “  +  make  +  ….  }          public  boolean  equals(Object  that)  {  if  (that  instanceOf  Car)  &&  ……  }          public  int  hashCode()  {  return  19  +  ……  }          public  Car  copy(String  make,  String  model,  int  mpg)  {  …..  }          //  plus  9  other  Scala-­‐specific  methods   }      
  • 23. Case Classes •  Case classes can also have mutable fields and methods case  class  Car(make:  String,  model:  String,  mpg:  Int,  var  odometer)  {          def  driveMiles(miles:  Int)  {  odometer  +=  miles  }   }       •  In Scala you can define multiple classes per source file
  • 24. Pattern Matching •  Case Classes // Class hierarchy: trait Expr case class Num(value : int) extends Expr case class Var(name : String) extends Expr case class Mul(left : Expr, right : Expr) extends Expr // Simplification rule: e match { case Mul(x, Num(1)) ⇒ x case _ ⇒ e }
  • 25. Pattern Matching •  Match on constants  def  describe(x:  Any)  =  x  match  {          case  5  =>  "five"          case  true  =>  "truth"          case  "hello"  =>  "hi!”        case  Nil  =>  "the  empty  list"          case  _  =>  "something  else”    }  
  • 26. Pattern Matching •  Typed patterns def  generalSize(x:  Any)  =  x  match  {        case  s:  String  =>  s.length          case  m:  Map[_,  _]  =>  m.size          case  _  =>  -­‐1    }  
  • 27. No Checked Exceptions //  Look  ma,  no  throws  clause!   def  foo()  {          throw  new  java.lang.Excep7on   }  
  • 28. Concurrency-readiness •  The future present is many cores •  Writing thread-safe code in Java is very difficult –  Mostly due to shared, mutable state
  • 29. Concurrency-readiness •  Scala –  Excellent support for immutability –  Actors / Futures –  Parallel collections
  • 30. Immutability •  Case classes •  Immutable collections are default –  Copies of collections share data •  val vs. var, val is encouraged •  Method parameters are vals
  • 31. Actors •  Included in standard Scala library •  Simplified multithreading and coordination •  Based on message passing –  Each actor has a mailbox queue of messages •  Implementation based on Erlang
  • 32. Actors         object  Coun7ngActor  extends  Actor  {            def  act()  {                    for  (i  <-­‐  1  to  10)  {                            println("Number:  "+i)                          Thread.sleep(1000)                    }            }     }       Coun7ngActor.start()  
  • 33. Actors import  scala.actors.Actor._       val  echoActor  =  actor  {          while  (true)  {                  receive  {                          case  msg  =>  println("received:  ”  +  msg)                  }          }   }   echoActor  !  "hello"     echoActor  !  "world!"    
  • 34. Futures   Return a Future immediately, run func in new thread   scala>  future  {  Thread.sleep(10000);  println("hi");  10  }   res2:  scala.actors.Future[Int]  =  <  func7on0>         Use the Future apply()  method to get the result   scala>  res2()          //  blocks  wai7ng  for  sleep()  to  finish   hi   res3:  Int  =  10  
  • 35. Actors / Futures / STM •  Akka provides more robust Actors and Futures •  Also provides –  Distributed (Remote) Actors –  Software Transactional Memory –  Java API
  • 36. Parallel Collections •  Add .par to collection to get parallel version •  Uses JDK7 fork-join framework •  Example: myData.par.filter(_.expensiveTest()).map(_.expensiveComputa7on())   –  Filter is run in parallel, results are collected, then map is run in parallel
  • 37. Interoperability with Java •  Scala classes are Java classes •  You can pass Scala objects to Java methods and vice-versa •  For the most part, seamless interop –  Cannot use Scala-only features from Java
  • 38. Java Interop Example @En7ty   class  Subscrip7onEvent  {          @Id  @GeneratedValue          var  id:  Long  =  _            @ManyToOne(op7onal=false)          var  subscrip7on:  Subscrip7on  =  _            var  address:  String  =  _            @Index(name="Subscrip7onEventStatus")          private  var  status:  String  =  _          def  deliveryStatus  =  DeliveryStatus.withName(status)          def  deliveryStatus_=(s:  DeliveryStatus)  {  status  =  s.toString  }   }  
  • 39. Java Interop Example @Controller   @RequestMapping(Array("/report"))   class  ReportController  {        class  MessageDto(message:  Message)  {          @BeanProperty  val  id  =  message.id          @BeanProperty  val  address  =  message.address          //  …      }                  @RequestMapping(Array("/messages"))      def  messages(@RequestParam(value="fromDate”)  from:  String,                                                              map:  ExtendedModelMap):  String  =  {                    //…                map.put(“messages”,  asJavaCollec7on(messageDtos))                  “report/messages”               }    
  • 40. JavaConversions •  Add Scala collection API methods to Java collections  import  collec7on.JavaConversions._            import  collec7on.Iterable            import  java.u7l.{List=>JList}      def  goodStudents(students:  JList[Student]):  Iterable[String]  =              students.filter(_.score  >  5).map(_.name)      
  • 41. Named and Default Params •  Named parameters def  resize(width:  Int,  height:  Int)  =  {  ...  }   resize(width  =  120,  height  =  42)   •  Default parameters def  f(elems:  List[Int],  x:  Int  =  0,  cond:  Boolean  =  true)   f(List(1))   f(Nil,  cond  =  false)  
  • 42. By-name Parameters •  Method parameters can be lazily evaluated class  Logger  {          def  debug(msg:  =>  String)  {            if  (isDebug)  doLog(DEBUG,  msg)          }   }     log.debug(“this  “  +  “  is  “  +  “expensive”)    
  • 43. Type Conveniences •  Type Aliases type  MyMap  =            mutable.HashMap[String,  mutable.HashMap[String,  Int]]     •  Import Aliases            import  com.nyx.domain.no7fica7on.{Topic=>DomainTopic}    
  • 44. Mixins •  Multiple implementation inheritance   trait  UserIden7fierCmd  extends  ApiKeyCmd  {            var  userId:  String  =  _          def  getUser  =  {…}   }     trait  RoleIdCmd  extends  ApiKeyCmd  {  var…    def…  }     object  cmd  extends  UserIden7fierCmd  with  RoleIdCmd  {..}  
  • 45. Duck Typing type  Closeable  =  {  def  close():  Unit  }     def  using[T  <:  Closeable,  S]  (obj:  T)(func:  T  =>  S):  S  =  {            val  result  =  func  (obj)            obj.close()            result   }     val  fis  =  new  FileInputStream(“data.txt”)     using(fis)  {  f  =>          while  (f.read()  !=  -­‐1)  {}   }    
  • 46. More Information •  My Scala Links gist –  https://gist.github.com/1249298