Upgrade to Pro — share decks privately, control downloads, hide ads and more …

2. Functional Programming and RxJS

iliya
February 08, 2017
490

2. Functional Programming and RxJS

iliya

February 08, 2017
Tweet

Transcript

  1. Today's Schedule 1. Functional programming. 2. Functional concepts. 3. Category

    Theory. 4. Observer and Iterator pattern overview. 5. Working with asynchronous data streams - RxJS
  2. Why Functional Programming in Angular Course ? • RxJS -

    Asynchronous data streams • ngrx - Reactive State Management
  3. JS array methods we need to know • map(fn) -

    fn will be called for each element of the array with args: (currentValue, index, array) and the returned value will be the new element in the result array. • filter(fn) - fn will be called for each element and it needs to return true if the current value should be added to the result array and false otherwise. • reduce(accFn, acc) - accFn will be called for each element of the array with argumenst (accumulator, currentValue, index). If its the first call accumulator will be acc from the reduce call. Otherwise it will be the previously returned result from the accFn.
  4. Functional Programming Functional programming is a programming paradigm that treats

    computation as the evaluation of mathematical functions and avoids changing-state and mutable data. It is a declarative programming paradigm, which means programming is done with expressions or declarations instead of statements.
  5. Functional Programming Advantages • Clean code - easier to test,

    debug and reason about. • Modularity - large problems are broken into small parts • Reusability - common helper functions, due to the modularity • Reduced coupling - Less amount of dependency between modules
  6. Functional concepts • Using functions and arrays for flow control

    • Pure functions, arrow functions, anonymous functions, recursive functions • Functions as First Class Citizens • Using: map(), filter() and reduce() • Currying and Partial Application • Lazy evaluation • Composition (Math)
  7. Functions - First Class Citizens • Functions can be assigned

    to variables • Functions can be passed as arguments
  8. Pure Functions • The function always evaluates the same result

    value given the same argument value(s) • Evaluation of the result does not cause any semantically observable side effect or output, such as mutation of mutable objects or output to I/O devices
  9. Currying and Partial Application • Currying is the technique of

    translating the evaluation of a function that takes multiple arguments into evaluating a sequence of functions. • Partial application refers to the process of fixing a number of arguments to a function, producing another function of smaller arity.
  10. Lazy evaluation • Call-by-need and deffered execution, is an evaluation

    strategy that waits until the value is needed to compute the result of a function. • Using it can result in a major increase in performance
  11. Functors Functors are mappings between categories.
 
 
 Functions that

    lift values out of a container, morph them, and put them into a new container. The first input is a morphism for the type and the second input is the container.
  12. Iterator Pattern • Used for traversing collections of objects •

    It provides a simple method for selecting, sequentially, the next item in the collection. • An iterator can be used to generate values
  13. Reactive Extensions • Library for composing asynchronous and event-based programs

    by using observable sequences. • Extends the observer pattern to support sequences of data and/ or events • Adds operators that allow you to compose sequences together declaratively
  14. RxJS • Original Project: https://github.com/Reactive-Extensions/RxJS • Rewrite: https://github.com/ReactiveX/rxjs 
 (better

    performance, better modularity, better debuggable call stacks, while staying mostly backwards compatible, with some breaking changes that reduce the API surface)
  15. Promises vs Observables • Observables are lazy • Promises emit

    only one value • We can cancel an observable
  16. Types of Observables • Cold - When your observable creates

    the producer • Hot - When your observable closes over the producer
  17. Cold vs Hot • Cold - start running upon subscription

    • Hot - already producing values even before a subscription
  18. Observable Errors • When an error occurs the observable is

    closed so no data can pass trough. • The error is passed down the chain and our catch block is called.
  19. Subjects • The Subject class inherits both Observable and Observer,

    in the sense that it is both an observer and an observable. • Subjects have the same operators and methods available to them as Observables do. • Subjects keep a list of subscribed observers (they have state). • A subject can be used as an observer to subscribe to any observable.
  20. Subjects • Can be used for converting a Cold Observable

    into a Hot one • Subject can act as a proxy for a group of subscribers and a source • We can use subjects to implement a custom observable with caching, buffering and time shifting • We can use subjects to broadcast data to multiple subscribers