Skip to content

Commit

Permalink
Use native on netstandard2.1 and TaskOptionCE using resumable code
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAngryByrd committed Mar 29, 2022
1 parent 94910f9 commit 1817967
Show file tree
Hide file tree
Showing 11 changed files with 612 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
<!-- <TargetFrameworks>netstandard2.1</TargetFrameworks> -->
<DebugType>portable</DebugType>
<LangVersion>preview</LangVersion>
<NoWarn>FS3511;FS3513</NoWarn>
</PropertyGroup>


Expand Down
2 changes: 2 additions & 0 deletions src/FsToolkit.ErrorHandling.TaskResult/List.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ namespace FsToolkit.ErrorHandling

open System.Threading.Tasks
open FsToolkit.ErrorHandling
#if NETSTANDARD2_0
open FSharp.Control.Tasks.Affine
#endif

[<RequireQualifiedAccess>]
module List =
Expand Down
2 changes: 2 additions & 0 deletions src/FsToolkit.ErrorHandling.TaskResult/Result.fs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
namespace FsToolkit.ErrorHandling

open System.Threading.Tasks
#if NETSTANDARD2_0
open FSharp.Control.Tasks.Affine
#endif

module Result =

Expand Down
3 changes: 3 additions & 0 deletions src/FsToolkit.ErrorHandling.TaskResult/Task.fs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
namespace FsToolkit.ErrorHandling

open System.Threading.Tasks

#if NETSTANDARD2_0
open FSharp.Control.Tasks.Affine
#endif

[<RequireQualifiedAccess>]
module Task =
Expand Down
2 changes: 2 additions & 0 deletions src/FsToolkit.ErrorHandling.TaskResult/TaskOption.fs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
namespace FsToolkit.ErrorHandling

open System.Threading.Tasks
#if NETSTANDARD2_0
open FSharp.Control.Tasks.Affine
#endif

[<RequireQualifiedAccess>]
module TaskOption =
Expand Down
443 changes: 441 additions & 2 deletions src/FsToolkit.ErrorHandling.TaskResult/TaskOptionCE.fs

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/FsToolkit.ErrorHandling.TaskResult/TaskResult.fs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
namespace FsToolkit.ErrorHandling

open System.Threading.Tasks
#if NETSTANDARD2_0
open FSharp.Control.Tasks.Affine
#endif

[<RequireQualifiedAccess>]
module TaskResult =
Expand Down
3 changes: 1 addition & 2 deletions src/FsToolkit.ErrorHandling.TaskResult/TaskResultOption.fs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace FsToolkit.ErrorHandling

open FSharp.Control.Tasks.Affine

[<RequireQualifiedAccess>]
module TaskResultOption =
Expand All @@ -20,7 +19,7 @@ module TaskResultOption =
let inline map3 ([<InlineIfLambda>] f) xTRO yTRO zTRO =
TaskResult.map3 (Option.map3 f) xTRO yTRO zTRO

let inline retn value = task { return Ok(Some value) }
let inline retn value = TaskResult.retn (Ok value)

let inline apply fTRO xTRO = map2 (fun f x -> f x) fTRO xTRO

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net5.0;net6.0</TargetFrameworks>
<!-- <TargetFrameworks>net6.0</TargetFrameworks> -->
<LangVersion>preview</LangVersion>
</PropertyGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net5.0'">
Expand Down
136 changes: 135 additions & 1 deletion tests/FsToolkit.ErrorHandling.TaskResult.Tests/Main.fs
Original file line number Diff line number Diff line change
@@ -1,7 +1,141 @@
module FsToolkit.ErrorHandling.TaskResult.Tests

open Expecto
open FSharp.Control.Tasks
open System.Threading.Tasks
open System
open FsToolkit.ErrorHandling

type Person = {
Name : string
DateOfBirth : DateTimeOffset
}

let getSomethingFromDatabase = task {
do! Task.Delay(2000)
return [
{ Name = "Jimmy Byrd" ; DateOfBirth = DateTimeOffset.UtcNow}
{ Name = "Todd Ropog" ; DateOfBirth = DateTimeOffset.UtcNow}
]
}



let writeDataToAFile filename = task {
let! result = getSomethingFromDatabase
do! System.IO.File.WriteAllTextAsync(filename, result |> string)
}


let getUserInput () =
let fileWrittenTo = writeDataToAFile "FooFiles.txt"
printfn "I wrote to this file %A" fileWrittenTo
System.Console.WriteLine("wriring values")


let getPeople () =
[
{ Name = "Jimmy Byrd" ; DateOfBirth = DateTimeOffset.Parse("03/18/1988")}
{ Name = "Todd Ropog" ; DateOfBirth = DateTimeOffset.UtcNow}
]

