From dc321d25f2780dbe436ccd013dcf986491790950 Mon Sep 17 00:00:00 2001 From: Tyson Williams Date: Tue, 7 Dec 2021 19:49:01 -0600 Subject: [PATCH] Add some functional programming primitives related to Lazy --- src/Hedgehog/GenLazy.fs | 15 +++++++++++++++ src/Hedgehog/GenLazyTuple.fs | 10 ++++++++++ src/Hedgehog/Hedgehog.fsproj | 5 ++++- src/Hedgehog/Lazy.fs | 24 ++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 src/Hedgehog/GenLazy.fs create mode 100644 src/Hedgehog/GenLazyTuple.fs create mode 100644 src/Hedgehog/Lazy.fs diff --git a/src/Hedgehog/GenLazy.fs b/src/Hedgehog/GenLazy.fs new file mode 100644 index 00000000..95120915 --- /dev/null +++ b/src/Hedgehog/GenLazy.fs @@ -0,0 +1,15 @@ +// Workaround for a Fable issue: https://github.com/fable-compiler/Fable/issues/2069 +#if FABLE_COMPILER +module Hedgehog.GenLazy +#else +[] +module internal Hedgehog.GenLazy +#endif + +let constant a = a |> Lazy.constant |> Gen.constant + +let map f = f |> Lazy.map |> Gen.map + +let join glgla = glgla |> Gen.bind Lazy.value + +let bind f gla = gla |> map f |> join diff --git a/src/Hedgehog/GenLazyTuple.fs b/src/Hedgehog/GenLazyTuple.fs new file mode 100644 index 00000000..2cff662c --- /dev/null +++ b/src/Hedgehog/GenLazyTuple.fs @@ -0,0 +1,10 @@ +// Workaround for a Fable issue: https://github.com/fable-compiler/Fable/issues/2069 +#if FABLE_COMPILER +module Hedgehog.GenLazyTuple +#else +[] +module internal Hedgehog.GenLazyTuple +#endif + +let mapFst f = f |> Tuple.mapFst |> GenLazy.map +let mapSnd f = f |> Tuple.mapSnd |> GenLazy.map diff --git a/src/Hedgehog/Hedgehog.fsproj b/src/Hedgehog/Hedgehog.fsproj index 202b7160..9f76e133 100644 --- a/src/Hedgehog/Hedgehog.fsproj +++ b/src/Hedgehog/Hedgehog.fsproj @@ -1,4 +1,4 @@ - + netstandard1.6;netstandard2.0;net45 @@ -32,6 +32,7 @@ https://github.com/hedgehogqa/fsharp-hedgehog/blob/master/doc/index.md <_Parameter1>Hedgehog.Linq.Tests + @@ -45,6 +46,8 @@ https://github.com/hedgehogqa/fsharp-hedgehog/blob/master/doc/index.md + + diff --git a/src/Hedgehog/Lazy.fs b/src/Hedgehog/Lazy.fs new file mode 100644 index 00000000..b72a7eb0 --- /dev/null +++ b/src/Hedgehog/Lazy.fs @@ -0,0 +1,24 @@ +// Workaround for a Fable issue: https://github.com/fable-compiler/Fable/issues/2069 +#if FABLE_COMPILER +module Hedgehog.Lazy +#else +[] +module internal Hedgehog.Lazy +#endif + +let func (f: unit -> 'a) = Lazy<'a>(valueFactory = fun () -> f ()) + +let constant (a: 'a) = Lazy<'a>(valueFactory = fun () -> a) + +let value (ma: Lazy<'a>) = ma.Value + +let map (f: 'a -> 'b) (ma: Lazy<'a>) : Lazy<'b> = + (fun () -> ma.Value |> f) + |> func + +let join (mma: Lazy>) = + (fun () -> mma.Value.Value) + |> func + +let bind (f: 'a -> Lazy<'b>) = + f |> map >> join