functional reactive programming
play

Functional Reactive Programming Bob Reynders PLaNES, 4 Februari - PowerPoint PPT Presentation

Functional Reactive Programming Bob Reynders PLaNES, 4 Februari 2015 1/19 Functional Reactive Programming Event discrete values Mouse.click: Event[Click] List[(Time, A)] Behavior time-varying values Mouse.position:


  1. Functional Reactive Programming Bob Reynders PLaNES, 4 Februari 2015 1/19

  2. Functional Reactive Programming ◮ Event discrete values ◮ Mouse.click: Event[Click] ◮ ≈ List[(Time, A)] ◮ Behavior time-varying values ◮ Mouse.position: Behavior[Coordinate] ◮ ≈ Time => A 2/19

  3. FRP API (subset) Event[T].map[A](f: T => A): Event[A] Beh[T].map[A](f: T => A): Beh[A] // Filter Event[T].keepIf(f: T => Boolean): Event[T] // Lift Beh[T].combine[A, B](b: Beh[A])(f: (T, A) => B): Beh[B] Event[T].merge(e: Event[T]): Event[T] // Sampling Beh[T].sampledBy(e: Event[_]): Event[T] // State Event[T].fold[A](initial: A)(fun: (A, T) => A): Beh[A] 3/19

  4. GUI Example // input val plusE: Event[Int] = plusButton.click.map(_ => 1) val minE: Event[Int] = minButton.click.map(_ => -1) val merged: Event[Int] = plusE.merge(minE) // state val counter: Beh[Int] = merged.fold(0) { (acc, e) => acc + e } // view def template(data: Int): GUI = ... val main: Beh[GUI] = counter.map(template) 4/19

  5. Static Dependency Graph +1 -1 merge (0, -1, +1, +1, +1, ...) fold 0 counter World (0, -1, 0, 1, 2, …) main (span(0), span(-1), ...) Figure 1: Static graph ◮ First order FRP ◮ Static guarantees 5/19

  6. Dynamic Dependency Graph New Figure 2: Switch ◮ Higher order FRP ◮ Event[Behavior[T]].switch: Behavior[T] 6/19

  7. Glitch-freedom x x2 always True val x: Beh[Int] val x2 = x.map(_ * 2) val alwaysTrue = x.combine(x2)(_ <= _) 7/19

  8. Multi-tier Functional Reactive Programming 8/19

  9. Multi-tier Functional Reactive Programming // One language ServerEvent[T].map[A](f: T => A): ServerEvent[A] ClientEvent[T].map[A](f: Rep[T] => Rep[A]): ClientEvent[A] ... ◮ Embed Javascript as a DSL in Scala ◮ Rep[String] = (Client) Javascript String ◮ String = (Server) Scala String 9/19

  10. Multi-tier API Extension // Replication between tiers ClientEvent[T].toServerAnon: ServerEvent[T] ServerEvent[T].toAllClients: ClientEvent[T] // Server -> Client ServerBeh[T].toAllClients: ClientBeh[T] 10/19

  11. Chat Application // prepare input val nameB: ClientBeh[String] = nameInput.text val msgB: ClientBeh[String] = msgInput.text val clickE: ClientEvent[MouseData] = sendBtn.click // model entries (across tiers!) val entryB = nameB.combine(msgB)(_ + ": " + _) val submitE = entryB.sampledBy(clickE).toServerAnon // model server application state val chatB: ServerBeh[List[String]] = submitE.fold(List.empty[String]) { (acc, entry) => entry :: acc } // view (template on the client) def template(view: Rep[List[String]]): Rep[GUI] = ... val main: ClientBeh[GUI] = chatB.toAllClients.map(template) 11/19

  12. Chat Application 12/19

  13. Multi-tier API Extension // Client-aware replication // - Track sources ClientEvent[T].toServer: ServerEvent[(Client, T)] ServerEvent[Client => Option[T]].toClient: ClientEvent[T] ServerBeh[Client => T].toClient: ClientBeh[T] 13/19

  14. Multi-tier API Extension // Generalise to categories of programs Event[C1, T].anonTo[C2]: Event[C2, T] // - Track sources Event[C1, Id[C2] => Option[T]].to[C2]: Event[C2, (Id[C1], T)] 14/19

  15. Glitch-freedom val src: ClientEvent[Int] val x2 = src.map(_ * 2).toServer val alwaysTrue = src.hold(0).combine(x2.hold(0))(_ < _) 15/19

  16. Glitch-freedom: naive ◮ 1 propagation on the client → 2 on the server Multi-tier FRP Server Client toServer Src’ Src _ < _ x2’ x2 16/19

  17. Glitch-freedom: glitch-free tiers ◮ 1 propagation on the client → 1 on the server Multi-tier FRP Server Client toServer Src’ Src _ < _ x2’ x2 17/19

  18. Glitch-freedom: total Figure 3: Distributed REScala: An Update Algorithm for Distributed Reactive Programming 18/19

  19. Network Overhead val serverB: Behavior[List[Behavior[Int]]] // changes of Int val serverB: Behavior[List[Int]] // changes of List[Int] val incServerB: IncBehavior[List[Int], Delta] // changes of Delta 19/19

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend