SlideShare a Scribd company logo
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rights
RXJS
TypeScript
Functional P.
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Agenda
• Functional Programming.
• TypeScript.
• Reactive programming.
2
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Functional Programming in JS
• JS is a multi-paradigm language. That means it
doesn't restrict you to one particular paradigm
when writing code.
• JS is not optimized to be a Functional
Programming language. However, it has many of
the tools you need for FP.
• Using FP concepts in JS does not mean you have
to necessarily abandon other paradigms.
3
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
FP VS OOP
• What is OOP Paradigm.
• What is Functional Programming Paradigm.
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
What is OOP Paradigm
Classes
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Functional Programming
Functions
R1
R2
R3
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
{
name: “Vivek”,
sayHi: () => `Hello, ${name}`
}
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Principles of FP
• Pure functions / Avoid side effects
• Higher order function.
• Function Currying.
• Function Composition over inheritance.
• Break functionality into reusable functions with single
responsibility.
• Avoid mutating state / Immutability .
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Pure Functions
• Any pure function simply maps input to output
that’s it.
• A function is called pure if it meets the following
two conditions:
1. There are no side-effects.
2. Given the same input, will always return the same
output.
10
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Pure function
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Examples of Impure Functions …
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Don’t Mutate Input
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Not pure function
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Do not depend on global/shared
properties
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Don’t depend on external State
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
No Ajax Calls
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Avoiding side effects
• Don't mutate the input
• Don't modify any external variables/properties/
state
• Don't make any AJAX calls
• Don't manipulate the DOM
18
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Benefits of Pure Function
• Can memoize output
• Easy to test.
• No shared state, So highly immune to bugs.
• Easy for parallel processing.
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Function currying
Demo …
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Higher Order Functions
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Function Composition
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Function Composition
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
What is TypeScript
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Basic Types
Demo …
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Classes
Demo …
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Interfaces
Demo …
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Generics
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Decorators
Demo …
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Why Typescript
• Throws error in during development and saves time.
• Help avoid silent errors, like passing string instead of
number.
• Typescript helps to write static typed code.
• Typescript provides improved IntelliSense,
suggestions.
• Helps a lot while refactoring.
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
IntelliSense and errors
Demo …
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Reactive
Programming
Reactive Programming is an asynchronous
programming paradigm concerned with data
streams and the propagation of change
Describe your application in terms of data
flowing through pipelines of transformations (
respond to those pipelines & produce some sort
of result on the user interface )
In simple words, in reactive programming - data
flows emitted by one component and the
underlying structure provided by the language
will propagate those changes to all the other
components that are registered to receive those
changes
32
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Reactive
Programming
33
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Reactive
Programming
34
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
RxJS
• Library to build reactive applications
• JavaScript implementation of the ReactiveX API
• Build asynchronous, modular event-based apps
• Easily create event streams or data streams
• Compose or transform streams with query like
operators
• Allows streams of data to be observed – Listen to
any observable stream to perform side effects
35
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
ReactiveX ?
• An API for async programming with
observable streams
• More than an API, it's an idea
• The observer pattern done right!
• Combination of the best ideas from observer
pattern, iterator pattern and functional
programming
• Implementations -
– Java : RxJava
– JavaScript : RxJS
– Scala : RxScala
– and a lot more …
36
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
The Observer
design
pattern
• Observer pattern defines a one-to-many
dependency between objects such that when one
object's state changes, all its dependents are
notified and updated automatically.
• In observer pattern, there are many observers
(subscriber objects) that are observing a subject
(publisher object). Observers register themselves
to a subject to get a notification when there is a
change in the subject's state
• An observer object can register or unregister from
a subject at any point in time
37
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
The Observer
design
pattern
38
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
The Observer design pattern
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
What are Observables ?
• Lazy push collections
of multiple values.
They fill the missing
spot in the
displayed table
40
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
What is an
Observable ?
• Observable is just a function that
takes an observer and returns a
function
• Shape:
– A function
– That accepts an observer: An
object with `next`, `error` and
`complete` methods on it
– And returns a cancellation
function
• Purpose:
– To connect the observer to
something that produces values
(a producer) and return a
means to tear down that
connection to the producer.
The observer is really a registry
of handlers that can be pushed
values over time
41
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
What is an Observer?
• Is a collection of callbacks that knows how to listen to values delivered by the
Observable. An object with `next`, `error` and `complete` methods on it
• When we talk about RxJS or Reactive programming, generally observables get top
billing. But the observer implementation is the workhorse of this type of reactive
programming. Observables are inert. They’re just functions. They sit there until
you `subscribe` to them, they set up your observer, and they’re done, back to
being boring old functions waiting to be called. The observers on the other hand,
stay active and listen for events from your producers
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Demo 1 : Let's create
an Observable
43
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
From Promiseland
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
To Observableland
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Promises vs Observables
46
• Single emission of value
• Eager
Promises
• Multi emission of values
• Lazy
Observables
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Operators
• Also Just Functions
• An operator in RxJS is little more than a function that takes a source observable,
and returns a new observable that will subscribe to that source observable when
you subscribe to it
• Some common operators :
– Map, filter, reduce
– scan
– combineLatest
– WithLatestFrom
– etc
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Example – The map operator
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Hot vs Cold
Observable
• Unicast vs Multicast
49
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Cold Observable
• COLD is when your observable creates the producer
• An observable is “cold” if its underlying producer is created and activated during
subscription. This means, that if observables are functions, then the producer is
created and activated by calling that function
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Cold Observable
• Example :
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Hot Observable
• HOT is when your observable closes over the producer
• An observable is “hot” if its underlying producer is either created or activated
outside of subscription
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Hot Observable
• Example :
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Subjects An RxJS Subject is a special type of
Observable that allows values to be multi-
casted to many Observers. While plain
Observables are unicast (each subscribed
Observer owns an independent execution of
the Observable), Subjects are multicast
54
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Subjects But how does it multicast?
55
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Subjects
• Every Subject is an Observable. Given a Subject, you
can subscribe to it, providing an Observer, which will
start receiving values normally. From the perspective
of the Observer, it cannot tell whether the Observable
execution is coming from a plain unicast Observable
or a Subject
• Every Subject is an Observer. It is an object with the
methods next(v), error(e), and complete(). To feed a
new value to the Subject, just call next(theValue), and
it will be multi-casted to the Observers registered to
listen to the Subject
• Internally to the Subject, subscribe does not invoke a
new execution that delivers values. It simply registers
the given Observer in a list of Observers, similarly to
how addListener usually works in other libraries and
languages
56
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Subjects -
remember
the observer
design
pattern?
57
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Cold to Hot using Subjects
• How to convert Cold Observables to Hot using Subjects?
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Types of Subject • Async Subject
• Behavior Subject
• Replay Subject
59
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Subject vs
Observable
• The most important distinction between a
Subject and an Observable is that a Subject
has state, it keeps a list of observers. On the
other hand, an Observable is just a
function that sets up observation
• While Subjects are Observables, Subjects
also implement an Observer interface. They
have next, error, and complete methods on
them. These methods are used to notify
their counterparts on observers in the
subject’s internal observers list. This means
a subject can be used as an observer to
subscribe to any observable.
60
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Demo 6 : A Reactive
example
61
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Thank you …

