Skip to content

Instantly share code, notes, and snippets.

View dbrattli's full-sized avatar
👨‍💻
Python ❤️ F#

Dag Brattli dbrattli

👨‍💻
Python ❤️ F#
View GitHub Profile
@dbrattli
dbrattli / ScheduledPromise.fs
Last active October 15, 2018 18:06
Promise with Scheduler
type Promise<'a> () =
let mutable result : Result<'a, exn> option = None
let catchCbs = List<Callback<exn>> ()
let thenCbs = List<Callback<'a>> ()
member this.Resolve (value: 'a) =
result <- Some <| Ok value
for cb in thenCbs do
scheduler.Post (fun () -> cb value)
@dbrattli
dbrattli / FactorialPromise.fs
Created October 14, 2018 21:39
Factorial Promise
let rec factorial (n : bigint) : Promise<bigint> =
let pr = Promise<bigint> ()
pr.Resolve n
pr.Then (fun value ->
if value = (bigint 0) then
single (bigint 1)
else
factorial(n - (bigint 1)).Then (fun res -> single (n * res))
)
@dbrattli
dbrattli / Scheduler.fs
Last active October 15, 2018 18:04
Scheduler
type Computation = unit -> unit
let scheduler = MailboxProcessor<Computation>.Start (fun inbox ->
let rec loop () = async {
let! action = inbox.Receive()
action ()
do! loop ()
}
loop ()
@dbrattli
dbrattli / PromiseExpression.fs
Last active October 15, 2018 05:23
Promise Computational Expression
let pr = promise {
let! res1 = fetch "http://test1"
let! res2 = fetch "http://test2"
let! res3 = fetch "http://test3"
let! res4 = fetch "http://test3"
return res4
}
@dbrattli
dbrattli / QueryBuilder.fs
Last active October 14, 2018 16:39
Query Builder
type QueryBuilder() =
member this.Zero () = Promise ()
member this.ReturnFrom (xs: Promise<'a>) = xs
member this.Return x =
let promise = Promise<'a> ()
promise.Resolve x
promise
member this.Bind(source: Promise<'a>, fn: 'a -> Promise<'b>) : Promise<'b> = source.Then fn
@dbrattli
dbrattli / PromiseOutsideChaining.fs
Last active October 14, 2018 15:59
Promise Outside Chaining
fetch("http://test1")
.Then(fun res1 -> fetch "http://test2")
.Then(fun res2 -> fetch "http://test3")
.Then(fun res3 -> fetch "http://test4")
.Then(fun res4 ->
printfn "Got response %A" res4
)
|> ignore
@dbrattli
dbrattli / ThenExtension.fs
Created October 14, 2018 15:52
Promise Then Extension
// Make a new extension method on Promise
type Promise<'a> with
member this.Then(fn: 'a -> Promise<'b>) : Promise<'b> =
let newPromise = Promise<'b> ()
this.Then(fun a ->
let tmpPromise = fn a
tmpPromise.Then(newPromise.Resolve).Catch(newPromise.Reject)
|> ignore
).Catch(newPromise.Reject)
|> ignore
@dbrattli
dbrattli / PromiseInsideChaining.fs
Created October 14, 2018 14:37
Promise Inside Chaining
fetch("http://test1")
.Then(fun res1 ->
fetch("http://test2")
.Then(fun res2 ->
fetch("http://test3")
.Then(fun res3 ->
fetch("http://test4")
.Then(fun res4 ->
printfn "Got last response: %A" res4
) |> ignore
@dbrattli
dbrattli / UseFetch.fs
Created October 14, 2018 14:33
Use fetch
let pr = fetch "http://test"
pr.Then (fun value ->
printfn "Got response from: %A" value
) |> ignore
@dbrattli
dbrattli / Fetch.fs
Created October 14, 2018 12:48
Fetch
let fetch(uri: string) =
let timeout = Promise.sleep 100
let response = sprintf "Got response from: %s" uri
let result = Promise<string> ()
timeout.Then(fun _ -> result.Resolve(response) ) |> ignore
result