Skip to content

Instantly share code, notes, and snippets.

@serjKim
Last active March 24, 2020 09:35
Show Gist options
  • Save serjKim/3771e0a40854aad8594f0c7bdf7acd90 to your computer and use it in GitHub Desktop.
Save serjKim/3771e0a40854aad8594f0c7bdf7acd90 to your computer and use it in GitHub Desktop.
F#: Reduce function dependencies
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