Skip to content

Instantly share code, notes, and snippets.

@SchlenkR
Created February 10, 2019 22:18
Show Gist options
  • Save SchlenkR/8400b2089e5e0b5be9ce498cd3837453 to your computer and use it in GitHub Desktop.
Save SchlenkR/8400b2089e5e0b5be9ce498cd3837453 to your computer and use it in GitHub Desktop.
type RowPointer = int
type Writer = Writer of (RowPointer -> RowPointer)
let run (f:Writer) = match f with | Writer f -> f
type Cursor() =
member this.Bind(m: Writer, f: unit -> Writer) =
Writer(fun rp ->
let res = f() |> run
let newRp = rp |> run m
res newRp)
member this.Return (x:Unit) = Writer (fun a -> a)
let cursor = Cursor()
let write content =
Writer (fun (row:RowPointer) ->
printfn "WRITING ROW %d: %s" row content
row + 1)
let x = cursor {
do! (write "Hello 1")
do! (write "Hello 2")
[1..5]
|> List.map (fun v ->
do! (write "Hello 2")
)
|> ignore
return ()
}
(run x) 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment