Scala Day by Day
-
Upload
ionut-andonescu -
Category
Software
-
view
75 -
download
0
Transcript of Scala Day by Day
SCALA DAY BY DAYWhat means to work with Scala. A story from a previous Java Developerby Ionut Andonescu23 April 2016, Iasi
Sponsors
Sponsors
SUMMARY Intro (Java Evolution) Scala Refresh ( Java - to - Scala) Scala Patterns Interesting/Weird Things Questions
INTRO Java Evolution
JAVA Imperative language Statically typed Runs on JVM
JDK 1.0 (21 January 1996) …… JDK SE 8 (18 March, 2014)
JAVA EVOLUTIONJDK SE 8 JDK SE 9
code reorganization lightweight JSON API JEP 286
addition of lambda expressions shift to functional programming
JEP 286: LOCAL VARIABLE TYPE INFERENCE Goal “We seek to improve the developer experience by reducing the ceremony associated with writing Java code, while maintaining Java's commitment to static type safety, by allowing developers to elide the often-unnecessary manifest declaration of local variable types.”
74%
12%
10%3%1%
What do you think of the proposed Local Variable Type In-ference feature overall? *Takipi
I think it'a great idea!
Meh, it'a OK.
I think it'a bad idea!
Not sure
Other
JEP 286: SYNTAX CHOICES var x = expr only (like C#) var, plus val for immutable locals (like Scala, Kotlin) var, plus let for immutable locals (like Swift) auto x = expr (like C++) const x = expr (already a reserved word) final x = expr (already a reserved word) let x = expr def x = expr (like Groovy) x := expr (like Go)
23.7
48.1
14
34
7.2
Possible syntax options*Takipi
var only
var and val
var and let
let only
auto only
Other
SCALA Object-functional language Statically typed Runs on JVM First version - 20 January 2004 latest & stable version - 8 March 2016 (2.11.8)
Around 500.000 developers are using Scala * http://www.scala-lang.org/blog/2016/03/14/announcing-the-scala-center.html
SCALA REFRESH Java - to - Scala
DIFFERENCESObvious differences
semicolons variable declaration : val/var method declarations inferring type information
lazy val imported file types methods with empty argument lists options either
Syntactic Sugar and more features
partial functions ranges: 1 to n reserved words if statement for comprehensions call by name / call by value try catch organized code in files and namespaces
JAVA CODE TO
SCALA METHODS
SCALA FUNCTIONAL
CLASS/INTERFACE CREATION
CASE CLASSES
PATTERN MATCHING
FUNCTIONS With default value
FUNCTIONS With named parameters
FUNCTIONS With nested functions
FUNCTIONS Passed as argumentReturned as value
SCALA PATTERNS Interesting/Weird Things
TUPLES
IMUTABILITY VS MUTABILITY Problem:“Separate students from a given List into separate groups: those from Romania and those outside of Romania”
IMUTABILITY VS MUTABILITY
IMUTABILITY VS MUTABILITY
IMUTABILITY VS MUTABILITY
IMUTABILITY VS MUTABILITY
MONADS
Pug monad
OPTION
EITHER
FUTURE
TRY
MAP & FLATMAPscala.util.Try
def map[U](f: T => U): Try[U]Maps the given function to the value from this Success or returns this if this is a Failure. def flatMap[U](f: T => Try[U]): Try[U]Returns the given function applied to the value from this Success or returns this if this is a Failure. ……
scala.Optiondef map[B](f: A => B): Option[B]Returns a scala.Some containing the result of applying f to this scala.Option's value if this scala.Option is nonempty. Otherwise returndef flatMap[B](f: A => Option[B]): Option[B]Returns the result of applying f to this scala.Option's value if this scala.Option is nonempty. Returns None if this scala.Option is empty. ……
WHAT MEANS TO WORK WITH SCALA?
“Unlike with many traditional functional languages, Scala allows a gradual, easy migration to a more functional style.
You can start to use it as a “Java without semicolons”. Over time, you can progress to gradually eliminate mutable state in your applications, phasing in safe functional composition patterns instead. As Scala programmers we believe that this progression is often a good idea.
At the same time, Scala is not opinionated; you can use it with any style you prefer.”
http://www.scala-lang.org/what-is-scala.html
QUESTIONS
THE END.
SCALA DAY BY DAYIonut Andonescu
23 April 2016, Iasi
Please fill the online evaluation form after event