Skip to content

Commit

Permalink
Add some functional programming primitives related to Lazy
Browse files Browse the repository at this point in the history
  • Loading branch information
TysonMN committed Dec 8, 2021
1 parent 0b6a159 commit dc321d2
Showing 4 changed files with 53 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/Hedgehog/GenLazy.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Workaround for a Fable issue: https://github.com/fable-compiler/Fable/issues/2069
#if FABLE_COMPILER
module Hedgehog.GenLazy
#else
[<RequireQualifiedAccess>]
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
10 changes: 10 additions & 0 deletions src/Hedgehog/GenLazyTuple.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Workaround for a Fable issue: https://github.com/fable-compiler/Fable/issues/2069
#if FABLE_COMPILER
module Hedgehog.GenLazyTuple
#else
[<RequireQualifiedAccess>]
module internal Hedgehog.GenLazyTuple
#endif

let mapFst f = f |> Tuple.mapFst |> GenLazy.map
let mapSnd f = f |> Tuple.mapSnd |> GenLazy.map
5 changes: 4 additions & 1 deletion src/Hedgehog/Hedgehog.fsproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard1.6;netstandard2.0;net45</TargetFrameworks>
@@ -32,6 +32,7 @@ https://github.com/hedgehogqa/fsharp-hedgehog/blob/master/doc/index.md
<_Parameter1>Hedgehog.Linq.Tests</_Parameter1>
</AssemblyAttribute>
<Compile Include="AutoOpen.fs" />
<Compile Include="Lazy.fs" />
<Compile Include="Numeric.fs" />
<Compile Include="Seed.fs" />
<Compile Include="Seq.fs" />
@@ -45,6 +46,8 @@ https://github.com/hedgehogqa/fsharp-hedgehog/blob/master/doc/index.md
<Compile Include="Journal.fs" />
<Compile Include="Tuple.fs" />
<Compile Include="GenTuple.fs" />
<Compile Include="GenLazy.fs" />
<Compile Include="GenLazyTuple.fs" />
<Compile Include="Outcome.fs" />
<Compile Include="Report.fs" />
<Compile Include="PropertyArgs.fs" />
24 changes: 24 additions & 0 deletions src/Hedgehog/Lazy.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Workaround for a Fable issue: https://github.com/fable-compiler/Fable/issues/2069
#if FABLE_COMPILER
module Hedgehog.Lazy
#else
[<RequireQualifiedAccess>]
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<Lazy<'a>>) =
(fun () -> mma.Value.Value)
|> func

let bind (f: 'a -> Lazy<'b>) =
f |> map >> join

0 comments on commit dc321d2

Please sign in to comment.