From 2d666a92353b6c11fc3766c4c40692793ad4ed2b Mon Sep 17 00:00:00 2001 From: Vadim Tsvetkov Date: Thu, 16 Jun 2022 02:07:35 +0300 Subject: [PATCH 1/2] Add TypeClass.Property.Generator implementation for streams --- lib/type_class/property/generator.ex | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib/type_class/property/generator.ex b/lib/type_class/property/generator.ex index 703445a..6006d54 100644 --- a/lib/type_class/property/generator.ex +++ b/lib/type_class/property/generator.ex @@ -116,6 +116,24 @@ defimpl TypeClass.Property.Generator, for: List do end end +defimpl TypeClass.Property.Generator, for: Stream do + @moduledoc false + + def generate(_) do + 1 + |> Stream.unfold(fn acc -> + next = + [0, 0, 0, 0, 0.0, 0.0, 0.0, 0.0, "", "", "", "", "", "", {}, [], %{}] + |> Enum.random() + |> TypeClass.Property.Generator.generate() + + {acc, next} + end) + |> Stream.drop(1) + |> Stream.take(:rand.uniform(4)) + end +end + defimpl TypeClass.Property.Generator, for: Tuple do @moduledoc false From 45c0da854ee87ef8b9953ed24705db0e75126d39 Mon Sep 17 00:00:00 2001 From: Vadim Tsvetkov Date: Thu, 16 Jun 2022 12:30:41 +0300 Subject: [PATCH 2/2] Make Stream generator return a cycled stream Now Enum.to_list of generated Stream returns always the same list --- lib/type_class/property/generator.ex | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/lib/type_class/property/generator.ex b/lib/type_class/property/generator.ex index 6006d54..ade29bb 100644 --- a/lib/type_class/property/generator.ex +++ b/lib/type_class/property/generator.ex @@ -120,17 +120,11 @@ defimpl TypeClass.Property.Generator, for: Stream do @moduledoc false def generate(_) do - 1 - |> Stream.unfold(fn acc -> - next = - [0, 0, 0, 0, 0.0, 0.0, 0.0, 0.0, "", "", "", "", "", "", {}, [], %{}] - |> Enum.random() - |> TypeClass.Property.Generator.generate() + l = TypeClass.Property.Generator.generate([]) - {acc, next} - end) - |> Stream.drop(1) - |> Stream.take(:rand.uniform(4)) + l + |> Stream.cycle() + |> Stream.take(length(l)) end end