-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use native on netstandard2.1 and TaskOptionCE using resumable code
- Loading branch information
1 parent
94910f9
commit 1817967
Showing
11 changed files
with
612 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
.../FsToolkit.ErrorHandling.TaskResult.Tests/FsToolkit.ErrorHandling.TaskResult.Tests.fsproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters