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
module Promise = | |
let sleep msecs = | |
let promise = Promise<unit> () | |
let timer = new System.Timers.Timer (float msecs) | |
timer.AutoReset <- false | |
let eventHandler ev = | |
promise.Resolve () | |
timer.Elapsed.Add eventHandler |
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
let pr = Promise () | |
pr.Catch (fun err -> printfn "Got Exception: %A" (err.ToString ())) |> ignore | |
pr.Reject (Exception "error") |
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
let pr = Promise () | |
pr.Then (fun x -> printfn "Got value: %A" x) |> ignore | |
pr.Resolve 42 |
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 Promise<'a> () = | |
let mutable result : Result<'a, exn> option = None | |
let thenCbs = List<Callback<'a>> () | |
let catchCbs = List<Callback<exn>> () | |
member this.Resolve (value: 'a) = | |
result <- Some <| Ok value | |
for cb in thenCbs do | |
cb value |
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
// Return a function that takes a callback that takes the result. | |
let times10 (x: int) : ((int -> unit) -> unit) = | |
let thenDo (cb : int -> unit) = | |
cb (x * 10) | |
thenDo | |
let cb result = | |
printfn "%A" result | |
let thenDo = times10 42 |
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
let times10 (x : int) (cont : int -> unit) : unit = | |
cont (x * 10) | |
let cb result = | |
printfn "%A" result | |
do times10 42 cb |
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
// Unindented Pythagoras CPS | |
let pythagorasCps a b return' : unit = | |
squareCps a (fun aa -> | |
squareCps b (fun bb -> | |
addCps aa bb (fun aabb -> | |
sqrtCps aabb (fun result -> | |
return' result)))) |
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
// Pythagoras rewritten using 'let' statements | |
let pythagoras a b = | |
let aa = square a | |
let bb = square b | |
let aabb = add aa bb | |
let result = sqrt aabb | |
result | |
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
let addCps a b cont : unit = | |
cont (a + b) | |
let squareCps x cont : unit = | |
cont (x * x) | |
let sqrtCps x cont : unit = | |
cont (sqrt x) | |
// Pythagoras rewritten in CPS |
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
let add a b = a + b | |
let square x = x * x | |
let pythagoras a b = sqrt (add (square a) (square b)) | |
let result = pythagoras 10.0 20.0 |