Skip to content
This repository has been archived by the owner on Nov 15, 2022. It is now read-only.

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
xkrt committed Aug 4, 2015
1 parent acb8740 commit 6112cca
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 61 deletions.
45 changes: 18 additions & 27 deletions src/InfluxDB.FSharp/AsyncChoiceBuilder.fs
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
namespace InfluxDB.FSharp

type internal AsyncChoiceBuilder () =
member inline __.Return value : Async<Choice<'T, 'Error>> =
async.Return (Choice1Of2 value)

member inline __.ReturnFrom (asyncChoice : Async<Choice<'T, 'Error>>) =
asyncChoice

member inline __.ReturnFrom (choice : Choice<'T, 'Error>) : Async<Choice<'T, 'Error>> =
async.Return choice

member inline this.Zero () : Async<Choice<unit, 'Error>> =
this.Return ()
member inline __.Return value : Async<Choice<'T, 'Error>> = async.Return (Choice1Of2 value)
member inline __.ReturnFrom (asyncChoice : Async<Choice<'T, 'Error>>) = asyncChoice
member inline __.ReturnFrom (choice : Choice<'T, 'Error>) : Async<Choice<'T, 'Error>> = async.Return choice
member inline this.Zero () : Async<Choice<unit, 'Error>> = this.Return ()

member inline __.Delay (generator : unit -> Async<Choice<'T, 'Error>>) : Async<Choice<'T, 'Error>> =
async.Delay generator
Expand All @@ -20,39 +13,32 @@ type internal AsyncChoiceBuilder () =
async {
let! r1' = r1
match r1' with
| Choice2Of2 error ->
return Choice2Of2 error
| Choice1Of2 () ->
return! r2
| Fail error -> return Fail error
| Ok () -> return! r2
}

