Why Not Make the Transition from Java to Scala?

Post on 28-Jul-2015

1.677 views 1 download

Tags:

Transcript of Why Not Make the Transition from Java to Scala?

Part 1

Scala for … you ?Katrin Shechtman, Senior Software Engineer at BoldRadius

01

Who am I? ✤ C —> C++ —> Java —> Scala!

✤ https://ca.linkedin.com/in/katrinshechtman!

✤ https://github.com/katrinsharp!

✤ @katrinsh!

✤ katrin.shechtman@boldradius.com

Who are you?

Feeling about Scala

Java Developer DBA DevOps/

SysAdmin ETL Engineer

I don’t like it! ?? ?? ?? ??

I don’t really care ?? ?? ?? ??

I’m curios to know more ?? ?? ?? ??

I’m totally in with it ?? ?? ?? ??

Let’s talk about Java!

Who is on with Java 8? Lambdas?There will be some code examples using Java 8, but don’t worry, its knowledge is only nice to have for this presentation.

String greeting = "Hello World";

–There are so many things that compiler and I know!

String greeting = "Hello World";

–We both know that it is String, what else could it be?

greeting = "Hello World";

–Each of us also knows where the line ends..

greeting = "Hello World" .. Nice..!

But it is too simplistic. !

What about something beefy.. generics?

–The crowd

class SpecialId {!! public String id;!! public SpecialId(String id) {!! ! this.id = id; !! }!} List<SpecialId> list = new LinkedList<SpecialId>(); list.add(new SpecialId("1234"));

–Doesn’t compiler know that it is a list of (at least) SpecialIds?

Why not:!

list = List<SpecialId>()!

or if you populate it right away:!

list = List(new SpecialId("1234"))

– Good question, heh?

greeting = "Hello World"!

list = List(new SpecialId("1234"))

–So far so good. What about SpecialId?

class SpecialId {!! public String id;!! public SpecialId(String id) {!! ! this.id = id; !! }!}

–What if we could just let compiler know what members the class has and compiler will do the rest?

class SpecialId(String id)!

id = SpecialId("1234")

–Remember the compiler knows that id is type of SpecialId

greeting = "Hello World"!

class SpecialId(String id)!

list = List(new SpecialId("1234"))

–Much more concise. What else?

class Service(String name)!

class Signup(SpecialId id, Service service)!

list = List<Signup>() //populated somewhere else

–How to get map of users per service?

✤ ol’ loop over iterator. !

✤ Java 8 lambdas: Map<Service, List<Signup>> m = list.stream() .collect(Collectors.groupingBy((signup) -> signup.service));

How to get map of users per service?

Map<Service, List<Signup>> m = list.stream() .collect(Collectors. groupingBy((signup) -> signup.service));

–What really matters here is collection (list), action (grouping by) and mapping function.

So why not to focus on what really matters?!

m = list.groupingBy(signup -> signup.service)

–Why not?

greeting = "Hello World"!

class SpecialId(String id)!

list = List(new SpecialId(“1234"))!

m = list.groupingBy(signup -> signup.service)

–This looks like more succinct language syntax.

Welcome to Scala - Java VM based and functional

language

val greeting = "Hello World"!

case class SpecialId(String id)!

case class Signup(id: SpecialId, service: Service)!

val signupList = List(Signup(SpecialId("1234"), Service("service1")), …)!

val m = signupList.groupBy(signup => signup.service)

Scala in a nutshell

✤ Statically typed + type inference.!

✤ Immutability: val vs var . !

✤ Built-in support for better equals, hashCode and toString in case classes.!

✤ Object Oriented: no primitives, classes, inheritance, traits mix-ins.

Part 1 (spot part 2 for other Scala features)

Statically typed w/ type inference

✤ Statically typed: val shouldBeString: String = 12 //type mismatch; found : Int(12) required: String def reminder(fraction: Int, denom: Int): Int = { fraction % denom } !

✤ Type inference: val willBeInt = 12def reminder(fraction: Int, denom: Int) = fraction % denom

Scala in a nutshell

✤ Statically typed + type inference.!

✤ Immutability: val vs var . !

✤ Built-in support for better equals, hashCode and toString in case classes.!

✤ Object Oriented: no primitives, classes, inheritance, traits mix-ins.

Part 1 (spot part 2 for other Scala features)

Immutability

✤ Mutable vs immutable: val str1 = "immutable string" var str2 = "I can change" str1 = "Compilation error..." //reassignment to val str2 = "Now I'm different, no problem”!

✤ Mutable variable vs mutable collection: var mutableList = List("1", "2") mutableList = mutableList :+ "3" val mutableList2 = MutableList("1", "2") mutableList2 += “3"!

✤ Everything is expression: val isSomething = if(cond) true else false

Scala in a nutshell

✤ Statically typed + type inference.!

✤ Immutability: val vs var . !

✤ Built-in support for better equals, hashCode and toString in case classes.!

✤ Object Oriented: no primitives, classes, inheritance, traits mix-ins.

Part 1 (spot part 2 for other Scala features)

Remember Signup case class?

✤ toString():println(Signup(SpecialId("1"), Service("s1"))) will print Signup(SpecialId(1),Service(s1))!

✤ equals(): val signup = Signup(SpecialId("1"), Service(“s1")) val signup2 = Signup(SpecialId("1"), Service("s1")) signup == signup2 // will return true

Scala in a nutshell

✤ Statically typed + type inference.!

✤ Immutability: val vs var . !

✤ Built-in support for better equals, hashCode and toString in case classes.!

✤ Object Oriented: no primitives, classes, inheritance, traits mix-ins.

Part 1 (spot part 2 for other Scala features)

Object Oriented

✤ No primitives: Int, Boolean etc!

✤ Traits - interfaces with methods!

✤ Classes and case classes!

✤ Single inheritance, multiple mix-ins

trait Fruit {! ! def isGreen: Boolean! ! def isLowCalories = false! } ! trait Size {! ! def size: String! } ! trait Apple extends Fruit {! ! val isLowCalories = isGreen! } ! class GreenApple(name: String) extends Apple {! ! val isGreen = true! }!case class MyFavoriteFruit(name: String) extends GreenApple(name) with Size {!! def size = "very big"!}!MyFavoriteFruit("smith")

Scala in a nutshell

✤ Statically typed + type inference.!

✤ Immutability: val vs var . !

✤ Built-in support for better equals, hashCode and toString in case classes.!

✤ Object Oriented: no primitives, classes, inheritance, traits mix-ins.

Part 1 (spot part 2 for other Scala features)

See you next part! Meanwhile..

✤ Functional Programming Principles with Martin Odersky: https://www.coursera.org/course/progfun!

✤ Learn Scala for Java Developers by Toby Weston: http://www.amazon.com/Learn-Scala-Java-Developers-Weston-ebook/dp/B00WIQKR9I!

✤ How to learn Scala by BoldRadius: http://guides.co/guide/how-to-learn-scala