Scala introduction

Download Scala introduction

Post on 15-Jan-2015

1.011 views

Category:

Technology

3 download

Embed Size (px)

DESCRIPTION

for Sayeret Lambda January 2011 meeting

TRANSCRIPT

<ul><li> 1. Introduction http://www.scala-lang.org Sayeret Lambda January 2011 Yardena Meymann</li></ul> <p> 2. What is Scala </p> <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><li><ul><li>but also to CLR, and to JavaScript (in progress) </li></ul></li></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><li><ul><li>DSL friendly </li></ul></li></ul> <p> 3. History </p> <ul><li>1995 </li></ul> <ul><li>Sun and Netscape team up and announce first beta release of Java </li></ul> <p> 4. History </p> <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> <p> 5. History </p> <ul><li>1999 </li></ul> <ul><li>Sun buys Java compiler from Martin Odersky, it is shipped with JDK 1.3 </li></ul> <p>OUR JAVA COMPILER ISNT GOOD ENOUGH I KNOW THIS GUY IN SWITZERLAND, HE WROTE A GREAT JAVA COMPILER ! 6. History </p> <ul><li>1999 </li></ul> <ul><li>Work on adding generics to Java begins </li></ul> <p> 7. History </p> <ul><li>2001 </li></ul> <ul><li>Martin Odersky starts</li></ul> <ul><li>working on Scala at </li></ul> <p> 8. Then and Now </p> <ul><li>2003Scala 1.0 </li></ul> <ul><li>2006Scala 2.0 </li></ul> <ul><li>2010Scala 2.8 </li></ul> <p> If I were to pick a language to use today other than Java, it would beScala James Gosling 9. Now </p> <ul><li>Open source projects in Scala:</li></ul> <ul><li><ul><li>Akka, Lift, Specs, ScalaCheck, Scalaz, ScalaQuery, </li></ul></li></ul> <ul><li>Books: </li></ul> <ul><li>Tools:</li></ul> <ul><li><ul><li>built-in REPL, Eclipse, IntelliJ and NetBeans plug-ins </li></ul></li></ul> <ul><li><ul><li>integration modules for almost every popular Java framework </li></ul></li></ul> <p> 10. Now </p> <ul><li>Used in industry:</li></ul> <ul><li><ul><li>LinkedIn, Twitter, Novell, EDF, The Guardian, Xebia, Xerox, Sony, Siemens, </li></ul></li></ul> <ul><li>Jobs: </li></ul> <p> 11. Influences on Scala Design </p> <ul><li>Java, C#</li></ul> <ul><li><ul><li>syntax, basic types, and class libraries, </li></ul></li></ul> <ul><li>Smalltalk</li></ul> <ul><li><ul><li>uniform object model, </li></ul></li></ul> <ul><li>Eiffel</li></ul> <ul><li><ul><li>uniform access principle, </li></ul></li></ul> <ul><li>Beta</li></ul> <ul><li><ul><li>systematic nesting, </li></ul></li></ul> <ul><li>ML, Haskell</li></ul> <ul><li><ul><li>many of the functional aspects. </li></ul></li></ul> <p> 12. Imagine 13. No Primitives </p> <ul><li>Everything is an object </li></ul> <p> 14. No Operators (almost) </p> <ul><li>Everything is a method </li></ul> <p>1 + 2 1.+(2) 123 toString 123.toString() 15. No Noise </p> <ul><li>Semicolon inference </li></ul> <ul><li>Type inference </li></ul> <ul><li>Empty and single line blocks dont need { } </li></ul> <ul><li>Infix notation for method invocations </li></ul> <ul><li>Implicit public </li></ul> <ul><li>Empty parameter lists dont need () </li></ul> <p> 16. No Statements </p> <ul><li>Everything is an expression </li></ul> <ul><li>Return is optional, last expression is returned </li></ul> <ul><li><ul><li>defplus2(x:Int) = x + 2 </li></ul></li></ul> <ul><li>for ,while ,if/elseare expressions </li></ul> <ul><li>Unitequivalent of void </li></ul> <p> 17. No final </p> <ul><li>valandvar , memoization </li></ul> <p>deffoo() { valx = immutable vary = mutable lazy valz = lazy } 18. No Checked Exceptions </p> <ul><li>All exceptions areruntime </li></ul> <ul><li>Also, you haveOption[T]andEither[A,B]that can be used instead of checked exceptions </li></ul> <p> 19. No Statics </p> <ul><li>And no manual singletons </li></ul> <ul><li>Instead we haveobject companion class </li></ul> <p>object HelloWorld { def main(args: Array[String]) = println("Hello, world!") } 20. First-class Functions val romanNumeral = Map(1 -&gt; "I", 2 -&gt; "II",3 -&gt; "III", 4 -&gt; "IV", 5 -&gt; "V") An object with apply method can be used as a function valinc = (x: Int)=&gt;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 classPerson( var name: String, var age: Int) 22. Case Classes </p> <ul><li>Also generates equals, hashCode and allows decomposition via pattern matching </li></ul> <p>abstract classTermcase classVar(name: String)extendsTermcase classFun(arg: String, body: Term) extends Termcase classApp(f: Term, v: Term)extendsTerm 23. No Switch Pattern Matching </p> <ul><li>Extractor objects are auto-generated for case classes </li></ul> <p>defprintTerm(term: Term) {termmatch{caseVar(n)=&gt;print(n)caseFun(x, b)=&gt; print("^" + x + ".") ; printTerm(b)caseApp(f, v)=&gt; Console.print("(");printTerm(f); print(" "); printTerm(v); print(")")}}defisIdentityFun(term: Term): Boolean = termmatch{caseFun(x, Var(y))ifx == y=&gt; true case_=&gt; false } 24. Functions and Closures </p> <ul><li>With some syntax sugar: </li></ul> <p>List(1, 2, 3).map((x: Int) =&gt; x + 1)List(2, 3, 4) List(1, 2, 3).map(x =&gt; x + 1) List(1, 2, 3).map(_ + 1) Closures: varmore = 7 valaddMore = (x: Int) =&gt; x +more 10 25. Lists </p> <ul><li>vallist = 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 (_ &lt; 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 (_ &gt; _)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> <p> 26. Tuples </p> <ul><li>defgetNameAndAge: Tuple2[String, Int] = { </li></ul> <ul><li>valname = ... </li></ul> <ul><li>valage = ... </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> <p> 27. For Comprehensions </p> <ul><li>Find all attendees named Fred that speaks Danish </li></ul> <p>for { att</p>