As a full-time Scala developer, I often find myself talking about Scala and functional programming in different kinds of situations, ranging from meeting a friend working in J2EE, Ruby or C++, to dedicated Scala Meetups aiming to promote deeper understanding of the language. However, something occurred to me lately. By hanging out with people who have some Scala knowledge or experience, I am somewhat holding on to a safe place. By presenting only to people who are curious about Scala, I'm preaching to the converted.
To make a long story short, I recently made an attempt at getting out of my comfort zone by presenting about how making the transition from Java to Scala makes total sense (from Java developer point of view). The presentation went through proof-hearing of approximately 60 experienced Java programmers (with almost no prior Scala knowledge) gathered in one room for a Lunch & Learn. Here are my slides.
1. Part 1
Scala for … you ?
Katrin Shechtman, Senior Software Engineer at BoldRadius
2. 01
Who am I?
✤ C —> C++ —> Java —> Scala!
✤ https://ca.linkedin.com/in/
katrinshechtman!
✤ https://github.com/katrinsharp!
✤ @katrinsh!
✤ katrin.shechtman@boldradius.co
m
3. 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
?? ?? ?? ??
4. 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.
5. String greeting = "Hello World";
–There are so many things that compiler and I know!
6. String greeting = "Hello World";
–We both know that it is String, what else could it be?
7. greeting = "Hello World";
–Each of us also knows where the line ends..
8. greeting = "Hello World" .. Nice..!
But it is too simplistic. !
What about something beefy.. generics?
–The crowd
9. 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?
10. Why not:!
list = List<SpecialId>()!
or if you populate it right away:!
list = List(new SpecialId("1234"))
– Good question, heh?
11. greeting = "Hello World"!
list = List(new SpecialId("1234"))
–So far so good. What about SpecialId?
12. 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?
14. greeting = "Hello World"!
class SpecialId(String id)!
list = List(new SpecialId("1234"))
–Much more concise. What else?
15. class Service(String name)!
class Signup(SpecialId id, Service service)!
list = List<Signup>() //populated somewhere else
–How to get map of users per service?
16. ✤ 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?
17. 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.
18. So why not to focus on what really matters?!
m = list.groupingBy(signup -> signup.service)
–Why not?
19. 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.
20. Welcome to Scala -
JavaVM 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)
21. 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)
22. 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 = 12
def reminder(fraction: Int, denom: Int) = fraction % denom
23. 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)
24. 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
25. 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)
26. 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
27. 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)
28. Object Oriented
✤ No primitives: Int, Boolean etc!
✤ Traits - interfaces with methods!
✤ Classes and case classes!
✤ Single inheritance, multiple mix-ins
29. 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")
30. 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)
31. 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