9 Streams as values
In this chapter you will learn
- how to declaratively design complicated program flows
- how to use recursion and laziness to defer some decisions
- how to handle IO-based streams of data
- how to create and handle infinite stream of values
- how to isolate time-dependent functionalities
—Leslie Lamport
We have just learned how to read and write data using IO
. In this chapter we will still use IO
to represent our side-effectful programs as values, but our data will become much bigger—possibly infinite.
You have probably heard about data streams and streaming infrastructures. They have been very popular in recent years. The word streams, however, has become very overloaded and can mean different things to different people. Generally, streams were conceived to deal with incoming data that needs to be processed but doesn’t fit in the memory. However, we tend to use it in other cases, too—it has become a way to architect the control flow in many applications. Hence, it’s one of the possible software architecture choices and a very powerful one, too!
We will reuse a lot of knowledge about IO
and its laziness. We will use sequence
and orElse
in this chapter, too! They really are ubiquitous in FP.
Answers
Top-down vs. bottom-up design
Checking if rates are trending
We know we need a function that decides whether a given sequence of rates is trending or not. That means we need a function with the following signature: