SlideShare uma empresa Scribd logo
1 de 53
Baixar para ler offline
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?

Mais conteúdo relacionado

Mais procurados

Functional programming in scala
Functional programming in scalaFunctional programming in scala
Functional programming in scala
Stratio
 
Zend%20Certification%20REVIEW%20Document
Zend%20Certification%20REVIEW%20DocumentZend%20Certification%20REVIEW%20Document
Zend%20Certification%20REVIEW%20Document
tutorialsruby
 
OOP programming
OOP programmingOOP programming
OOP programming
anhdbh
 

Mais procurados (18)

Introduction to functional programming with java 8
Introduction to functional programming with java 8Introduction to functional programming with java 8
Introduction to functional programming with java 8
 
Functional programming in scala
Functional programming in scalaFunctional programming in scala
Functional programming in scala
 
Functional programming principles and Java 8
Functional programming principles and Java 8Functional programming principles and Java 8
Functional programming principles and Java 8
 
Ocl
OclOcl
Ocl
 
Object Oriented Programming Principles
Object Oriented Programming PrinciplesObject Oriented Programming Principles
Object Oriented Programming Principles
 
[OOP - Lec 01] Introduction to OOP
[OOP - Lec 01] Introduction to OOP[OOP - Lec 01] Introduction to OOP
[OOP - Lec 01] Introduction to OOP
 
Week 2: Getting Your Hands Dirty – Part 2
Week 2: Getting Your Hands Dirty – Part 2Week 2: Getting Your Hands Dirty – Part 2
Week 2: Getting Your Hands Dirty – Part 2
 
Scala Day by Day
Scala Day by DayScala Day by Day
Scala Day by Day
 
Zend%20Certification%20REVIEW%20Document
Zend%20Certification%20REVIEW%20DocumentZend%20Certification%20REVIEW%20Document
Zend%20Certification%20REVIEW%20Document
 
Java object oriented programming concepts - Brainsmartlabs
Java object oriented programming concepts - BrainsmartlabsJava object oriented programming concepts - Brainsmartlabs
Java object oriented programming concepts - Brainsmartlabs
 
Week 1: Getting Your Hands Dirty - Part 1
Week 1: Getting Your Hands Dirty - Part 1Week 1: Getting Your Hands Dirty - Part 1
Week 1: Getting Your Hands Dirty - Part 1
 
Oop Presentation
Oop PresentationOop Presentation
Oop Presentation
 
PL Lecture 01 - preliminaries
PL Lecture 01 - preliminariesPL Lecture 01 - preliminaries
PL Lecture 01 - preliminaries
 
PL Lecture 02 - Binding and Scope
PL Lecture 02 - Binding and ScopePL Lecture 02 - Binding and Scope
PL Lecture 02 - Binding and Scope
 
Java basics and java variables
Java basics and java variablesJava basics and java variables
Java basics and java variables
 
An Intro to Scala for PHP Developers
An Intro to Scala for PHP DevelopersAn Intro to Scala for PHP Developers
An Intro to Scala for PHP Developers
 
OOP programming
OOP programmingOOP programming
OOP programming
 
Functional programming
Functional programmingFunctional programming
Functional programming
 

Destaque

Destaque (15)

Building a Data Lake - An App Dev's Perspective
Building a Data Lake - An App Dev's PerspectiveBuilding a Data Lake - An App Dev's Perspective
Building a Data Lake - An App Dev's Perspective
 
Geek Night 16.0 - Evolution of Programming Languages
Geek Night 16.0 - Evolution of Programming LanguagesGeek Night 16.0 - Evolution of Programming Languages
Geek Night 16.0 - Evolution of Programming Languages
 
Planning and Optimizing Data Lake Architecture - Milos Milovanovic
 Planning and Optimizing Data Lake Architecture - Milos Milovanovic Planning and Optimizing Data Lake Architecture - Milos Milovanovic
Planning and Optimizing Data Lake Architecture - Milos Milovanovic
 
Hadoop con 2015 hadoop enables enterprise data lake
Hadoop con 2015   hadoop enables enterprise data lakeHadoop con 2015   hadoop enables enterprise data lake
Hadoop con 2015 hadoop enables enterprise data lake
 
Talend winter 2017 overview webinar
Talend winter 2017 overview webinarTalend winter 2017 overview webinar
Talend winter 2017 overview webinar
 
Présentation de Talend Winter 2017
Présentation de Talend Winter 2017 Présentation de Talend Winter 2017
Présentation de Talend Winter 2017
 
