Skip to content

Instantly share code, notes, and snippets.

View gerardtoconnor's full-sized avatar

Gerard gerardtoconnor

View GitHub Profile
@gerardtoconnor
gerardtoconnor / ChunkList.fs
Created February 17, 2019 12:52
Inital Draft of a List that chunks to avoid growing onto LOH
open System.Collections.Generic
let CHUNK_SIZE = 2048
let INIT_SIZE = 32
[<AllowNullLiteral>]
type private CNode<'T>() =
let data = Array.zeroCreate<'T> INIT_SIZE
member val Tail = Unchecked.defaultof<CNode<'T>> with get, set
member val Data = data with get, set
let MyView model =
stackPanel {
children [
label { content "FontSize is:" }
label { content <@ model.FontSize @> ; fontSize <@ model.FontSize @> }
button {
content "Increase Font Size"
onClick (<@ model.FontSize @> ,fun _ _ -> model.FontSize + 2.) }
]
}
/////////////////
/// My Model defs
type SubModel = {
mutable Value1:string
mutable Counter:int
mutable MyRow : int
mutable MyCol : int
}
type MyModel = {
type Work =
| Idle
| Working of cts:CancellationTokenSource
let jsonView model =
let working = ref Idle
button {
content "json request" ;
onClick (<@ working.Value @>,<@ model.Message @>, fun _ _ (ct:CancellationToken) -> task {
use client = new System.Net.Http.HttpClient()
///////////////////
// Model definitions
type SubModel = {
Value1:Var<string>
Counter:Var<int>
Visible:Var<Visibility>
IsVisible:Var<bool>
}
type MyModel = {
/// Node Types
/// //////////////////
type ChoiceNode<'T>(inext:INode<'T>,ifail:INode<'T>,current:Zapp<'T>) =
let mutable next = inext
let mutable fail = ifail
interface INode<'T> with
member __.Next with get () = next and set v = next <- v
member __.Fail with get () = fail and set v = fail <- v
member inline x.Zero() =
// Zero presumes a true, failing must be explicit, progressing is implicit
x.DNode.Next.Apply x
member inline x.Return(result: bool) =
if result then
x.DNode.Next.Apply x // same as `next`
else
x.DNode.Fail.Apply x // same as None
type INode<'T> =
abstract member Next : INode<'T> with get, set
abstract member Fail : INode<'T> with get, set
abstract member Apply : State<'T> -> unit
and State<'T>(hctx: HttpContext, deps: 'T,amb: AsyncTaskMethodBuilder) =
let buffer = MemoryStreamPool.Get()
member val MethodBuilder : AsyncTaskMethodBuilder = amb with get
member val HttpContext = hctx with get
member val Dependencies = deps with get // alternative typed basic dependency injection system
member inline x.Bind(taskLike:^abl, continuation : ^inp -> unit) : unit =
let awt = (^abl : (member GetAwaiter : unit -> TaskAwaiter< ^inp>)(taskLike))
if awt.IsCompleted then
continuation (awt.GetResult())
else
let mutable awtref <- awt
let mutable smref <- GenericStateAwaiter< ^inp>(awt,continuation,x.MethodBuilder)
x.MethodBuilder.AwaitUnsafeOnCompleted(&awtref,&smref)
let bindTask (task : 'a Task) (continuation : 'a -> Step<'b>) =
let awt = task.GetAwaiter()
if awt.IsCompleted then // Proceed to the next step based on the result we already have.
continuation(awt.GetResult())
else // Await and continue later when a result is available.
Await (awt, (fun () -> continuation(awt.GetResult())))