Scala introduction

  • 988 views
Uploaded on

for Sayeret Lambda January 2011 meeting

for Sayeret Lambda January 2011 meeting

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
988
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
16
Comments
0
Likes
4

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Introduction http://www.scala-lang.org Sayeret Lambda January 2011 Yardena Meymann
  • 2. What is Scala
    • Object oriented and functional
    • Statically typed - advanced type system
    • Compiled to JVM bytecode
      • but also to CLR, and to JavaScript (in progress)
    • High performance
    • Very good interoperability with Java
    • Support for modularity and extensibility
      • DSL friendly
  • 3. History
    • 1995
    • Sun and Netscape team up and announce first beta release of Java
  • 4. History
    • 1995
    • Philip Wadler tells Martin Odersky about Java.
    • Odersky decides to learn the language by writing a compiler
  • 5. History
    • 1999
    • Sun buys Java compiler from Martin Odersky, it is shipped with JDK 1.3
    OUR JAVA COMPILER ISN’T GOOD ENOUGH I KNOW THIS GUY IN SWITZERLAND, HE WROTE A GREAT JAVA COMPILER !
  • 6. History
    • 1999
    • Work on adding generics to Java begins
  • 7. History
    • 2001
    • Martin Odersky starts
    • working on Scala at
  • 8. Then and Now
    • 2003 Scala 1.0
    • 2006 Scala 2.0
    • 2010 Scala 2.8
    “ If I were to pick a language to use today other than Java, it would be Scala ” James Gosling
  • 9. Now
    • Open source projects in Scala:
      • Akka, Lift, Specs, ScalaCheck, Scalaz, ScalaQuery, …
    • Books:
    • Tools:
      • built-in REPL, Eclipse, IntelliJ and NetBeans plug-ins
      • integration modules for almost every popular Java framework
  • 10. Now
    • Used in industry:
      • LinkedIn, Twitter, Novell, EDF, The Guardian, Xebia, Xerox, Sony, Siemens, …
    • Jobs:
  • 11. Influences on Scala Design
    • Java, C#
      • syntax, basic types, and class libraries,
    • Smalltalk
      • uniform object model,
    • Eiffel
      • uniform access principle,
    • Beta
      • systematic nesting,
    • ML, Haskell
      • many of the functional aspects.
  • 12. Imagine
  • 13. No Primitives
    • Everything is an object
  • 14. No Operators (almost)
    • Everything is a method
    1 + 2 1.+(2) 123 toString 123.toString()
  • 15. No Noise
    • Semicolon inference
    • Type inference
    • Empty and single line blocks don’t need { }
    • Infix notation for method invocations
    • Implicit public
    • Empty parameter lists don’t need ()
  • 16. No Statements
    • Everything is an expression
    • Return is optional, last expression is returned
      • def plus2(x:Int) = x + 2
    • for , while , if/else are expressions
    • Unit equivalent of void
  • 17. No final
    • val and var , memoization
    def foo() { val x = “immutable” var y = “mutable” lazy val z = “lazy” }
  • 18. No Checked Exceptions
    • All exceptions are runtime
    • Also, you have Option[T] and Either[A,B] that can be used instead of checked exceptions
  • 19. No Statics
    • And no “manual” singletons
    • Instead we have object – companion class
    object HelloWorld { def main(args: Array[String]) = println("Hello, world!") }
  • 20. First-class Functions val romanNumeral = Map(1 -> "I", 2 -> "II", 3 -> "III", 4 -> "IV", 5 -> "V") An object with apply method can be used as a function val inc = (x: Int) => x + 1 inc(1)  2
  • 21. No “Java Bean” Boilerplate Java public class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } public void setName(String name) { this.name = name; } public void setAge(age: int) { this.age = age; } } Scala class Person( var name: String, var age: Int)
  • 22. Case Classes
    • Also generates equals, hashCode and allows decomposition via pattern matching
    abstract class Term case class Var(name: String) extends Term case class Fun(arg: String, body: Term) extends Term case class App(f: Term, v: Term) extends Term
  • 23. No Switch – Pattern Matching
    • Extractor objects are auto-generated for case classes
    def printTerm(term: Term) { term match { case Var(n) => print(n) case Fun(x, b) => print("^" + x + ".") ; printTerm(b) case App(f, v) => Console.print("(");printTerm(f); print(" "); printTerm(v); print(")") } } def isIdentityFun(term: Term): Boolean = term match { case Fun(x, Var(y)) if x == y => true case _ => false }
  • 24. Functions and Closures
    • With some syntax sugar:
    List(1, 2, 3).map((x: Int) => x + 1)  List(2, 3, 4) List(1, 2, 3).map(x => x + 1) List(1, 2, 3).map(_ + 1) Closures: var more = 7 val addMore = (x: Int) => x + more  10
  • 25. Lists
    • val list = 1 :: 2 :: 3 :: Nil
    • list. head  1
    • list. tail  List(2, 3)
    • list. map (_ + 1)  List(2, 3, 4)
    • list. filter (_ < 2)  List(3)
    • list. exists (_ == 3)  true
    • list. drop (2)  List(3)
    • list. reverse  List(3, 2, 1)
    • list. sort (_ > _)  List(3, 2, 1)
    • List. flatten (list)  List(1, 2, 3)
    • list. slice (2, 3)  List(3)
    • ...
  • 26. Tuples
    • def getNameAndAge: Tuple2[String, Int] = {
    • val name = ...
    • val age = ...
    • ( name, age )
    • }
    • val ( name, age ) = getNameAndAge
    • println(“Name: “ + name)
    • println(“Age: “ + age)
  • 27. For Comprehensions
    • Find all attendees named Fred that speaks Danish
    for { att <- attendees if att.name == “Fred” lang <- att.spokenLanguages if lang == “Danish” } yield att
  • 28. No Interfaces
    • Trait s for mixin composition
    trait Dad { private var children: List[Child] = Nil def addChild(child: Child) = children = child :: children def getChildren = children.clone } class Man( val name: String) extends Human val jonas = new Man(“Jonas”) with Dad jonas.addChild( new Child(“Jacob”)) class Man( val name: String) extends Human with Dad
  • 29. Traits for Multiple Inheritance val order = new Order(customer) with Entity with InventoryItemSet with Invoicable with PurchaseLimiter with MailNotifier with ACL with Versioned with Transactional
  • 30. Traits – Stackable Modification
    • trait IgnoreCaseSet extends java.util.Set[String] {
    • abstract override def add(e: String) = {
    • super. add(e.toLowerCase)
    • }
    • abstract override def contains(e: String) = {
    • super. contains(e.toLowerCase)
    • }
    • abstract override def remove(e: String) = {
    • super. remove(e.toLowerCase)
    • }
    • }
  • 31. Many More…
    • Far more powerful generics
    • Self types
    • High-order types
    • Structural and dependent types
    • Declaration-site variance and existential types support
    • Views (implicit conversions) and implicit arguments
  • 32. Many More…
    • Currying, partial functions
    • Continuations
    • Built-in XML support
  • 33. This presentation is based on
    • Pragmatic Real-World Scala
      • by Jonas Bonér
      • http://www.slideshare.net/jboner/pragmatic-real-world-scala-45-min-presentation