Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Functional and Reactive Jetpack for Javascript

2,768 views

Published on

While working on my WebRTC enabled side project, I noticed that many asynchronous processes which can be easily explained are really tough to implement using traditional imperative programming with object structuring. Intuitively we understand what it means. It means that we need to define new abstractions to effectively deal with asynchrony.
In this talk, I want to share with you new kinds of glue for structuring our programs, which I learned while taking bits of two other programming paradigms—functional and reactive—and using them in javascript.

Published in: Internet
  • Be the first to comment

Functional and Reactive Jetpack for Javascript

  1. 1. FUNCTIONAL & REACTIVE JETPACK for  Javascript.
  2. 2. Dmytrii Shchadei @metrofun
  3. 3. INTRO TODO Dmytrii Shchadei @metrofun
  4. 4. FUNCTIONAL & REACTIVE JETPACK for  Javascript.
  5. 5. WebRTC
  6. 6. ⚙ ⚙⚙ SDP  /  ICESDP  /  ICE DATACHANNEL CASE STUDY: SEND FILE P2P
  7. 7. Evénts
  8. 8. WHY IMPERATIVE OBJECT ORIENTED PROGRAMMING IS HARD? Combina5on  of  interfaces  creates  new  interfaces Implicit  dependencies  on  closure  variables  and  context Number  of  states  grows  as  a  product  of  its  building  blocks  ones
  9. 9. DECLARATIVE PROGRAMMING PART1 FUNCTIONAL PROGRAMMING
  10. 10. NO MUTABLE DATA Higher-­‐order  func5ons  (e.g.  map,  filter,  reduce  etc) No  states  due  to  no  assignments No  loops/switch  statements  (condi5ons  are  immutable) Recursion  instead  of  loops  (kudos  to  TCO,  haha  Java)
  11. 11. REFERENTIAL TRANSPARENCY No  Math.random   No  Date.now   … NO  SIDE  EFFECTS IDEMPOTENT Arguments  modifica5on   Excep5ons   …
  12. 12. UNIX PHILOSOPHY Make  each  program  do  one  thing  well.     Expect  the  output  of  every  program   to  become  the  input  to  another Douglas  McIlroy   The  inventor  of  Unix  pipes   “
  13. 13. DECLARATIVE PROGRAMMING PART2 REACTIVE PROGRAMMING
  14. 14. How to compose side effects? E R R RO A S Y CN
  15. 15. Generalize & Unify Value  +  Value  =  Collec5on Value  +  Async  =  Callbacks Value  +  Async  +  Errors  =  Promise Value  +  Value  +  Async  +  Errors  =  ?
  16. 16. Interface  Observable  <T>Interface  Iterator  <T> boolean  hasNext() T  next() Interface  ?  <T> onCompleted(  func5on  ) onNext(  func5on(  T  )  ) onError(  func5on(  Error  )  ) Let’s derive… Interface  Promise onError(  func5on(  Error  )  )   …
  17. 17. How does it fit the whole picture? SINGLE VALUE MULTIPLE VALUES PULL SYNC INTERACTIVE Value Collection PUSH ASYNC REACTIVE Promise Observable
  18. 18. What we received? Inversion  of  control  –  reac5veness  (less  memory)   Errors  are  first  class  ci5zens   Side  effects  are  encapsulated  so  we  can  use  FP
  19. 19. Observables + Higher Order Functions RxJS
  20. 20. CASE STUDY: TRANSFER PAGE
  21. 21. let  dataOb  =  /*  data  stream  of  ArrayBuffer  chunks  from  WebRTC  */…   let  metaOb    =  /*  single  meta  object  with  filename  &  size  from  Signal  Server  */… let  sentSizeOb  =  dataOb.scan(/*ini5al  value*/0,  (sum,  data)    =>  sum  +  data.byteLength); BEAUTY OF PROPER ABSTRACTION let  progressOb  =  sentSizeOb.combineLatest(metaOb,        (size,  meta)  =>  size  /  meta.size  *  100).subscribe(/*update  UI*/…); let  speedOb  =  sentSizeOb.skip(1).combineLatest(dataOb.take(1).map((size)  =>  {5me:  Date.now(),  size}),          (sum,  first)  =>  (sum  -­‐  first.size)  /  (Date.now()  -­‐  first.5me)).subscribe(/*update  UI*/…); let  5meleiOb  =  speedOb.combineLatest(sentSizeOb,metaOb,          (speed,  sizeSent,  meta)  =>  (meta.size  -­‐  sizeSent)  /  speed).subscribe(/*update  UI*/…);
  22. 22. Your  Mouse  is  a  Database Erik  Meijer     author  of  Reac=ve  Extensions “ (100,  30) (0,  0) (170,  -­‐10) (260,  50)
  23. 23. DECLARATIVE PROGRAMMING PART3 FRP?
  24. 24. FP + RP !== FRP
  25. 25. As  far  as  I  can  tell,  Rx  and  Bacon.js  lack  both  of  the  two   fundamental  proper5es  on  which  I  based  the  original  FRP.    I  think  an  accurate  descrip5on  of  Rx  and  Bacon.js  is   "composi5onal  event  systems  inspired  by  FRP". “ Conal  Ellio?   inventor  of  FRP
  26. 26. FRP (kinda SVG) RxJS, BaconJS (kinda BMP) Denota5on:  x(t)  =  t            y(t)  =  1            t  ∈  [0,  3] Sequence:  [{x:  0,  y:  1},            {x:1,  y:1},            {x:2,  y:1},            {x:3,  y:1}] xx yy
  27. 27. FRP IS DEAD LONG LIVE COMPOSITIONAL EVENT SYSTEMS
  28. 28. Dziękuję! Pytania? gmetrofun@gmail.com @metrofun TELE FILE htp://telefile.me

×