why not make the transition from java to scala?

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

Upload: boldradius-solutions

Post on 28-Jul-2015

1.675 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Why Not Make the Transition from Java to Scala?

Part 1

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

Page 2: Why Not Make the Transition from Java to Scala?

01

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

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

✤ https://github.com/katrinsharp!

✤ @katrinsh!

[email protected]

Page 3: Why Not Make the Transition from Java to Scala?

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 ?? ?? ?? ??

Page 4: Why Not Make the Transition from Java to Scala?

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.

Page 5: Why Not Make the Transition from Java to Scala?

String greeting = "Hello World";

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

Page 6: Why Not Make the Transition from Java to Scala?

String greeting = "Hello World";

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

Page 7: Why Not Make the Transition from Java to Scala?

greeting = "Hello World";

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

Page 8: Why Not Make the Transition from Java to Scala?

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

But it is too simplistic. !

What about something beefy.. generics?

–The crowd

Page 9: Why Not Make the Transition from Java to Scala?

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?

Page 10: Why Not Make the Transition from Java to Scala?

Why not:!

list = List<SpecialId>()!

or if you populate it right away:!

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

– Good question, heh?

Page 11: Why Not Make the Transition from Java to Scala?

greeting = "Hello World"!

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

–So far so good. What about SpecialId?

Page 12: Why Not Make the Transition from Java to Scala?

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?

Page 13: Why Not Make the Transition from Java to Scala?

class SpecialId(String id)!

id = SpecialId("1234")

–Remember the compiler knows that id is type of SpecialId

Page 14: Why Not Make the Transition from Java to Scala?

greeting = "Hello World"!

class SpecialId(String id)!

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

–Much more concise. What else?

Page 15: Why Not Make the Transition from Java to Scala?

class Service(String name)!

class Signup(SpecialId id, Service service)!

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

–How to get map of users per service?

Page 16: Why Not Make the Transition from Java to Scala?

✤ 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?

Page 17: Why Not Make the Transition from Java to Scala?

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.

Page 18: Why Not Make the Transition from Java to Scala?

So why not to focus on what really matters?!

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

–Why not?

Page 19: Why Not Make the Transition from Java to Scala?

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.

Page 20: Why Not Make the Transition from Java to Scala?

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)

Page 21: Why Not Make the Transition from Java to Scala?

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)

Page 22: Why Not Make the Transition from Java to Scala?

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

Page 23: Why Not Make the Transition from Java to Scala?

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)

Page 24: Why Not Make the Transition from Java to Scala?

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

Page 25: Why Not Make the Transition from Java to Scala?

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)

Page 26: Why Not Make the Transition from Java to Scala?

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

Page 27: Why Not Make the Transition from Java to Scala?

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)

Page 28: Why Not Make the Transition from Java to Scala?

Object Oriented

✤ No primitives: Int, Boolean etc!

✤ Traits - interfaces with methods!

✤ Classes and case classes!

✤ Single inheritance, multiple mix-ins

Page 29: Why Not Make the Transition from Java to Scala?

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")

Page 30: Why Not Make the Transition from Java to Scala?

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)

Page 31: Why Not Make the Transition from Java to Scala?

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