GeekNight 22.0 Multi-paradigm programming in Scala and Akka
-
Upload
geeknighthyderabad -
Category
Technology
-
view
238 -
download
0
Transcript of GeekNight 22.0 Multi-paradigm programming in Scala and Akka
Multi-paradigm programming in Scala and Akka
Why not both?
You really don’t have to choose just one.
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
⊸ 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.
➢ Why should you consider another programming paradigm?
➢ What is the Best code?
➢ What is the Best code?
○ The following =>
➢ 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!
➢ Get all products from the orders
➢ Get all products from the orders
Or
➢ Sort the products by Id
Or
➢ 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.
➢ Why learn Functional Programming ?
➢ 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.
➢ How does FP improve code?
➢ 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
➢ What is functional programming ?
○ Immutability.○ Referential transparency.○ Higher order functions.○ Tail call recursion, mapping, reducing, currying,
lazy evaluation,...
➢ 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.
➢ What makes Scala functional ?
➢ Functions are 1st class citizens =>
○ Instead =>
➢ Functions are 1st class citizens =>
○ Instead =>
➢ Immutability (preferred)
○ Preventing reference reassignment through val.○ Avoiding mutable state through immutable
collections.■ Eg : 2 threads referring to same tree.
➢ After one thread adds a node.
○ Immutability has benefits in concurrency as synchronization is not needed.
➢ Having referential transparency.○ Should hold substitution model.
➢ No side effects
➢ Having referential transparency.○ Should hold substitution model.
➢ No side effects
StringBuilder.append has side effects!
String is referentially transparent and holds substitution model.
➢ Similarly,
add() here is also without side-effects and referentially transparent.
➢ 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.
➢ Just enough Scala for Akka.
➢ 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.
➢ Pattern matching○ Switch case on steroids
➢ 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 =>
➢ Pattern matching
Questions?
The Actor Model
⊸ Avoiding shared mutable state
▫ Functional Programming - all state is immutable
▫ Actor Model - state is mutable but encapsulated in actors
What’s an actor?
⊸ An actor has state⊸ An actor has behaviour⊸ ….
Sounds similar to something else, doesn’t it?
Actors are what “objects” were supposed to be
⊸ Actors are what objects are supposed to be (Smalltalk objects)
⊸
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
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
What is Akka?
Akka is a toolkit and runtime for building highly concurrent, distributed, and resilient message-driven applications on the JVM.
Anatomy of an actor
An akka actor must have⊸ State⊸ Behavior
▫ Message processing
State
Processing messages
Let’s look at an actor
DEMO
Scaling out in clusters
⊸ Location transparency with ActorRef
⊸ Akka’s cluster and cluster-sharding modules
⊸ Resilience with Supervision strategies
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
Start thinking in Hybrids
Questions?