let getPersonByName nameToSearchBy =
getPeople ()
|> List.tryFind(fun person -> person.Name = nameToSearchBy)

let getPersonByNameResult nameToSearchBy =
let person =
getPeople ()
|> List.tryFind(fun person -> person.Name = nameToSearchBy)
// match person with
// | Some p -> Ok p
// | None -> Error (sprintf "No person found by the name of %s" nameToSearchBy)
person
|> Result.requireSome (sprintf "No person found by the name of %s" nameToSearchBy)


let getDiscount (dateOfBirthToGet : DateTimeOffset) =
if dateOfBirthToGet.Date = DateTimeOffset.UtcNow.Date then
Some (5.0)
else
None


let getDiscountResult (dateOfBirthToGet : DateTimeOffset) =
let today = DateTimeOffset.UtcNow.Date
let discount =
if dateOfBirthToGet.Date = today then
Some (5.0)
else
None
discount
|> Result.requireSome (sprintf "No discount for the DoB of %A" dateOfBirthToGet.Date )


let getFinalPrice (shoppingCartItems : list<string * float>) discount =
let summedTotal =
shoppingCartItems
|> List.sumBy(fun (itemName, itemPrice) -> itemPrice)
summedTotal * (100.0 - discount)

let example () =

let items = [
("Beer", 8.0)
("Cheese", 2.0)
]
// match getPersonByName "Todd Ropog" with
// | Some person ->
// match getDiscount person.DateOfBirth with
// | Some discount ->
// let finalPrice = getFinalPrice items discount
// printfn "%A" finalPrice
// | None -> ()
// | None -> ()

// match getPersonByNameResult "David" with
// | Ok person ->
// match getDiscountResult person.DateOfBirth with
// | Ok discount ->
// let finalPrice = getFinalPrice items discount
// printfn "%A" finalPrice
// | Error e -> printfn "%s" e
// | Error e -> printfn "%s" e


// getPersonByName "Todd Ropog"
// |> Option.bind(fun person -> getDiscount person.DateOfBirth)
// |> Option.map (fun discount -> getFinalPrice items discount)
// |> Option.iter(fun finalPrice -> printfn "%A" finalPrice)

// let result =
// getPersonByNameResult "Jimmy Byrd"
// |> Result.bind(fun person -> getDiscountResult person.DateOfBirth)
// |> Result.map (fun discount -> getFinalPrice items discount)
// match result with
// | Ok finalPrice -> printfn "%A" finalPrice
// | Error e -> printfn "%s" e


// let thirdExample = option {
// let! person = getPersonByName "David"
// let! discount = getDiscount person.DateOfBirth
// let finalPrice = getFinalPrice items discount
// printfn "%A" finalPrice
// }

let thirdExampleResult = result {
let! person = getPersonByNameResult "Jimmy Byrd"
let! discount = getDiscountResult person.DateOfBirth
let finalPrice = getFinalPrice items discount
return finalPrice
}

match thirdExampleResult with
| Ok finalPrice -> printfn "%A" finalPrice
| Error e -> printfn "%s" e


[<EntryPoint>]
let main argv =
Tests.runTestsInAssembly defaultConfig argv
Tests.runTestsInAssembly defaultConfig argv
22 changes: 20 additions & 2 deletions tests/FsToolkit.ErrorHandling.TaskResult.Tests/TaskOptionCE.fs
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,26 @@ let ceTests =
}

Expect.equal actual (Some data) "Should be ok"
}
testCaseTask "Task.Yield"
<| fun () ->
task {

let! actual =
taskOption {
do! Task.Yield()
}

Expect.equal actual (Some ()) "Should be ok"
} ]

let specialCaseTask returnValue =
#if NETSTANDARD2_0
Unsafe.uply { return returnValue }
#else
Task.FromResult returnValue
#endif

[<Tests>]
let ceTestsApplicative =
testList
Expand All @@ -342,7 +360,7 @@ let ceTestsApplicative =
taskOption {
let! a = Some 3
let! b = Some 1 |> Async.singleton
let! c = Unsafe.uply { return Some 3 }
let! c = specialCaseTask (Some 3)
let! d = ValueTask.FromResult(Some 5)
return a + b - c - d
}
Expand All @@ -356,7 +374,7 @@ let ceTestsApplicative =
taskOption {
let! a = Some 3
and! b = Some 1 |> Async.singleton
and! c = Unsafe.uply { return None }
and! c = specialCaseTask(None)
and! d = ValueTask.FromResult(Some 5)
return a + b - c - d
}
Expand Down

0 comments on commit 1817967

Please sign in to comment.