Skip to content

Commit

Permalink
Overload source to accept any combination of factors (#1)
Browse files Browse the repository at this point in the history
* Overload source to accept any combination of factors
  • Loading branch information
michaelwinch authored Jan 22, 2024
1 parent a09d809 commit 30a66ad
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/AsyncWriterResult/Library.fs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ module AsyncWriter =

let retn a = Writer.retn a |> Async.retn

let map f = f |> Writer.map |> Async.map


type ResultBuilder() =
member __.Return(x) = Result.retn x
Expand All @@ -242,14 +244,36 @@ type WriterResultBuilder() =
member __.ReturnFrom(m: Writer<'w, Result<'a, 'b>>) = m
member __.Bind(m, f) = WriterResult.bind f m
member __.Zero() = __.Return()
member __.Source(x: Writer<'w, Result<'a, 'b>>) = x

let writerResult = WriterResultBuilder()

[<AutoOpen>]
module WriterResultBuilderExtensions =
type WriterResultBuilder with
member __.Source(x: Result<'a, 'b>) = x |> Writer.retn
member __.Source(x: Writer<'w, 't>) = x |> Writer.map Ok


type AsyncWriterResultBuilder() =
member __.Return(x) = AsyncWriterResult.retn x
member __.ReturnFrom(m: Async<Writer<'w, Result<'a, 'b>>>) = m
member __.Bind(m, f) = AsyncWriterResult.bind f m
member __.Zero() = __.Return()
member __.Source(x: Async<Writer<'w, Result<'a, 'b>>>) = x

let asyncWriterResult = AsyncWriterResultBuilder()

[<AutoOpen>]
module AsyncWriterResultBuilderExtensions =
type AsyncWriterResultBuilder with
member __.Source(x: Result<'a, 'b>) = x |> AsyncWriter.retn
member __.Source(x: Writer<'w, 't>) = x |> Writer.map Ok |> Async.retn
member __.Source(x: Async<'t>) = x |> Async.map WriterResult.retn

[<AutoOpen>]
module AsyncWriterResultBuilderExtensionsHighPriority =
type AsyncWriterResultBuilder with
member __.Source(x: Writer<'w, Result<'a, 'b>>) = x |> Async.retn
member __.Source(x: Async<Result<'a, 'b>>) = x |> Async.map Writer.retn
member __.Source(x: Async<Writer<'w, 't>>) = x |> AsyncWriter.map Result.retn
17 changes: 17 additions & 0 deletions src/AsyncWriterResult/TaskWriterResult.fs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ module TaskWriter =

let retn a = Writer.retn a |> Task.retn

let map f = f |> Writer.map |> Task.map




Expand Down Expand Up @@ -110,5 +112,20 @@ module TaskWriterResult =
member __.ReturnFrom(m: Task<Writer<'w, Result<'a, 'b>>>) = m
member __.Bind(m, f) = bind f m
member __.Zero() = __.Return()
member __.Source(x: Task<Writer<'w, Result<'a, 'b>>>) = x

let taskWriterResult = TaskWriterResultBuilder()

[<AutoOpen>]
module TaskWriterResultBuilderExtensions =
type TaskWriterResultBuilder with
member __.Source(x: Result<'a, 'b>) = x |> TaskWriter.retn
member __.Source(x: Writer<'w, 't>) = x |> Writer.map Ok |> Task.retn
member __.Source(x: Task<'t>) = x |> Task.map WriterResult.retn

[<AutoOpen>]
module TaskWriterResultBuilderExtensionsHighPriority =
type TaskWriterResultBuilder with
member __.Source(x: Writer<'w, Result<'a, 'b>>) = x |> Task.retn
member __.Source(x: Task<Result<'a, 'b>>) = x |> Task.map Writer.retn
member __.Source(x: Task<Writer<'w, 't>>) = x |> TaskWriter.map Result.retn

0 comments on commit 30a66ad

Please sign in to comment.