More on Generation Size

Learn scaling and modifications of generation size.

If we want to apply some modifications to the generation size, we’re looking for the StreamData.scale/2 function. It takes a generator and a function that takes a size and returns a new size, and then it returns a new generator. The new generator uses the generation size returned by the passed function. (That was a mouthful.) Essentially, it modifies the generation size of a generator in a dynamic way, based on the original generation size.

Scaling in generation size

Scaling is especially useful in a couple of scenarios. The first one is when we want to increase or decrease the speed at which the generation size progresses. For example, it might be a good idea to reduce the growth of the generation size for a complex and deeply nested generator. To do that, we can scale the generation size with mathematic functions like square roots:

Executable

Press + to interact
generator = StreamData.list_of(StreamData.string(:ascii))
Press + to interact
scaled_generator =
StreamData.scale(generator, fn size ->
round(:math.sqrt(size))
end)
Press + to interact
scaled_generator |> Stream.drop(50) |> Enum.take(3)

Output

iex> generator = StreamData.list_of(StreamData.string(:ascii)) 
#StreamData<54.17825959/2 in StreamData.list_of/1>
iex> scaled_generator =
...> StreamData.scale(generator, fn size ->
...> round(:math.sqrt(size))
...> end) 
#StreamData<76.17825959/2 in
...