Chapter 7. Switch

 

This chapter covers

  • The last two core primitives: sample and switch
  • How to write video games in FRP

FRP code describes data flow in a directed graph structure, and until now those graphs have been static. Cells for storing state are constructed using the hold primitive, and the total number of them hasn’t been able to change as the program runs. switch allows you to change this structure dynamically. sample allows you to sample the value of a cell.

7.1. The sample primitive: getting a cell’s value

What you’re about to read may surprise you, coming from people as puritanical about compositionality as we are. Remember that in chapter 1 we talked about how people new to FRP will ask us, “How do you get the value?” We waved our hands around and gave some evasive answer. We wanted you to be thinking the right way before we told you that we can just say (for example)

Cell<Scene> scene = ...;
Scene sc = scene.sample();

and fetch the current value of a cell directly. Oh, the hypocrisy!

Now that you’ve calmed down and picked the book back up, a common case where sample is useful is in the paint() method of an animation. A listen callback might trigger a repaint(). For those who don’t know about Java, this doesn’t paint directly but schedules a paint() method to be executed “later.”

7.2. switch

7.3. switch use case #1: zombies

7.4. Transforming the game character with switch

7.5. switch use case #2: creation and destruction of game characters

7.6. The efficiency of big merges

7.7. Game characters and efficiency in RxJS

7.8. Switch use case #3: removing invalid states

7.9. Switch use case #4: switching between screens

7.10. Summary

sitemap