More Related Content

PPTX
Final ppt
PPTX
How to become rpa developer
PDF
DEVOXX UK 2018 - GraphQL as an alternative approach to REST
PDF
Oracle API Platform Cloud Service Best Practices & Lessons Learnt
PDF
I Love APIs 2015: Create Design-driven APIs with Node.js and Swagger
PDF
apidays LIVE Paris 2021 - Spatially enabling Web APIs through OGC Standards ...
PDF
AdaCore Paris Tech Day 2016: Pierre-Marie Rodat - Libadalang, New Generation ...
PDF
Ros: 站在巨人的肩膀上
Final ppt
How to become rpa developer
DEVOXX UK 2018 - GraphQL as an alternative approach to REST
Oracle API Platform Cloud Service Best Practices & Lessons Learnt
I Love APIs 2015: Create Design-driven APIs with Node.js and Swagger
apidays LIVE Paris 2021 - Spatially enabling Web APIs through OGC Standards ...
AdaCore Paris Tech Day 2016: Pierre-Marie Rodat - Libadalang, New Generation ...
Ros: 站在巨人的肩膀上

Similar to Functional programming, TypeScript and RXJS (20)

PPTX
Luis Atencio on RxJS
PPTX
Functional reactive programming
PDF
NGRX Apps in Depth
PPTX
Functional Reactive Programming with RxJS
PPTX
Reactive programming with rx java
PDF
Predictable reactive state management - ngrx
PPTX
Solve it Differently with Reactive Programming
PDF
DMCA#21: reactive-programming
PDF
RxSwift
PDF
Rx Swift
PDF
Reactive programming and RxJS
PDF
My Gentle Introduction to RxJS
PDF
DZone_RC_RxJS
PDF
Functional programming 101
PPTX
Reactive programming every day
PDF
Cycle.js - Functional reactive UI framework (Nikos Kalogridis)
PDF
Cycle.js - A functional reactive UI framework
PDF
Reactive programming with cycle.js
PPTX
Reactive Programming in Java 8 with Rx-Java
PDF
Rxjs In Action Covers Rxjs 5 1st Edition Paul P Daniels Luis Atencio
Luis Atencio on RxJS
Functional reactive programming
NGRX Apps in Depth
Functional Reactive Programming with RxJS
Reactive programming with rx java
Predictable reactive state management - ngrx
Solve it Differently with Reactive Programming
DMCA#21: reactive-programming
RxSwift
Rx Swift
Reactive programming and RxJS
My Gentle Introduction to RxJS
DZone_RC_RxJS
Functional programming 101
Reactive programming every day
Cycle.js - Functional reactive UI framework (Nikos Kalogridis)
Cycle.js - A functional reactive UI framework
Reactive programming with cycle.js
Reactive Programming in Java 8 with Rx-Java
Rxjs In Action Covers Rxjs 5 1st Edition Paul P Daniels Luis Atencio
Ad

