3. Agenda for tonight
⊸ Demonstrate a blend of OOP and
FP in a hybrid language - Scala
⊸ Use Scala with Akka to easily
build scalable Actor Systems
4. ⊸ Disclaimer:
▫ This is not a talk about Scala as a language, its features or its
comparison with Java.
Don’t want to do this =>
▫ As polyglot programmers, we acknowledge that some languages
are better than others for the specific kind of problems that they
are trying to solve.
▫ Scala and Java are just being used as tools to understand and
help in embracing functional programming paradigm.
5. ➢ Why should you consider another programming
paradigm?
8. ➢ What is the Best code?
○ The best code is the one that doesn't exists!
Why?
○ Code rots.
○ needs maintenance.
○ needs to be tested.
○ needs to be read and understood again and again
over time.
Conciseness matters!
13. ➢ Why Scala?
○ Because Scala smoothens the transition from
OOP to FP.
○ Supplements Java and adds more features and
reduces the verbosity.
○ It is not new - first introduced in 2003.
15. ➢ Why learn Functional Programming ?
○ Write high-performing code that's easier to
understand and reason about.
○ Write programs that can utilize multiple cores
more efficiently.
○ It would evolve your way of thinking about
programs and eventually make you a better
programmer.
17. ➢ How does FP improve code?
○ Scalable and efficient code that can utilize
multiple cores better - Map/Reduce engines.
○ Better to reason about code. No need to create
mental model or remember gradual state
changes.
○ By emphasizing on the evaluation of expressions
instead of evaluation of statements
18. ➢ What is functional programming ?
○ Immutability.
○ Referential transparency.
○ Higher order functions.
○ Tail call recursion, mapping, reducing, currying,
lazy evaluation,...
19. ➢ What is functional programming ?
○ Immutability.
○ Referential transparency.
○ Higher order functions.
○ Tail call recursion, mapping, reducing, currying,
lazy evaluation,...
➢ Writing code without side effects.
It is a restriction on how we write programs and not
on what programs we can express.
23. ➢ Immutability (preferred)
○ Preventing reference reassignment through val.
○ Avoiding mutable state through immutable
collections.
■ Eg : 2 threads referring to same tree.
24. ➢ After one thread adds a node.
○ Immutability has benefits in concurrency as
synchronization is not needed.
25. ➢ Having referential transparency.
○ Should hold substitution model.
➢ No side effects
26. ➢ Having referential transparency.
○ Should hold substitution model.
➢ No side effects
StringBuilder.append has side effects!
30. ➢ Function with Side Effect
○ A function has side effects if it does something
other than simply return a result like
■ Modifying a variable/data structure.
■ Throwing exception or halting abruptly.
■ Reading/writing to file/database
■ …
➢ In a pure function
○ Input is obtained only by parameters.
○ Output is calculated and returned.
32. ➢ Case classes
○ Regular classes which export their constructor
parameters and which provide a recursive
decomposition mechanism via pattern
matching.
○ The params can be accessed with .(dot)
notation.
34. ➢ Pattern matching
○ A pattern match includes a sequence of
alternatives, each starting with the keyword
‘case’.
○ Each alternative case includes a pattern and
one or more expressions, which will be
evaluated if the pattern matches.
○ An arrow symbol => separates the pattern from
the expressions.
Example =>
39. The Actor Model
⊸ Avoiding shared mutable state
▫ Functional Programming - all state is immutable
▫ Actor Model - state is mutable but encapsulated in
actors
40. What’s an actor?
⊸ An actor has state
⊸ An actor has behaviour
⊸ ….
Sounds similar to something else, doesn’t it?
41. Actors are what “objects” were supposed to be
⊸ Actors are what objects are
supposed to be (Smalltalk
objects)
⊸
42. What’s an actor?
⊸ An actor has state
⊸ An actor has behaviour
Similar to OOP objects but with one difference
⊸ Actors communicate by sending immutable messages
to each other
43. Akka - actors on the JVM
⊸ Akka actors
▫ are light-weight JVM objects
▫ are guaranteed to run on a single thread (no
locks/synchronization required)
▫ process messages sent to them in order and one at
a time
▫ do not share their state
▫ can be addressed irrespective of location
▫ asynchronous and non-blocking
44. What is Akka?
Akka is a toolkit and runtime for building highly
concurrent, distributed, and resilient message-driven
applications on the JVM.
45. Anatomy of an actor
An akka actor must have
⊸ State
⊸ Behavior
▫ Message processing
50. Scaling out in clusters
⊸ Location transparency with ActorRef
⊸ Akka’s cluster and cluster-sharding modules
⊸ Resilience with Supervision strategies
51. Where to go from here
⊸ Take Martin Odersky’s course on Coursera
⊸ Try out some Lightbend Activator projects from
Github
⊸ Reach out the Akka team on Gitter and Google
Groups