Skip to content

Instantly share code, notes, and snippets.

@cowlike
Last active September 1, 2019 15:05
Show Gist options
  • Select an option

  • Save cowlike/53144912970de8f01a7906b076f34b5b to your computer and use it in GitHub Desktop.

Select an option

Save cowlike/53144912970de8f01a7906b076f34b5b to your computer and use it in GitHub Desktop.

Revisions

  1. cowlike revised this gist Sep 1, 2019. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions FSharpPlusTransformer.fs
    Original file line number Diff line number Diff line change
    @@ -38,4 +38,14 @@ OptionT.run run3 |> printfn "%A"
    run3 >>= (fun p -> lift (return (p * 10)))
    |> OptionT.run
    |> printfn "%A"
    |> ignore

    let run4 = monad {
    let realInt: Option<Result<int,string>> = Some (result 1)
    let! x = ResultT realInt
    return x * 10 }

    run4 >>= (fun p -> lift (Some (p * 10)))
    |> ResultT.run
    |> printfn "%A"
    |> ignore
  2. cowlike revised this gist Aug 31, 2019. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions FSharpPlusTransformer.fs
    Original file line number Diff line number Diff line change
    @@ -29,13 +29,13 @@ let run2 = monad {
    }

    let run3 = monad {
    let realInt: Result<int option,string> = Ok (Some 1)
    let realInt: Result<int option,string> = return (Some 1)
    let! x = OptionT realInt
    return x * 10 }

    OptionT.run run3 |> printfn "%A"

    run3 >>= (fun p -> lift (Ok (p * 10)))
    run3 >>= (fun p -> lift (return (p * 10)))
    |> OptionT.run
    |> printfn "%A"
    |> ignore
  3. cowlike revised this gist Aug 31, 2019. 1 changed file with 14 additions and 2 deletions.
    16 changes: 14 additions & 2 deletions FSharpPlusTransformer.fs
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@ let doNextAsyncThing (x:System.DateTime) = async {
    }

    let f x = 2 * x
    let run' = monad {
    let run1 = monad {
    let! x = lift doAsyncThing
    let! y = OptionT (doNextAsyncThing x)
    return f y
    @@ -22,8 +22,20 @@ let doNextResultThing (rand: System.Random): Result<int option,string> = monad {
    return (if r < 5 then Some r else None)
    }

    let run2' = monad {
    let run2 = monad {
    let! x = lift doResultThing
    let! y = OptionT (doNextResultThing(x))
    return f y
    }

    let run3 = monad {
    let realInt: Result<int option,string> = Ok (Some 1)
    let! x = OptionT realInt
    return x * 10 }

    OptionT.run run3 |> printfn "%A"

    run3 >>= (fun p -> lift (Ok (p * 10)))
    |> OptionT.run
    |> printfn "%A"
    |> ignore
  4. cowlike revised this gist Aug 31, 2019. No changes.
  5. cowlike revised this gist Aug 31, 2019. No changes.
  6. cowlike revised this gist Aug 31, 2019. No changes.
  7. cowlike created this gist Aug 31, 2019.
    29 changes: 29 additions & 0 deletions FSharpPlusTransformer.fs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    #r "/Users/cowlike/.nuget/packages/fsharpplus/1.1.0-ci00271/lib/netstandard2.0/FSharpPlus.dll"

    open FSharpPlus
    open FSharpPlus.Data

    let doAsyncThing = async {return System.DateTime.Now}
    let doNextAsyncThing (x:System.DateTime) = async {
    let m = x.Millisecond
    return (if m < 500 then Some m else None)
    }

    let f x = 2 * x
    let run' = monad {
    let! x = lift doAsyncThing
    let! y = OptionT (doNextAsyncThing x)
    return f y
    }

    let doResultThing: Result<System.Random,string> = Ok <| System.Random()
    let doNextResultThing (rand: System.Random): Result<int option,string> = monad {
    let r = rand.Next(10)
    return (if r < 5 then Some r else None)
    }

    let run2' = monad {
    let! x = lift doResultThing
    let! y = OptionT (doNextResultThing(x))
    return f y
    }