Recently uploaded (20)

PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
Well-logging-methods_new................
PPTX
Construction Project Organization Group 2.pptx
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
Internet of Things (IOT) - A guide to understanding
PPTX
Geodesy 1.pptx...............................................
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
Lecture Notes Electrical Wiring System Components
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
Welding lecture in detail for understanding
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
R24 SURVEYING LAB MANUAL for civil enggi
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Well-logging-methods_new................
Construction Project Organization Group 2.pptx
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Internet of Things (IOT) - A guide to understanding
Geodesy 1.pptx...............................................
Embodied AI: Ushering in the Next Era of Intelligent Systems
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Lecture Notes Electrical Wiring System Components
bas. eng. economics group 4 presentation 1.pptx
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Welding lecture in detail for understanding
Model Code of Practice - Construction Work - 21102022 .pdf
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
R24 SURVEYING LAB MANUAL for civil enggi
Ad

Functional programming, TypeScript and RXJS

  • 1. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rights RXJS TypeScript Functional P.
  • 2. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Agenda • Functional Programming. • TypeScript. • Reactive programming. 2
  • 3. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Functional Programming in JS • JS is a multi-paradigm language. That means it doesn't restrict you to one particular paradigm when writing code. • JS is not optimized to be a Functional Programming language. However, it has many of the tools you need for FP. • Using FP concepts in JS does not mean you have to necessarily abandon other paradigms. 3
  • 4. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights FP VS OOP • What is OOP Paradigm. • What is Functional Programming Paradigm.
  • 5. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights What is OOP Paradigm Classes
  • 6. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
  • 7. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Functional Programming Functions R1 R2 R3
  • 8. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights { name: “Vivek”, sayHi: () => `Hello, ${name}` }
  • 9. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Principles of FP • Pure functions / Avoid side effects • Higher order function. • Function Currying. • Function Composition over inheritance. • Break functionality into reusable functions with single responsibility. • Avoid mutating state / Immutability .
  • 10. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Pure Functions • Any pure function simply maps input to output that’s it. • A function is called pure if it meets the following two conditions: 1. There are no side-effects. 2. Given the same input, will always return the same output. 10
  • 11. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Pure function
  • 12. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Examples of Impure Functions …
  • 13. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Don’t Mutate Input
  • 14. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Not pure function
  • 15. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Do not depend on global/shared properties
  • 16. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Don’t depend on external State
  • 17. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights No Ajax Calls
  • 18. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Avoiding side effects • Don't mutate the input • Don't modify any external variables/properties/ state • Don't make any AJAX calls • Don't manipulate the DOM 18
  • 19. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Benefits of Pure Function • Can memoize output • Easy to test. • No shared state, So highly immune to bugs. • Easy for parallel processing.
  • 20. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Function currying Demo …
  • 21. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Higher Order Functions
  • 22. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Function Composition
  • 23. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Function Composition
  • 24. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights What is TypeScript
  • 25. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Basic Types Demo …
  • 26. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Classes Demo …
  • 27. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Interfaces Demo …
  • 28. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Generics
  • 29. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Decorators Demo …
  • 30. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Why Typescript • Throws error in during development and saves time. • Help avoid silent errors, like passing string instead of number. • Typescript helps to write static typed code. • Typescript provides improved IntelliSense, suggestions. • Helps a lot while refactoring.
  • 31. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights IntelliSense and errors Demo …
  • 32. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Reactive Programming Reactive Programming is an asynchronous programming paradigm concerned with data streams and the propagation of change Describe your application in terms of data flowing through pipelines of transformations ( respond to those pipelines & produce some sort of result on the user interface ) In simple words, in reactive programming - data flows emitted by one component and the underlying structure provided by the language will propagate those changes to all the other components that are registered to receive those changes 32
  • 33. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Reactive Programming 33
  • 34. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Reactive Programming 34
  • 35. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights RxJS • Library to build reactive applications • JavaScript implementation of the ReactiveX API • Build asynchronous, modular event-based apps • Easily create event streams or data streams • Compose or transform streams with query like operators • Allows streams of data to be observed – Listen to any observable stream to perform side effects 35
  • 36. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights ReactiveX ? • An API for async programming with observable streams • More than an API, it's an idea • The observer pattern done right! • Combination of the best ideas from observer pattern, iterator pattern and functional programming • Implementations - – Java : RxJava – JavaScript : RxJS – Scala : RxScala – and a lot more … 36
  • 37. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights The Observer design pattern • Observer pattern defines a one-to-many dependency between objects such that when one object's state changes, all its dependents are notified and updated automatically. • In observer pattern, there are many observers (subscriber objects) that are observing a subject (publisher object). Observers register themselves to a subject to get a notification when there is a change in the subject's state • An observer object can register or unregister from a subject at any point in time 37
  • 38. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights The Observer design pattern 38
  • 39. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights The Observer design pattern
  • 40. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights What are Observables ? • Lazy push collections of multiple values. They fill the missing spot in the displayed table 40
  • 41. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights What is an Observable ? • Observable is just a function that takes an observer and returns a function • Shape: – A function – That accepts an observer: An object with `next`, `error` and `complete` methods on it – And returns a cancellation function • Purpose: – To connect the observer to something that produces values (a producer) and return a means to tear down that connection to the producer. The observer is really a registry of handlers that can be pushed values over time 41
  • 42. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights What is an Observer? • Is a collection of callbacks that knows how to listen to values delivered by the Observable. An object with `next`, `error` and `complete` methods on it • When we talk about RxJS or Reactive programming, generally observables get top billing. But the observer implementation is the workhorse of this type of reactive programming. Observables are inert. They’re just functions. They sit there until you `subscribe` to them, they set up your observer, and they’re done, back to being boring old functions waiting to be called. The observers on the other hand, stay active and listen for events from your producers
  • 43. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Demo 1 : Let's create an Observable 43
  • 44. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights From Promiseland
  • 45. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights To Observableland
  • 46. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Promises vs Observables 46 • Single emission of value • Eager Promises • Multi emission of values • Lazy Observables
  • 47. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Operators • Also Just Functions • An operator in RxJS is little more than a function that takes a source observable, and returns a new observable that will subscribe to that source observable when you subscribe to it • Some common operators : – Map, filter, reduce – scan – combineLatest – WithLatestFrom – etc
  • 48. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Example – The map operator
  • 49. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Hot vs Cold Observable • Unicast vs Multicast 49
  • 50. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Cold Observable • COLD is when your observable creates the producer • An observable is “cold” if its underlying producer is created and activated during subscription. This means, that if observables are functions, then the producer is created and activated by calling that function
  • 51. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Cold Observable • Example :
  • 52. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Hot Observable • HOT is when your observable closes over the producer • An observable is “hot” if its underlying producer is either created or activated outside of subscription
  • 53. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Hot Observable • Example :
  • 54. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Subjects An RxJS Subject is a special type of Observable that allows values to be multi- casted to many Observers. While plain Observables are unicast (each subscribed Observer owns an independent execution of the Observable), Subjects are multicast 54
  • 55. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Subjects But how does it multicast? 55
  • 56. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Subjects • Every Subject is an Observable. Given a Subject, you can subscribe to it, providing an Observer, which will start receiving values normally. From the perspective of the Observer, it cannot tell whether the Observable execution is coming from a plain unicast Observable or a Subject • Every Subject is an Observer. It is an object with the methods next(v), error(e), and complete(). To feed a new value to the Subject, just call next(theValue), and it will be multi-casted to the Observers registered to listen to the Subject • Internally to the Subject, subscribe does not invoke a new execution that delivers values. It simply registers the given Observer in a list of Observers, similarly to how addListener usually works in other libraries and languages 56
  • 57. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Subjects - remember the observer design pattern? 57
  • 58. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Cold to Hot using Subjects • How to convert Cold Observables to Hot using Subjects?
  • 59. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Types of Subject • Async Subject • Behavior Subject • Replay Subject 59
  • 60. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Subject vs Observable • The most important distinction between a Subject and an Observable is that a Subject has state, it keeps a list of observers. On the other hand, an Observable is just a function that sets up observation • While Subjects are Observables, Subjects also implement an Observer interface. They have next, error, and complete methods on them. These methods are used to notify their counterparts on observers in the subject’s internal observers list. This means a subject can be used as an observer to subscribe to any observable. 60
  • 61. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Demo 6 : A Reactive example 61
  • 62. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
  • 63. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Thank you …

Editor's Notes

  • #23: Function composition is the process of combining two or more functions to build a new bigger complicated function. Recommended to use composition over inheritance. To avoid complex hierarchies. const toSlug = compose( encodeURIComponent, join('-'), map(toLowerCase), split(' '));
  • #55: Show subject.js
  • #56: Show subject.js
  • #57: Show subject.js
  • #58: Show subject.js