Scala presentationjune112011


Published on

presented for JUG-C monthly meetup

Published in: Technology
1 Comment
No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Scala presentationjune112011

  1. 1. Introducing Scala By Prasanna Kumar JUG-C group meetup 11 June 2011
  2. 2. About me <ul><li>Java programmer turned Scala “enthusiast” </li></ul><ul><li>Started (learning) with scala very recently :-) </li></ul><ul><ul><li>Explored almost all the workaround to setup development environment for Scala </li></ul></ul><ul><li>Committer of Kandash project </li></ul><ul><ul><ul><li>- Kanban dashboard application </li></ul></ul></ul><ul><li>Reach me </li></ul><ul><li>Email : [email_address] </li></ul><ul><li>Twitter : @prassee </li></ul><ul><li>Blog : </li></ul>
  3. 3. Topic Covered <ul><li>What is Scala? </li></ul><ul><li>Why Scala? </li></ul><ul><li>Basics of Scala </li></ul><ul><li>Scala development environment </li></ul><ul><ul><li>(a small demo with Intellij IDEA & SBT) </li></ul></ul><ul><li>Scala Frameworks </li></ul><ul><li>Learning Resources </li></ul><ul><li>Recent News / updates </li></ul><ul><li>Questions ? </li></ul>
  4. 4. What is Scala
  5. 5. Brief History <ul><li>A JVM language </li></ul><ul><ul><li>Source code compiled in to .class file which can be deployed in to any JVM / Container </li></ul></ul><ul><ul><li>Blend of OOP and Functional OP </li></ul></ul><ul><li>Invented by Martin Odersky & Team </li></ul><ul><ul><li>Key developer behind Generics in Java </li></ul></ul><ul><ul><li>Created functional language called pizza </li></ul></ul><ul><ul><li>Under the incubation of EPFL </li></ul></ul><ul><ul><ul><li>(École Polytechnique Fédérale de Lausanne) ??? </li></ul></ul></ul>
  6. 6. Motivations <ul><li>Considered as the Java of the Feature !!!!!!!!! </li></ul><ul><li>Has many good features already which are much awaited or still under development in Java 7 , 8 ... etc </li></ul><ul><li>Increasingly becoming popular language </li></ul>
  7. 7. Who is using
  8. 8. Why Scala ? <ul><li>Comparatively less code (Less chance of introducing defects) </li></ul><ul><li>Modular code – (coz of imperative & functional model) </li></ul><ul><li>Maintainable code (easy to extend) </li></ul><ul><li>Less side effects (by following functional & less state agonistic code) </li></ul><ul><li>Seamless integration with Java </li></ul><ul><ul><li>Existing java code can be used in Scala – vice versa </li></ul></ul>
  9. 9. Basics of Scala
  10. 10. Hello World – first Scala program !!! object HelloWorld { def main(args: Array[String]) { println(&quot;Hello World&quot;) } } object HelloWorld e xtends Application { // no need of main method println(&quot;Hello World&quot;) } Output: Hello World
  11. 11. Programming Constructs
  12. 12. Variables Scala has two types of variables var & val Var – read write Val – read only Type inference val i=23 // inferred to int val str = “string” // inferred to int scala> val array: Array[String] = new Array(5) array: Array[String] = Array(null, null, null, null, null) scala> array = new Array(2) <console>:5: error: reassignment to val array = new Array(2)
  13. 13. Conditional Statements <ul><li>If statement follows the same syntax as with java , however if statements are expressions by themselves </li></ul><ul><ul><li>// normal usage </li></ul></ul><ul><ul><li>if(1>2) { </li></ul></ul><ul><li>println(&quot;1 is greater&quot;) </li></ul><ul><li>} else { </li></ul><ul><li>println(&quot;2 is greater&quot;) </li></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>// usage of if stmt as expressions </li></ul></ul><ul><ul><li>val xpr = if(1>2) { 1 } else { 2 } </li></ul></ul><ul><ul><li>println(&quot;result of if expr is &quot;+xpr) </li></ul></ul>
  14. 14. Loops <ul><li>For loops </li></ul><ul><ul><li>Much comprehended than java -> more flexibility </li></ul></ul><ul><ul><li>and functional agonistic </li></ul></ul><ul><ul><li>def forTest() { </li></ul></ul><ul><ul><li>val list = List(1,2,3,4) </li></ul></ul><ul><ul><li>for(valus<- list) { </li></ul></ul><ul><ul><li>print(valus) </li></ul></ul><ul><ul><li>// prints 1 2 3 4 </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>// prints 1 2 3 4 </li></ul></ul><ul><ul><li>list.foreach(print(_)) </li></ul></ul><ul><ul><li>// conditional if stmts inside for </li></ul></ul><ul><ul><li>for(numbs <- list if(numbs > 2)) { </li></ul></ul><ul><ul><li>println(&quot;numbers greater than 2 &quot; +numbs) </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>// multiple if stmts inside for </li></ul></ul><ul><ul><li>for(nums <- list if(nums >1); if(nums<4)) { </li></ul></ul><ul><ul><li>println(&quot;Filtered numbers&quot; + nums) </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul>
  15. 15. While and do-While <ul><li>While loop also share the same syntax of java (or any other imperative language) </li></ul><ul><li>// while stmt </li></ul><ul><li>var a =0 </li></ul><ul><li>while(a<10) { </li></ul><ul><li>a =a +1 </li></ul><ul><li>println(a) </li></ul><ul><li>} </li></ul><ul><li>// do while stmt </li></ul><ul><li>var count = 0 </li></ul><ul><li>do { count += 1 println(count) </li></ul><ul><li>} while (count < 10) </li></ul>
  16. 16. First Class in Scala <ul><li>The first scala class </li></ul><ul><ul><li>// class declaration with default constructor </li></ul></ul><ul><ul><li>class Rational(n:Int,d:Int) { </li></ul></ul><ul><ul><li>val num = n // public by default </li></ul></ul><ul><ul><li>val den = d </li></ul></ul><ul><ul><li>/* auxillary constructor definitions */ </li></ul></ul><ul><ul><li>def this(n:Int) = this(n,1) // auxiallry constructor </li></ul></ul><ul><ul><li>/* method definitions */ </li></ul></ul><ul><ul><li>def +(that:Rational):Rational = new Rational(num*that.den + den*that.num ,den*that.den) </li></ul></ul><ul><ul><li>def +(i:Int):Rational = new Rational(num * i,den) </li></ul></ul><ul><ul><li>// override key word is mandatory while overridding methods </li></ul></ul><ul><ul><li>override def toString = num + &quot;/&quot; + den </li></ul></ul><ul><ul><li>} </li></ul></ul>
  17. 17. Named & Default Parameters <ul><li>Parameters can be given default values </li></ul><ul><li>class Rational(n:Int,d:Int = 1) { </li></ul><ul><ul><li>...... </li></ul></ul><ul><ul><li>...... </li></ul></ul><ul><li>} </li></ul><ul><li>Rational(2) // use of default param </li></ul><ul><li>Rational(d=4,n=7) // order of param has changed still no impact due to named param </li></ul><ul><li>Avoid creating overloaded method (but only when necessary) </li></ul>
  18. 18. Case classes <ul><li>Scala's approach of pattern matching </li></ul><ul><li>very flexible </li></ul><ul><li>Elegant syntax </li></ul><ul><li>No need to create objects </li></ul><ul><li>Classes prefixed with 'case' keyword </li></ul><ul><li>case class Java(vers:String) { </li></ul><ul><li>override def version: String = &quot;1.6&quot; </li></ul><ul><li>// no need of braces for single line </li></ul><ul><li>} </li></ul><ul><li>// usage </li></ul><ul><li>case class Scala(vers:String) { </li></ul><ul><li>override def version: String = &quot;2.9.0&quot; </li></ul><ul><li>// no need of braces for single line </li></ul><ul><li>} </li></ul><ul><li>for (languages <- list) { </li></ul><ul><li>languages match { </li></ul><ul><li>case Java(&quot;1.6&quot;) => { </li></ul><ul><li>println(languages.version) </li></ul><ul><li>} </li></ul><ul><li>case Scala(&quot;2.9&quot;) => { </li></ul><ul><li>println(languages.version) </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  19. 19. Companion classes <ul><li>Scala's approach to static methods </li></ul><ul><li>Unlike Java Scala doesn't have static keyword </li></ul><ul><li>Classes can be created with the keyword 'object' methods inside such classes can be accessed without creating objects </li></ul><ul><li>object Printer { </li></ul><ul><li>def print(file:File) { </li></ul><ul><li>println(&quot;printing file&quot;) </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>Companion classes are those which shares the name of the originating class and has to exist in the same source file </li></ul><ul><li>Can be useful for creating factory classes </li></ul>
  20. 20. Traits <ul><li>Scala's approach to Interfaces (as in Java) </li></ul><ul><li>Unlike Java, Scala's traits can have implementation </li></ul><ul><li>Like final keyword in java Scala provides a key word called 'sealed' to define classes and traits to prevent inheritance. </li></ul><ul><li>- sealed class StringUtils { </li></ul><ul><li>… .... </li></ul><ul><li>} </li></ul><ul><li>trait IntSet { </li></ul><ul><li>def contains(x: Int): Boolean </li></ul><ul><li>def notContains(x: A) = !contains(x) </li></ul><ul><li>def add(x: Int): IntSet </li></ul><ul><li>} </li></ul><ul><li>object EmptySet extends IntSet { </li></ul><ul><li>def contains(x: Int): Boolean = false </li></ul><ul><li>def add(x: Int): IntSet = new NonEmptySet(x, EmptySet, EmptySet) </li></ul><ul><li>} </li></ul><ul><li>class NonEmptySet(elem: Int, left: IntSet, right: IntSet) extends IntSet { </li></ul><ul><li>def contains(x: Int): Boolean = if (x < elem) left contains x else if (x > elem) right contains x else true </li></ul><ul><li>def add(x: Int): IntSet = if (x < elem) new NonEmptySet(elem, left add x, right) else if (x > elem) new NonEmptySet(elem, left, right add x) else this </li></ul><ul><li>} </li></ul>
  21. 21. Packages <ul><li>// unlike java import stmts are relative to java </li></ul><ul><li>// and independent of physical location </li></ul><ul><li>package org { </li></ul><ul><li>package jugc { </li></ul><ul><li>import scalatweet.Tweeter </li></ul><ul><li>// imports are relative </li></ul><ul><li>class TweeterClient { </li></ul><ul><li>val tweeter = new Tweeter(); </li></ul><ul><li>tweeter.findAllMyTweets(&quot;prassee&quot;) </li></ul><ul><li>} </li></ul><ul><li>package scala { </li></ul><ul><li>class ComClass1 { </li></ul><ul><li>def whereIam = &quot;org.jugc.scala.ComClass1&quot; </li></ul><ul><li>}}}} </li></ul><ul><li>package org.jugc.scala { </li></ul><ul><li>class ComClass2 { </li></ul><ul><li>def whereIam = &quot;org.jugc.scala.ComClass2&quot; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>package org.jugc.scalatweet { </li></ul><ul><li>class Tweeter { </li></ul><ul><li>def findAllMyTweets(userId: String): List[String] = List(&quot;gave a presentation on scala&quot;, &quot;blogged about scala&quot;) </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  22. 22. Exception Handling <ul><li>package org.jugc.scalatweet { </li></ul><ul><li>import{FileNotFoundException, FileReader} </li></ul><ul><li>class Tweeter { </li></ul><ul><li>def findAllMyTweets(userId: String): List[String] = List(&quot;gave a presentation on scala&quot;, &quot;blogged about scala&quot;) </li></ul><ul><li>def post(tweet:String) { </li></ul><ul><li>} </li></ul><ul><li>def postTweet() { </li></ul><ul><li>try { </li></ul><ul><li>val f = new FileReader(&quot;input.txt&quot;) </li></ul><ul><li> </li></ul><ul><li>} catch { </li></ul><ul><li>case ex:FileNotFoundException => { </li></ul><ul><li>throw new TweetPostException() </li></ul><ul><li>} </li></ul><ul><li>case ex:Exception => // handle exception thwow </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>class TweetPostException extends Exception { </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  23. 23. Scala's Type Heirachy
  24. 24. Setting up development environment <ul><li>Scala has plugins for </li></ul><ul><ul><li>Eclipse </li></ul></ul><ul><ul><li>Netbeans </li></ul></ul><ul><ul><li>Intellij IDEA </li></ul></ul><ul><ul><li>All the above have SBT integration (good news) !!! </li></ul></ul><ul><li>Case Study </li></ul><ul><li>Intellij IDEA </li></ul><ul><ul><li>Community edition (still a good IDE for kick starting with Scala development) </li></ul></ul><ul><ul><ul><li>(Not promoting IDEA in anyway) </li></ul></ul></ul><ul><ul><li>Over to demo session !!!! </li></ul></ul><ul><ul><li>Or </li></ul></ul><ul><ul><li>refer to my latest blog post </li></ul></ul><ul><ul><li>- Eclipse looks promising !!!!!! </li></ul></ul><ul><ul><li>My suggestion is to consider all the above 3 IDE's for development </li></ul></ul>
  25. 25. Frameworks <ul><li>There are lot of frameworks for Scala (like java) </li></ul><ul><li>Scala test – Unit testing </li></ul><ul><li>Specs - BDD </li></ul><ul><li>Akka - concurrency </li></ul><ul><li>Junit integration (seamless) </li></ul><ul><li>Lift for web 2.0 </li></ul><ul><li>Scalatra – templating engine </li></ul><ul><li>....... </li></ul><ul><li>A lot more !!!!!!! </li></ul>
  26. 26. Learning Resources <ul><li>Books </li></ul>The most prominent books for learning scala - from the source More books to come ......
  27. 27. Learning Resources <ul><li>Blogs </li></ul><ul><ul><li> </li></ul></ul><ul><ul><li> </li></ul></ul><ul><ul><li> </li></ul></ul><ul><ul><li>Create a blog for yourself </li></ul></ul><ul><ul><li>Tweet about it !!! </li></ul></ul><ul><ul><li>JUG-C google groups !!! </li></ul></ul><ul><li>Effective use of Stack Overflow </li></ul><ul><ul><li>Ask questions </li></ul></ul><ul><ul><li>Answering questions </li></ul></ul><ul><ul><ul><li>Quuick response </li></ul></ul></ul><ul><ul><ul><li>Professional community </li></ul></ul></ul><ul><ul><ul><li>Open to opinions </li></ul></ul></ul>
  28. 28. Learning levels <ul><li>Have levels for your self while learning the language this really helps !!!! </li></ul><ul><li>Level A1: Beginning application programmer </li></ul><ul><li>Java-like statements and expressions: standard operators, method calls, conditionals, loops, try/catch </li></ul><ul><li>class, object, def, val, var, import, package </li></ul><ul><li>Infix notation for method calls </li></ul><ul><li>Simple closures </li></ul><ul><li>Collections with map, filter, etc </li></ul><ul><li>for-expressions </li></ul><ul><li>Level A2: Intermediate application programmer </li></ul><ul><li>Pattern matching </li></ul><ul><li>Trait composition </li></ul><ul><li>Recursion, in particular tail recursion </li></ul><ul><li>XML literals </li></ul><ul><li>Level A3: Expert application programmer </li></ul><ul><li>Folds, i.e. methods such as foldLeft, foldRight </li></ul><ul><li>Streams and other lazy data structures </li></ul><ul><li>Actors </li></ul><ul><li>Combinator parsers </li></ul><ul><li>Level L1: Junior library designer </li></ul><ul><li>Type parameters </li></ul><ul><li>Traits </li></ul><ul><li>Lazy vals </li></ul><ul><li>Control abstraction, currying </li></ul><ul><li>By-name parameters </li></ul><ul><li>Level L2: Senior library designer </li></ul><ul><li>Variance annotations </li></ul><ul><li>Existential types (e.g., to interface with Java wildcards) </li></ul><ul><li>Self type annotations and the cake pattern for dependency injection </li></ul><ul><li>Structural types (aka static duck typing) </li></ul><ul><li>Defining map/flatmap/withFilter for new kinds of for-expressions </li></ul><ul><li>Extractors </li></ul><ul><li>Level L3: Expert library designer </li></ul><ul><li>Early initializers </li></ul><ul><li>Abstract types </li></ul><ul><li>Implicit definitions </li></ul><ul><li>Higher-kinded types </li></ul>
  29. 29. Recent News updates <ul><li>Typesafe </li></ul><ul><ul><li>A company formed by Martin odersky and Jonas boner (Akka) </li></ul></ul><ul><ul><li>Launched a Typesafe stack </li></ul></ul><ul><ul><ul><li>A scala development stack – opensource !!! </li></ul></ul></ul><ul><ul><li>James Gosling !!! - inventor of java honoured with the post of member of advisory board </li></ul></ul><ul><ul><li>Lot of updates coming on </li></ul></ul><ul><li>Cloud foundry </li></ul><ul><ul><li>Launched by VMWare </li></ul></ul><ul><ul><li>Announed a PaaS service </li></ul></ul><ul><ul><li>Included support for Scala </li></ul></ul><ul><ul><ul><li> </li></ul></ul></ul><ul><li>JUG-C Initiatives </li></ul><ul><ul><li>Scalatweet a lift based web app under development </li></ul></ul><ul><ul><li>@scalatweet </li></ul></ul><ul><ul><li>Open for community participation </li></ul></ul>
  30. 30. Questions ?
  31. 31. Thank you !!!!!