Making sense of                          & other JVM languages          Praveen Manvi          November 2012
whoami• Currently working as Senior Technologist @ Thomson Reuters• 13 + years java development experience• Focused on API...
Purpose of this presentation  Introduce Scala language to Java developers  Hope to convince that scala solves many of Java...
What is Scala?Scala is a new programming language for the Java Virtual Machine. Itcombines advanced object-oriented and fu...
HistoryScalable Language – 2006 (same principles for small to large) The language is so named because it was designed to g...
What Experts say …“I can honestly say if someone had shown methe Programming Scala book by by MartinOdersky, Lex Spoon & B...
Salient FeaturesSpeed/Safety of a static languageClean syntax, expressive power, type inferencePure object-orientation (Ev...
Ceremony vs EssenceGet the minors from the list List<Person> peopleList<Person> minors(List<Person> people) {     List<Per...
Fixing Java Day Today IssuesNative XML support def page(name: String, format: String) = Action {     Ok(<p>You requested p...
Fixing Java Day Today Issues…Multi-line StringSQL( """ select * from Country c join CountryLanguage l             on l.Cou...
Fixing Java Day Today Issues…Import statementsMultiple classes   import java.util.{Date, Collection, List}Renaming the cla...
Fixing Java Day Today Issues…Handling exceptions  val PORT = try System.getProperty(“PORT", "8080").toInt catch { case e: ...
Fixing Java Day Today Issues…Handling Nulldef createPerson(name:String, age:Int, ethnicity:Option[String]) = {  storeInDat...
Less boiler plate codeNo need to use external libraries to avoid, moreimportantly fluency is part of language-   Apache co...
Java – 8 lambdaNo more anonymous classesclass UIBuilder { public UIBuilder() {                button.addActionListener(e -...
Scala == effective Java•   Favor Immutability•   Avoid Null references•   Type safe objects•   Consider static factory met...
DSL in scalaNew language can be defined to buy(200 sharesOf "GOOGLE") maxUnitPrice 300 using defaultPricingif a programmin...
Multi core programming“Single-core performance is running out of steam, and you need   to parallelize everything,”Make col...
Interesting new (& enhanced)              concepts• Inheritance  – Sealed Vs final  – Traits Vs interfaces• Generics & Ope...
Functional Programming•   Assignment less programming•   Immutable state•   Functions as basic building blocs•   Programmi...
Closures and Curryingval multiply = { final int x, final int y -> x * y } // twice:: Integer -> Integerfinal twice = multi...
Scala show-pieces
Final comments• Scala is not just a “Java++”.  – its different and you can get into any depth level,    everyday you will ...
Scala is like “Dragon” in Avatar  It will frighten in the beginning…
But once you are trained…It will be awesome experience to ride
Java.next() – which one to pick
Feature List• is an agile and dynamic language for the Java  Virtual Machine• builds upon the strengths of Java but  has a...
Feature List• provides the ability to statically type  check and statically compile your code• supports Domain-Specific La...
Feature List• increases developer productivity by reducing  scaffolding code when developing web, GUI,  database or consol...
Powered By Scala
References
Upcoming SlideShare
Loading in …5
×

Scala and jvm_languages_praveen_technologist

472 views
407 views

Published on

Introduction to scala

0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
472
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
0
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Scala and jvm_languages_praveen_technologist

  1. 1. Making sense of & other JVM languages Praveen Manvi November 2012
  2. 2. whoami• Currently working as Senior Technologist @ Thomson Reuters• 13 + years java development experience• Focused on API design, middleware, desktop ,web application frameworks• Used Groovy/Scala for personal/internal projects but NOT professionally• The material here are strictly personal opinions with limited functional programming exposure & understood by internet literature .Consider what you like & disdain others• Created http://JavaIQ.in in play framework as personal project
  3. 3. Purpose of this presentation Introduce Scala language to Java developers Hope to convince that scala solves many of Javas design/coding problems. Explain how some other JVM languages stack against Scala particularly Groovy where-ever applicable.However - Its NOT a comprehensive overview on scala
  4. 4. What is Scala?Scala is a new programming language for the Java Virtual Machine. Itcombines advanced object-oriented and functional concepts which willgive enormous productivity and expressiveness benefits over Java.Scala is fully interoperable with Java, preserving existing investments insoftware, tools and skills.Key Differentiator from other JVM languages:Allows to write succinct programs without losing type safety andperformance in both object oriented and functional style.
  5. 5. HistoryScalable Language – 2006 (same principles for small to large) The language is so named because it was designed to grow with the demands of users Martin Odersky - Creator
  6. 6. What Experts say …“I can honestly say if someone had shown methe Programming Scala book by by MartinOdersky, Lex Spoon & Bill Venners back in 2003I’d probably have never created Groovy.” –James Strachan.“If I were to pick a language to use today otherthan Java, it would be scala” - James Gosling“I think Scala is the only current language likelyto take the throne from Java” - Charles OliverNutter (JRuby Creator)
  7. 7. Salient FeaturesSpeed/Safety of a static languageClean syntax, expressive power, type inferencePure object-orientation (Everything is an Object)Support for concise functional programmingScalability to large industrial-scale systemsPattern matchingXML support such as XML literals in code
  8. 8. Ceremony vs EssenceGet the minors from the list List<Person> peopleList<Person> minors(List<Person> people) { List<Person> result = new ArrayList<Person>(); for (Person person : people) { if (person.age() <= 18) { result.add(person); } } return result;}val minors = people.filter(_.age <= 18)def minors = people.filter { p -> p.age <= 18 }“succinctness is power” - http://www.paulgraham.com/power.html
  9. 9. Fixing Java Day Today IssuesNative XML support def page(name: String, format: String) = Action { Ok(<p>You requested page {name} in format {format}</p>.toString).as(HTML) }Case class for type safe immutable objects case class Person(firstName: String,lastName:String , age:Int, salaryIn$:Int, skills:Array[String]) new Person("Praveen", "Manvi", 35, 40000, Array("Java Script", "Java", "Scala", "Groovy")), new Person(firstName="Prasanna", "Manvi", 40, 90000, Array("C", "Pascal", "C++")),Functional Java Collections with fluent API val (passed, failed) = List(49, 31, 58, 76, 82, 88, 90) partition ( _ > 60 )
  10. 10. Fixing Java Day Today Issues…Multi-line StringSQL( """ select * from Country c join CountryLanguage l on l.CountryCode = c.Code where c .code = {countryCode}; """ ).on("countryCode" -> "FRA") Packages can be nested & imports can be in methods package out { class Main { ... } package inner { class Helper { ... } } }
  11. 11. Fixing Java Day Today Issues…Import statementsMultiple classes import java.util.{Date, Collection, List}Renaming the classes import java.util.{Collection => JavaCollection}Import classses anywhere in the documentPattern Matching def match(x: Int): String = x match { case 0 => “none” case 1 => “one” case 2 => “two” case _ => “many” }
  12. 12. Fixing Java Day Today Issues…Handling exceptions val PORT = try System.getProperty(“PORT", "8080").toInt catch { case e: NumberFormatException => 8080 }Passing default/named parameters /** override only the loadFactory via named arguments */ val m4 = new HashMap[String,Int](loadFactor = 0.8)Singleton class /** * object creates */ object Main { }
  13. 13. Fixing Java Day Today Issues…Handling Nulldef createPerson(name:String, age:Int, ethnicity:Option[String]) = { storeInDatabase(name,age,ethnicity.getOrElse("U").charAt(0))}Adding methods to existing classcase User(name:String,pwd:String)implicit UserWrapper(val u:User) extends AnyValdef isAdmin = u.name.equals(“admin”)
  14. 14. Less boiler plate codeNo need to use external libraries to avoid, moreimportantly fluency is part of language- Apache commons – IO, String manipulation- Guava – Collections- Function Java- Java 7 – File APIs, resource handlingWe don’t have resolve special tricks (Predicate,anonymous classes) to get this benefitSo, Its about mindset change not just the keystrokessavings
  15. 15. Java – 8 lambdaNo more anonymous classesclass UIBuilder { public UIBuilder() { button.addActionListener(e -> //process ActionEvent e) }}
  16. 16. Scala == effective Java• Favor Immutability• Avoid Null references• Type safe objects• Consider static factory methods & builder pattern instead of long constructor
  17. 17. DSL in scalaNew language can be defined to buy(200 sharesOf "GOOGLE") maxUnitPrice 300 using defaultPricingif a programming language is sufficiently powerful, then your fantasy is the limit
  18. 18. Multi core programming“Single-core performance is running out of steam, and you need to parallelize everything,”Make collections go parallel val people: Array[Person] val(minors, adults) = people.par partition(_.age<18)
  19. 19. Interesting new (& enhanced) concepts• Inheritance – Sealed Vs final – Traits Vs interfaces• Generics & Operator overloading – val list = new ListBuffer[String] – list+=“praveen”
  20. 20. Functional Programming• Assignment less programming• Immutable state• Functions as basic building blocs• Programming with expressions - transforming of data
  21. 21. Closures and Curryingval multiply = { final int x, final int y -> x * y } // twice:: Integer -> Integerfinal twice = multiply.curry(2) // quadruple:: Integer -> Integerfinal quadruple = multiply.curry(4)assert twice(5) == 10assert quadruple(5) == 20
  22. 22. Scala show-pieces
  23. 23. Final comments• Scala is not just a “Java++”. – its different and you can get into any depth level, everyday you will get surprised. – Kingdom of nouns Vs Kingdom of Verbs
  24. 24. Scala is like “Dragon” in Avatar It will frighten in the beginning…
  25. 25. But once you are trained…It will be awesome experience to ride
  26. 26. Java.next() – which one to pick
  27. 27. Feature List• is an agile and dynamic language for the Java Virtual Machine• builds upon the strengths of Java but has additional power features inspired by languages like Python, Ruby and Smalltalk• makes modern programming features available to Java developers with almost-zero learning curve.#1 choice for java progrmmers
  28. 28. Feature List• provides the ability to statically type check and statically compile your code• supports Domain-Specific Languages and other compact syntax so your code becomes easy to read and maintain• makes writing shell and build scripts easy with its powerful processing primitives, OO abilities and an DSL for build systems
  29. 29. Feature List• increases developer productivity by reducing scaffolding code when developing web, GUI, database or console applications• simplifies testing by supporting unit testing and mocking out-of-the-box• seamlessly integrates with all existing Java classes and libraries
  30. 30. Powered By Scala
  31. 31. References

×