Scala Day by Day

45
SCALA DAY BY DAY What means to work with Scala. A story from a previous Java Developer by Ionut Andonescu 23 April 2016, Iasi

Transcript of Scala Day by Day

Page 1: 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

Page 2: Scala Day by Day

Sponsors

Page 3: Scala Day by Day

Sponsors

Page 4: Scala Day by Day

SUMMARY Intro (Java Evolution) Scala Refresh ( Java - to - Scala) Scala Patterns Interesting/Weird Things Questions

Page 5: Scala Day by Day

INTRO Java Evolution

Page 6: Scala Day by Day

JAVA Imperative language Statically typed Runs on JVM

JDK 1.0 (21 January 1996) …… JDK SE 8 (18 March, 2014) 

Page 7: Scala Day by Day

JAVA EVOLUTIONJDK SE 8 JDK SE 9

code reorganization  lightweight JSON API  JEP 286

addition of lambda expressions shift to functional programming

Page 8: Scala Day by Day

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.”

Page 9: Scala Day by Day

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

Page 10: Scala Day by Day

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)

Page 11: Scala Day by Day

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

Page 12: Scala Day by Day

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

Page 13: Scala Day by Day

SCALA REFRESH Java - to - Scala

Page 14: Scala Day by Day

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

Page 15: Scala Day by Day

JAVA CODE TO

Page 16: Scala Day by Day

SCALA METHODS

Page 17: Scala Day by Day

SCALA FUNCTIONAL

Page 18: Scala Day by Day

CLASS/INTERFACE CREATION

Page 19: Scala Day by Day

CASE CLASSES

Page 20: Scala Day by Day

PATTERN MATCHING

Page 21: Scala Day by Day

FUNCTIONS With default value

Page 22: Scala Day by Day

FUNCTIONS With named parameters

Page 23: Scala Day by Day

FUNCTIONS With nested functions

Page 24: Scala Day by Day

FUNCTIONS Passed as argumentReturned as value

Page 25: Scala Day by Day

SCALA PATTERNS Interesting/Weird Things

Page 26: Scala Day by Day

TUPLES

Page 27: Scala Day by Day

IMUTABILITY VS MUTABILITY Problem:“Separate students from a given List into separate groups: those from Romania and those outside of Romania”

Page 28: Scala Day by Day

IMUTABILITY VS MUTABILITY

Page 29: Scala Day by Day

IMUTABILITY VS MUTABILITY

Page 30: Scala Day by Day

IMUTABILITY VS MUTABILITY

Page 31: Scala Day by Day

IMUTABILITY VS MUTABILITY

Page 32: Scala Day by Day

MONADS

Page 33: Scala Day by Day

Pug monad

Page 34: Scala Day by Day

OPTION

Page 35: Scala Day by Day

EITHER

Page 36: Scala Day by Day

FUTURE

Page 37: Scala Day by Day

TRY

Page 38: Scala Day by Day

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. ……

Page 39: Scala Day by Day
Page 40: Scala Day by Day

WHAT MEANS TO WORK WITH SCALA?

Page 41: Scala Day by Day

“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

Page 42: Scala Day by Day

QUESTIONS

Page 43: Scala Day by Day

THE END.

Page 44: Scala Day by Day
Page 45: Scala Day by Day

SCALA DAY BY DAYIonut Andonescu

23 April 2016, Iasi

Please fill the online evaluation form after event