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
// An asynchronous value | |
let valAsync = async { return 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
// An asynchronous function taking a long time before "returning" a value | |
let funcAsync () = | |
async { | |
do! Async.Sleep 5000 | |
return 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
// Callback handler | |
let myCallback x = | |
printfn "Got %A" x | |
// Asynchronous function taking a callback handler | |
let asyncFun x cb = | |
let result = x * 10 | |
cb result | |
// Call function with provided callback handler |
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 |
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
// 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
// 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
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
// 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
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 |