Last active
March 24, 2020 09:35
-
-
Save serjKim/3771e0a40854aad8594f0c7bdf7acd90 to your computer and use it in GitHub Desktop.
F#: Reduce function dependencies
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
type Entity = { ArrivalDate: DateTime } | |
type IDb = | |
abstract member Conn: string | |
type Workflow<'Result> = Workflow of (IDb -> 'Result) | |
let db = { new IDb with member x.Conn = "asd" } | |
let run idb (Workflow fn) = fn idb | |
let bind f fn = | |
Workflow(fun d -> | |
let r = run d fn | |
run d (f r)) | |
type WorkflowBuilder() = | |
member _.Bind(x, f) = bind f x | |
member _.Return (x) = Workflow(fun _ -> x) | |
member _.ReturnFrom (workflow: Workflow<'Result>) = workflow | |
let workflow = new WorkflowBuilder() | |
let createEntity arrivalDate = | |
let create db = | |
{ ArrivalDate = arrivalDate } | |
Workflow create | |
let result = workflow { | |
let! p = createEntity (System.DateTime.Now) | |
return p | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment