GeekNight 22.0 Multi-paradigm programming in Scala and Akka

53
Multi-paradigm programming in Scala and Akka

Transcript of GeekNight 22.0 Multi-paradigm programming in Scala and Akka

Page 1: GeekNight 22.0 Multi-paradigm programming in Scala and Akka

Multi-paradigm programming in Scala and Akka

Page 2: GeekNight 22.0 Multi-paradigm programming in Scala and Akka

Why not both?

You really don’t have to choose just one.

Page 3: GeekNight 22.0 Multi-paradigm programming in Scala and Akka

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

Page 4: GeekNight 22.0 Multi-paradigm programming in Scala and Akka

⊸ 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.

Page 5: GeekNight 22.0 Multi-paradigm programming in Scala and Akka

➢ Why should you consider another programming paradigm?

Page 6: GeekNight 22.0 Multi-paradigm programming in Scala and Akka

➢ What is the Best code?

Page 7: GeekNight 22.0 Multi-paradigm programming in Scala and Akka

➢ What is the Best code?

○ The following =>

Page 8: GeekNight 22.0 Multi-paradigm programming in Scala and Akka

➢ 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!

Page 9: GeekNight 22.0 Multi-paradigm programming in Scala and Akka
Page 10: GeekNight 22.0 Multi-paradigm programming in Scala and Akka

➢ Get all products from the orders

Page 11: GeekNight 22.0 Multi-paradigm programming in Scala and Akka

➢ Get all products from the orders

Or

Page 12: GeekNight 22.0 Multi-paradigm programming in Scala and Akka

➢ Sort the products by Id

Or

Page 13: GeekNight 22.0 Multi-paradigm programming in Scala and Akka

➢ 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.

Page 14: GeekNight 22.0 Multi-paradigm programming in Scala and Akka

➢ Why learn Functional Programming ?

Page 15: GeekNight 22.0 Multi-paradigm programming in Scala and Akka

➢ 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.

Page 16: GeekNight 22.0 Multi-paradigm programming in Scala and Akka

➢ How does FP improve code?

Page 17: GeekNight 22.0 Multi-paradigm programming in Scala and Akka

➢ 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

Page 18: GeekNight 22.0 Multi-paradigm programming in Scala and Akka

➢ What is functional programming ?

○ Immutability.○ Referential transparency.○ Higher order functions.○ Tail call recursion, mapping, reducing, currying,

lazy evaluation,...

Page 19: GeekNight 22.0 Multi-paradigm programming in Scala and Akka

➢ 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.

Page 20: GeekNight 22.0 Multi-paradigm programming in Scala and Akka

➢ What makes Scala functional ?

Page 21: GeekNight 22.0 Multi-paradigm programming in Scala and Akka

➢ Functions are 1st class citizens =>

○ Instead =>

Page 22: GeekNight 22.0 Multi-paradigm programming in Scala and Akka

➢ Functions are 1st class citizens =>

○ Instead =>

Page 23: GeekNight 22.0 Multi-paradigm programming in Scala and Akka

➢ Immutability (preferred)

○ Preventing reference reassignment through val.○ Avoiding mutable state through immutable

collections.■ Eg : 2 threads referring to same tree.

Page 24: GeekNight 22.0 Multi-paradigm programming in Scala and Akka

➢ After one thread adds a node.

○ Immutability has benefits in concurrency as synchronization is not needed.

Page 25: GeekNight 22.0 Multi-paradigm programming in Scala and Akka

➢ Having referential transparency.○ Should hold substitution model.

➢ No side effects

Page 26: GeekNight 22.0 Multi-paradigm programming in Scala and Akka

➢ Having referential transparency.○ Should hold substitution model.

➢ No side effects

StringBuilder.append has side effects!

Page 27: GeekNight 22.0 Multi-paradigm programming in Scala and Akka
Page 28: GeekNight 22.0 Multi-paradigm programming in Scala and Akka

String is referentially transparent and holds substitution model.

Page 29: GeekNight 22.0 Multi-paradigm programming in Scala and Akka

➢ Similarly,

add() here is also without side-effects and referentially transparent.

Page 30: GeekNight 22.0 Multi-paradigm programming in Scala and Akka

➢ 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.

Page 31: GeekNight 22.0 Multi-paradigm programming in Scala and Akka

➢ Just enough Scala for Akka.

Page 32: GeekNight 22.0 Multi-paradigm programming in Scala and 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.

Page 33: GeekNight 22.0 Multi-paradigm programming in Scala and Akka

➢ Pattern matching○ Switch case on steroids

Page 34: GeekNight 22.0 Multi-paradigm programming in Scala and Akka

➢ 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 =>

Page 35: GeekNight 22.0 Multi-paradigm programming in Scala and Akka

➢ Pattern matching

Page 36: GeekNight 22.0 Multi-paradigm programming in Scala and Akka
Page 38: GeekNight 22.0 Multi-paradigm programming in Scala and Akka

Questions?

Page 39: GeekNight 22.0 Multi-paradigm programming in Scala and Akka

The Actor Model

⊸ Avoiding shared mutable state

▫ Functional Programming - all state is immutable

▫ Actor Model - state is mutable but encapsulated in actors

Page 40: GeekNight 22.0 Multi-paradigm programming in Scala and Akka

What’s an actor?

⊸ An actor has state⊸ An actor has behaviour⊸ ….

Sounds similar to something else, doesn’t it?

Page 41: GeekNight 22.0 Multi-paradigm programming in Scala and Akka

Actors are what “objects” were supposed to be

⊸ Actors are what objects are supposed to be (Smalltalk objects)

Page 42: GeekNight 22.0 Multi-paradigm programming in Scala and Akka

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

Page 43: GeekNight 22.0 Multi-paradigm programming in Scala and Akka

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

Page 44: GeekNight 22.0 Multi-paradigm programming in Scala and Akka

What is Akka?

Akka is a toolkit and runtime for building highly concurrent, distributed, and resilient message-driven applications on the JVM.

Page 45: GeekNight 22.0 Multi-paradigm programming in Scala and Akka

Anatomy of an actor

An akka actor must have⊸ State⊸ Behavior

▫ Message processing

Page 46: GeekNight 22.0 Multi-paradigm programming in Scala and Akka

State

Page 47: GeekNight 22.0 Multi-paradigm programming in Scala and Akka

Processing messages

Page 48: GeekNight 22.0 Multi-paradigm programming in Scala and Akka

Let’s look at an actor

Page 49: GeekNight 22.0 Multi-paradigm programming in Scala and Akka

DEMO

Page 50: GeekNight 22.0 Multi-paradigm programming in Scala and Akka

Scaling out in clusters

⊸ Location transparency with ActorRef

⊸ Akka’s cluster and cluster-sharding modules

⊸ Resilience with Supervision strategies

Page 51: GeekNight 22.0 Multi-paradigm programming in Scala and Akka

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

Page 52: GeekNight 22.0 Multi-paradigm programming in Scala and Akka

Start thinking in Hybrids

Page 53: GeekNight 22.0 Multi-paradigm programming in Scala and Akka

Questions?