An other data structure today ! After seeing
Set. Let’s talk about
Stream, now called
LazyList in Scala 2.13+.
This is a bit of a longer exercise because there are several ways to create a
LazyList and several ways to use it. Remember, this is your journey. If you only have the time to complete the beginning, you can move on or come back to it later.
In the first section, you saw how to create a
LazyList recursively using the symbol
#::. You also learned that you can
take as many elements as you want, but since Scala values immutability, doing it several times in a row will always returns the same elements. Make sure to read the log statements carefully.
In the second section, we are using
LazyList.from to use the build-in tools that allow you to start a sequential
stream starting at
n and increasing one by one. You also see that
map can be used to modify the outcome of the
LazyList. And finally,
take can only reduce the size of the stream.
In the third and final section, we are implementing
factorial, once recursively and then using a
LazyList. Have you notice the use of the
foldLeft from previous SKB !?