member inline __.Bind (value : Async<Choice<'T, 'Error>>, binder : 'T -> Async<Choice<'U, 'Error>>) : Async<Choice<'U, 'Error>> =
async {
let! value' = value
match value' with
| Choice2Of2 error ->
return Choice2Of2 error
| Choice1Of2 x ->
return! binder x
| Fail error -> return Fail error
| Ok x -> return! binder x
}

member inline __.Bind (value : Choice<'T, 'Error>, binder : 'T -> Async<Choice<'U, 'Error>>) : Async<Choice<'U, 'Error>> =
async {
match value with
| Choice1Of2 x -> return! binder x
| Choice2Of2 error -> return Choice2Of2 error
| Ok x -> return! binder x
| Fail error -> return Fail error
}

member inline __.TryWith (computation : Async<Choice<'T, 'Error>>, catchHandler : exn -> Async<Choice<'T, 'Error>>)
: Async<Choice<'T, 'Error>> =
member inline __.TryWith (computation : Async<Choice<'T, 'Error>>, catchHandler : exn -> Async<Choice<'T, 'Error>>) : Async<Choice<'T, 'Error>> =
async.TryWith(computation, catchHandler)

member inline __.TryFinally (computation : Async<Choice<'T, 'Error>>, compensation : unit -> unit)
: Async<Choice<'T, 'Error>> =
member inline __.TryFinally (computation : Async<Choice<'T, 'Error>>, compensation : unit -> unit) : Async<Choice<'T, 'Error>> =
async.TryFinally (computation, compensation)

member inline __.Using (resource : ('T :> System.IDisposable), binder : _ -> Async<Choice<'U, 'Error>>)
: Async<Choice<'U, 'Error>> =
member inline __.Using (resource : ('T :> System.IDisposable), binder : _ -> Async<Choice<'U, 'Error>>) : Async<Choice<'U, 'Error>> =
async.Using (resource, binder)

member this.While (guard, body : Async<Choice<unit, 'Error>>) : Async<Choice<_,_>> =
Expand All @@ -67,3 +53,8 @@ type internal AsyncChoiceBuilder () =
enum.MoveNext,
this.Delay (fun () ->
body enum.Current)))

[<AutoOpen>]
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module internal AsyncChoiceBuilder =
let asyncChoice = AsyncChoiceBuilder()
47 changes: 19 additions & 28 deletions src/InfluxDB.FSharp/ChoiceBuilder.fs
Original file line number Diff line number Diff line change
@@ -1,54 +1,41 @@
namespace InfluxDB.FSharp

type internal ChoiceBuilder () =
static let zero = Choice1Of2 ()

member inline __.Return value : Choice<'T, 'Error> =
Choice1Of2 value

member inline __.ReturnFrom (m : Choice<'T, 'Error>) =
m

member inline __.Zero () : Choice<unit, 'Error> =
zero
static let zero : Choice<unit,'Error> = Choice1Of2 ()

member inline __.Return value : Choice<'T, 'Error> = Ok value
member inline __.ReturnFrom (m : Choice<'T, 'Error>) = m
member inline __.Zero () : Choice<unit, 'Error> = zero
member inline __.Delay (generator : unit -> Choice<'T, 'Error>) : Choice<'T, 'Error> =
generator ()

member inline __.Combine (r1, r2) : Choice<'T, 'Error> =
match r1 with
| Choice2Of2 error ->
Choice2Of2 error
| Choice1Of2 () ->
r2
| Fail error -> Fail error
| Ok () -> r2

member inline __.Bind (value, binder : 'T -> Choice<'U, 'Error>) : Choice<'U, 'Error> =
match value with
| Choice2Of2 error ->
Choice2Of2 error
| Choice1Of2 x ->
binder x
| Fail error -> Fail error
| Ok x -> binder x

member inline __.TryWith (body : 'T -> Choice<'U, 'Error>, handler) =
fun value ->
try body value
with ex ->
handler ex
with ex -> handler ex

member inline __.TryFinally (body : 'T -> Choice<'U, 'Error>, handler) =
fun value ->
try body value
finally
handler ()
finally handler ()

member this.Using (resource : ('T :> System.IDisposable), body : _ -> Choice<_,_>)
: Choice<'U, 'Error> =
member this.Using (resource : ('T :> System.IDisposable), body : _ -> Choice<_,_>) : Choice<'U, 'Error> =
try body resource
finally
if not <| System.Object.ReferenceEquals (null, box resource) then
if not <| isNull (box resource) then
resource.Dispose ()

member this.While (guard, body : Choice<unit, 'Error>) : Choice<_,_> =
member this.While (guard, body : Choice<unit, 'Error>) : Choice<_, 'Error> =
if guard () then
this.Bind (body, (fun () -> this.While (guard, body)))
else
Expand All @@ -58,5 +45,9 @@ type internal ChoiceBuilder () =
this.Using (sequence.GetEnumerator (), fun enum ->
this.While (
enum.MoveNext,
this.Delay (fun () ->
body enum.Current)))
this.Delay (fun () -> body enum.Current)))

[<AutoOpen>]
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module internal ChoiceBuilder =
let choice = ChoiceBuilder()
2 changes: 1 addition & 1 deletion src/InfluxDB.FSharp/InfluxDB.FSharp.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@
<Content Include="packages.config" />
<Content Include="app.config" />
<Compile Include="InternalsVisibleTo.fs" />
<Compile Include="Prelude.fs" />
<Compile Include="AsyncChoiceBuilder.fs" />
<Compile Include="ChoiceBuilder.fs" />
<Compile Include="Prelude.fs" />
<Compile Include="Types.fs" />
<Compile Include="Point.fsi" />
<Compile Include="Point.fs" />
Expand Down
3 changes: 2 additions & 1 deletion src/InfluxDB.FSharp/InternalsVisibleTo.fs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace HttpFs

open System.Runtime.CompilerServices

[<assembly: InternalsVisibleTo "InfluxDB.FSharp.UnitTests">]
[<assembly: InternalsVisibleTo "InfluxDB.FSharp.IntegrationTests">]
()
()
6 changes: 2 additions & 4 deletions src/InfluxDB.FSharp/Prelude.fs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ module internal Prelude =

let inline isNull arg = Object.ReferenceEquals (null, arg)

let choice = ChoiceBuilder ()
let asyncChoice = AsyncChoiceBuilder ()

let invCulture = System.Globalization.CultureInfo.InvariantCulture


Expand Down Expand Up @@ -131,9 +128,10 @@ module internal Map =
|> String.concat "; "
sprintf "[%s]" pairs


module internal Option =
let inline ofNull value =
if Object.ReferenceEquals(value, null) then None else Some value
if isNull value then None else Some value


module internal Async =
Expand Down

0 comments on commit 6112cca

Please sign in to comment.