Sequence
differs from List
in another way. List
is a fixed set of items.
Sequence
, on the other hand, can be generated on the fly, such as via
the generateSequence()
global function.
generateSequence()
takes a lambda expression, and it emits the result
of evaluating that lambda expression, over and over as downstream consumers
pull items from the resulting Sequence
. Once the lambda expression returns
null
, the Sequence
ends, and no more items are emitted.
In this case, percentileDice()
will return a random number from 1 to 100.
Our Sequence
from generateSequence()
will return a stream of random
numbers, until the next number exceeds 95, at which point the Sequence
ends.
Sequence
has a toList()
function that collects all the emitted items and
puts them in a List
, which is useful here just for printing the results.