Skip to content

Instantly share code, notes, and snippets.

View gerardtoconnor's full-sized avatar

Gerard gerardtoconnor

View GitHub Profile
@gerardtoconnor
gerardtoconnor / ContinuationHttpHandler.fs
Last active June 23, 2017 18:17
Test format of alternative continuation HttpHandler format to prevent need to warp sync results in async
// Unlike Suave bind format, the idea is that the handlers execute as pre-applied continuations, with the added benefit of
// allowing sync operations to be executed in a handler with no addional and wasteful async/task wrappers as the async/task
// of the subsequent succ/fail Continuation that is called can be returned, passed directly through, unlike bind
open System
open System.Text
open System.Threading.Tasks
open System.Collections.Generic
open Microsoft.AspNetCore.Http
@gerardtoconnor
gerardtoconnor / giraffe composition.fs
Created July 13, 2017 00:14
Current Suave/Giraffe HttpHandler Composition
let bind (handler : HttpHandler) =
fun (result : HttpHandlerResult) ->
async {
let! ctxOpt = result
match ctxOpt with
| None -> return None
| Some ctx ->
match ctx.Response.HasStarted with
| true -> return Some ctx
| false -> return! handler ctx
@gerardtoconnor
gerardtoconnor / GirraffeHandlerExamples.fs
Created July 13, 2017 00:26
GirraffeHandlerExamples
// given a string path, return succssfull Some of HttpContext for binding
let route (path : string) =
fun (ctx : HttpContext) ->
if (getPath ctx).Equals path
then Some ctx
else None
|> async.Return
// given a list of handlers, itterate through list until a pipeline returns succssfull Some of HttpContext for binding
let rec choose (handlers : HttpHandler list) =
@gerardtoconnor
gerardtoconnor / NextFormat.fs
Last active July 13, 2017 07:36
Next Continuation Handler
type HttpHandlerResult = Task<HttpContext option> // same as previous
type HttpCont = HttpContext -> HttpHandlerResult // continuation same as previous HttpHandler
type HttpHandler = HttpCont -> HttpContext -> HttpHandlerResult // (HttpCont -> HttpCont) : Handler now has two input parameters
let compose (handler : HttpHandler) (handler2 : HttpHandler) : HttpHandler =
fun (next:HttpCont) (ctx : HttpContext) ->
// 1. last next will ultimately be a (fun ctx -> Some ctx) but allows to compose / short circuit with continuations
// 2. We can apply next function at load so all handlers have thier next function refs and need only apply ctx on run
@gerardtoconnor
gerardtoconnor / NextMiddleware.fs
Last active July 13, 2017 01:40
Example Middleware next format
type GiraffeMiddleware (next : RequestDelegate,
handler : HttpHandler,
loggerFactory : ILoggerFactory) =
member __.Invoke (ctx : HttpContext) =
task {
let finalNext = (fun ctx -> task { Some ctx } )
let! result = handler finalNext ctx
if (result.IsNone) then
@gerardtoconnor
gerardtoconnor / NextAsSome.fs
Created July 13, 2017 01:38
Showing similarities to option bind format
let route (path : string) : HttpHandler =
fun Some' ctx ->
if (getPath ctx).Equals path
then Some' ctx
else Task.FromResult None
namespace CRUDSample
open System.IO
open NPoco
open Microsoft.Data.Sqlite
type LunchSpot() =
member val ID = 0 with get, set
member val Name = "" with get, set
member val Latitude = 0.0 with get, set
string func6 = "DelaySign";
fsharpFunc2.Invoke((FSharpFunc<bool, Unit>) new FakeMsbuildTasks.getResponseFileFromTask\u004054\u002D5<a>(fsc)).Invoke(func6);
string func7 = "DisabledWarnings";
fsharpFunc1.Invoke((FSharpFunc<string, Unit>) new FakeMsbuildTasks.getResponseFileFromTask\u004055\u002D6<a>(fsc)).Invoke(func7);
FSharpFunc<FSharpFunc<string, Unit>, FSharpFunc<string, Unit>> fsharpFunc1 = (FSharpFunc<FSharpFunc<string, Unit>, FSharpFunc<string, Unit>>) new FakeMsbuildTasks.bind\u004026(prop);
FSharpFunc<FSharpFunc<bool, Unit>, FSharpFunc<string, Unit>> fsharpFunc2 = (FSharpFunc<FSharpFunc<bool, Unit>, FSharpFunc<string, Unit>>) new FakeMsbuildTasks.bindBool\u004028(prop);```
module App =
//////////////////
type Zapp<'T> = State<'T> -> unit // Zapp<'T> is temporary name for Zebra function handler
type DepContainer = {
Name :string
}
type Person = {
Name:string
@gerardtoconnor
gerardtoconnor / ParseOperatorOverloading.fsx
Created July 12, 2018 08:52
Parse operator overloading for pre-build a tightly typed conversion node for minimal allocs at runtime
type ValueOption<'T> =
struct
val HasValue : bool
val Value: 'T
new(a,b) = {HasValue = a; Value = b}
end
let inline VSome v = ValueOption<_>(true,v)
let inline VNone () = ValueOption<'T>(false,Unchecked.defaultof<'T>)
type Parse = Parse with