Introduction http://www.scala-lang.org Sayeret Lambda January 2011 Yardena Meymann
What is Scala <ul><li>Object oriented and functional </li></ul><ul><li>Statically typed - advanced type system </li></ul><...
History <ul><li>1995 </li></ul><ul><li>Sun and Netscape team up and announce first beta release of Java </li></ul>
History <ul><li>1995 </li></ul><ul><li>Philip Wadler tells Martin Odersky about Java.  </li></ul><ul><li>Odersky decides t...
History <ul><li>1999 </li></ul><ul><li>Sun buys Java compiler from Martin Odersky, it is shipped with JDK 1.3 </li></ul>OU...
History <ul><li>1999 </li></ul><ul><li>Work on adding generics to Java begins </li></ul>
History <ul><li>2001 </li></ul><ul><li>Martin Odersky starts  </li></ul><ul><li>working on Scala at </li></ul>
Then and Now <ul><li>2003  Scala 1.0 </li></ul><ul><li>2006  Scala 2.0 </li></ul><ul><li>2010  Scala 2.8 </li></ul>“ If I ...
Now <ul><li>Open source projects in Scala:  </li></ul><ul><ul><li>Akka, Lift, Specs, ScalaCheck, Scalaz, ScalaQuery, … </l...
Now <ul><li>Used in industry:  </li></ul><ul><ul><li>LinkedIn, Twitter, Novell, EDF, The Guardian, Xebia, Xerox, Sony, Sie...
Influences on Scala Design <ul><li>Java, C#  </li></ul><ul><ul><li>syntax, basic types, and class libraries, </li></ul></u...
Imagine
No Primitives <ul><li>Everything is an object </li></ul>
No Operators (almost) <ul><li>Everything is a method </li></ul>1 + 2 1.+(2) 123 toString 123.toString()
No Noise <ul><li>Semicolon inference </li></ul><ul><li>Type inference </li></ul><ul><li>Empty and single line blocks don’t...
No Statements <ul><li>Everything is an expression </li></ul><ul><li>Return is optional, last expression is returned </li><...
No final <ul><li>val  and  var , memoization </li></ul>def  foo() { val  x = “immutable” var  y = “mutable” lazy   val  z ...
No Checked Exceptions <ul><li>All exceptions are  runtime </li></ul><ul><li>Also, you have  Option[T]  and  Either[A,B]  t...
No Statics <ul><li>And no “manual” singletons </li></ul><ul><li>Instead we have  object  – companion class </li></ul>objec...
First-class Functions val romanNumeral = Map(1 -> &quot;I&quot;, 2 -> &quot;II&quot;,  3 -> &quot;III&quot;, 4 -> &quot;IV...
No “Java Bean” Boilerplate Java public class Person { private String name; private int age; public Person(String name, int...
Case Classes <ul><li>Also generates equals, hashCode and allows decomposition via pattern matching </li></ul>abstract clas...
No Switch – Pattern Matching <ul><li>Extractor objects are auto-generated for case classes </li></ul>def  printTerm(term: ...
Functions and Closures <ul><li>With some syntax sugar: </li></ul>List(1, 2, 3).map((x: Int) => x + 1)    List(2, 3, 4) Li...
Lists <ul><li>val  list = 1 :: 2 :: 3 :: Nil </li></ul><ul><li>list. head      1 </li></ul><ul><li>list. tail     List(2...
Tuples <ul><li>def  getNameAndAge: Tuple2[String, Int] = { </li></ul><ul><li>val  name = ... </li></ul><ul><li>val  age = ...
For Comprehensions <ul><li>Find all attendees named Fred that speaks Danish </li></ul>for { att <- attendees if  att.name ...
No Interfaces <ul><li>Trait s for mixin composition  </li></ul>trait  Dad { private var  children: List[Child] = Nil def  ...
Traits for Multiple Inheritance val  order =  new  Order(customer) with  Entity with  InventoryItemSet with  Invoicable wi...
Traits – Stackable Modification <ul><li>trait  IgnoreCaseSet  extends  java.util.Set[String] { </li></ul><ul><li>abstract ...
Many More… <ul><li>Far more powerful generics </li></ul><ul><li>Self types </li></ul><ul><li>High-order types </li></ul><u...
Many More… <ul><li>Currying, partial functions </li></ul><ul><li>Continuations </li></ul><ul><li>Built-in XML support </li...
This presentation is based on <ul><li>Pragmatic Real-World Scala </li></ul><ul><ul><li>by Jonas Bonér </li></ul></ul><ul><...
Upcoming SlideShare
Loading in...5
×

Scala introduction

1,101

Published on

for Sayeret Lambda January 2011 meeting

Published in: Technology
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,101
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
19
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Scala introduction

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

    Clipping is a handy way to collect important slides you want to go back to later.

×