Generating a Java Stream of Fibonacci numbers
A Java stream represents potentially an infinite sequence of data. This is a simple post that will go into the mechanics involved in generating a simple stream of Fibonacci numbers.
The simplest way to get this stream of data is to use the generate method of Stream.
As you can imagine to generate a specific Fibonacci number in this sequence, the previous two numbers are required, which means the state of the previous two numbers need to be maintained somewhere. The two solutions that I will be describing here both maintain this state, however they do it differently.
Mutable State
In the first approach, I am just going to maintain state this way:
with index keeping track of the index of the current fibonacci number in the sequence and prev capturing the most recent two in the sequence. So the next in the series is generated by mutating the index and the changing the array holding the recent values. So given this state, how do we generate the stream, using code which looks like this:
This is using a closure to capture the fibState and mutating it repeatedly as the stream of numbers is generated. The approach works well, though the thought of mutating one value probably should induce a level of dread — is it thread safe (probably not), will it work for parallel streams(likely not), but should suffice for cases where the access is strictly sequential. A far better approach is to get a version of state that is immutable.
Immutable State
Instead of mutating the state, this version returns the next immutable state. Here is what a version using “iterate” function of Stream, looks like:
this function takes two parameters — the initial state and something which can generate the next state. Also to return numbers from it, I am mapping this “state” type to a number in the “map” operation.
Conclusion
This is generally instructive on how to generate a stream using Fibonacci sequence as an example. The approach will be fairly similar for any stream that you may need to generate. My personal preference is for the Immutable state variation as that delegates most of the mechanism of generating the stream to the excellent “iterate” helper function of Stream.