Blockchain in Banking: A Measured Approach
Blockchain in Banking: A Measured ApproachBlockchain in Banking: A Measured Approach
Blockchain in Banking: A Measured Approach
 
How First Principles, Advice & AI is killing Banking
How First Principles, Advice & AI is killing BankingHow First Principles, Advice & AI is killing Banking
How First Principles, Advice & AI is killing Banking
 
Machine learning for_finance
Machine learning for_financeMachine learning for_finance
Machine learning for_finance
 
Machine learning use cases in finance
Machine learning use cases in financeMachine learning use cases in finance
Machine learning use cases in finance
 
Building the Data Lake with Azure Data Factory and Data Lake Analytics
Building the Data Lake with Azure Data Factory and Data Lake AnalyticsBuilding the Data Lake with Azure Data Factory and Data Lake Analytics
Building the Data Lake with Azure Data Factory and Data Lake Analytics
 
Ibm cognitive commerce for banking final
Ibm cognitive commerce for banking finalIbm cognitive commerce for banking final
Ibm cognitive commerce for banking final
 
Modern Data Architecture for a Data Lake with Informatica and Hortonworks Dat...
Modern Data Architecture for a Data Lake with Informatica and Hortonworks Dat...Modern Data Architecture for a Data Lake with Informatica and Hortonworks Dat...
Modern Data Architecture for a Data Lake with Informatica and Hortonworks Dat...
 
Big data architectures and the data lake
Big data architectures and the data lakeBig data architectures and the data lake
Big data architectures and the data lake
 
Top Ten Trends in Banking 2017
Top Ten Trends in Banking 2017Top Ten Trends in Banking 2017
Top Ten Trends in Banking 2017
 

Semelhante a GeekNight 22.0 Multi-paradigm programming in Scala and Akka

Functional Programming in Ruby
Functional Programming in RubyFunctional Programming in Ruby
Functional Programming in Ruby
Alex Teut
 

Semelhante a GeekNight 22.0 Multi-paradigm programming in Scala and Akka (20)

Scala and Akka together - geek night jan 2017
Scala and Akka together - geek night jan 2017Scala and Akka together - geek night jan 2017
Scala and Akka together - geek night jan 2017
 
Functional programming
Functional programmingFunctional programming
Functional programming
 
Introduction to Functional programming
Introduction to Functional programmingIntroduction to Functional programming
Introduction to Functional programming
 
Yes scala can!
Yes scala can!Yes scala can!
Yes scala can!
 
Reactive Software Systems
Reactive Software SystemsReactive Software Systems
Reactive Software Systems
 
Introduction to concurrent programming with akka actors
Introduction to concurrent programming with akka actorsIntroduction to concurrent programming with akka actors
Introduction to concurrent programming with akka actors
 
Introduction to concurrent programming with Akka actors
Introduction to concurrent programming with Akka actorsIntroduction to concurrent programming with Akka actors
Introduction to concurrent programming with Akka actors
 
Functional programming with Java 8
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8
 
Software Craftmanship - Cours Polytech
Software Craftmanship - Cours PolytechSoftware Craftmanship - Cours Polytech
Software Craftmanship - Cours Polytech
 
Functional Programming in Ruby
Functional Programming in RubyFunctional Programming in Ruby
Functional Programming in Ruby
 
8 introduction to_java_script
8 introduction to_java_script8 introduction to_java_script
8 introduction to_java_script
 
Anatomy of spark catalyst
Anatomy of spark catalystAnatomy of spark catalyst
Anatomy of spark catalyst
 
Scala: functional programming for the imperative mind
Scala: functional programming for the imperative mindScala: functional programming for the imperative mind
Scala: functional programming for the imperative mind
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
 
Functional programming in Scala
Functional programming in ScalaFunctional programming in Scala
Functional programming in Scala
 
I know Java, why should I consider Clojure?
I know Java, why should I consider Clojure?I know Java, why should I consider Clojure?
I know Java, why should I consider Clojure?
 
Concurrency - Why it's hard ?
Concurrency - Why it's hard ?Concurrency - Why it's hard ?
Concurrency - Why it's hard ?
 
A sip of elixir
A sip of elixirA sip of elixir
A sip of elixir
 
Why Concurrency is hard ?
Why Concurrency is hard ?Why Concurrency is hard ?
Why Concurrency is hard ?
 
Rubyslava slides-26.09.2013
Rubyslava slides-26.09.2013Rubyslava slides-26.09.2013
Rubyslava slides-26.09.2013
 

Mais de GeekNightHyderabad

Mais de GeekNightHyderabad (20)

Testing strategies in microservices
Testing strategies in microservicesTesting strategies in microservices
Testing strategies in microservices
 
Metaprogramming ruby
Metaprogramming rubyMetaprogramming ruby
Metaprogramming ruby
 
Scaling enterprise digital platforms with kubernetes
Scaling enterprise digital platforms with kubernetesScaling enterprise digital platforms with kubernetes
Scaling enterprise digital platforms with kubernetes
 
FreedomBox & Community Wi-Fi networks
FreedomBox & Community Wi-Fi networksFreedomBox & Community Wi-Fi networks
FreedomBox & Community Wi-Fi networks
 
Rendezvous with aucovei (autonomous connected car)
Rendezvous with aucovei (autonomous connected car)Rendezvous with aucovei (autonomous connected car)
Rendezvous with aucovei (autonomous connected car)
 
Role of AI & ML in beauty care industry
Role of AI & ML in beauty care industryRole of AI & ML in beauty care industry
Role of AI & ML in beauty care industry
 
Breaking down a monolith
Breaking down a monolithBreaking down a monolith
Breaking down a monolith
 
Design lean agile_thinking presentation
Design lean agile_thinking presentationDesign lean agile_thinking presentation
Design lean agile_thinking presentation
 
Scaling pipelines
Scaling pipelinesScaling pipelines
Scaling pipelines
 
Blockchain beyond bitcoin
Blockchain beyond bitcoinBlockchain beyond bitcoin
Blockchain beyond bitcoin
 
Http/2
Http/2Http/2
Http/2
 
Hardware hacking and internet of things
Hardware hacking and internet of thingsHardware hacking and internet of things
Hardware hacking and internet of things
 
Spring to Cloud - REST To Microservices
Spring to Cloud - REST To MicroservicesSpring to Cloud - REST To Microservices
Spring to Cloud - REST To Microservices
 
Serverless
ServerlessServerless
Serverless
 
Building Cloud Native Applications Using Spring Boot and Spring Cloud
Building Cloud Native Applications Using Spring Boot and Spring CloudBuilding Cloud Native Applications Using Spring Boot and Spring Cloud
Building Cloud Native Applications Using Spring Boot and Spring Cloud
 
Progressive Web Applications - The Next Gen Web Technologies
Progressive Web Applications - The Next Gen Web TechnologiesProgressive Web Applications - The Next Gen Web Technologies
Progressive Web Applications - The Next Gen Web Technologies
 
Scaling a Game Server: From 500 to 100,000 Users
Scaling a Game Server: From 500 to 100,000 UsersScaling a Game Server: From 500 to 100,000 Users
Scaling a Game Server: From 500 to 100,000 Users
 
Big Data - Need of Converged Data Platform
Big Data - Need of Converged Data PlatformBig Data - Need of Converged Data Platform
Big Data - Need of Converged Data Platform
 
Understanding the Intelligent Cloud
Understanding the Intelligent CloudUnderstanding the Intelligent Cloud
Understanding the Intelligent Cloud
 
Geek Night 17.0 - Artificial Intelligence and Machine Learning
Geek Night 17.0 - Artificial Intelligence and Machine LearningGeek Night 17.0 - Artificial Intelligence and Machine Learning
Geek Night 17.0 - Artificial Intelligence and Machine Learning
 

Último

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Último (20)

Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 

GeekNight 22.0 Multi-paradigm programming in Scala and Akka

  • 2. Why not both? You really don’t have to choose just one.
  • 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?
  • 6. ➢ What is the Best code?
  • 7. ➢ What is the Best code? ○ The following =>
  • 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!
  • 9.
  • 10. ➢ Get all products from the orders
  • 11. ➢ Get all products from the orders Or
  • 12. ➢ Sort the products by Id Or
  • 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.
  • 14. ➢ Why learn Functional Programming ?
  • 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.
  • 16. ➢ How does FP improve code?
  • 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.
  • 20. ➢ What makes Scala functional ?
  • 21. ➢ Functions are 1st class citizens => ○ Instead =>
  • 22. ➢ Functions are 1st class citizens => ○ Instead =>
  • 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!
  • 27.
  • 28. String is referentially transparent and holds substitution model.
  • 29. ➢ Similarly, add() here is also without side-effects and referentially transparent.
  • 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.
  • 31. ➢ Just enough Scala for Akka.
  • 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.
  • 33. ➢ Pattern matching ○ Switch case on steroids
  • 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 =>
  • 36.
  • 37.
  • 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
  • 46. State
  • 48. Let’s look at an actor
  • 49. DEMO
  • 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
  • 52. Start thinking in Hybrids