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.
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)
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.”