Skip to content

Instantly share code, notes, and snippets.

View jobjo's full-sized avatar

Joel Bjornson jobjo

  • London
View GitHub Profile
(* Functors and modular implicits
* opam switch 4.02.0+modular-implicits
* eval `opam config env`
*)
module type FUNCTOR = sig
type 'a t
val map : ('a -> 'b) -> 'a t -> 'b t
end
let map {F : FUNCTOR} = F.map
(* General functor signature. *)
module type FUNCTOR = sig
type 'a t
val map : ('a -> 'b) -> 'a t -> 'b t
end
(* General def of catamorphism module, parameterized by a functor *)
module Cata (F : FUNCTOR) = struct
type 'a t = 'a F.t
@jobjo
jobjo / Higher-Kinded F#
Last active August 29, 2015 14:11
What if F# supported higher-kinded polymorphism?
// Functor interface.
type Functor<'F> = { map : ('T -> 'U) -> 'F<'T> -> 'F<'U> }
// Monad interface.
type Monad<'M> = {
return : 'T -> 'M<'T>
join : 'M<'M<'T>> -> 'M<'T>
}
// Monad "library" (operations over monads).
@jobjo
jobjo / Aggregations.fs
Last active August 29, 2015 14:10
Aggregating elements in F#
open System
open System.Collections.Generic
// The problem I'm addressing is aggregating elements from a tree.
// In my particular scenario I'm interested in collecting all leaf nodes
// into an array. The fundamental problem is captured by the different versions
// of aggregate, listed below. The functions are merely simulating the branching
// and aggregation steps. I'm not concerned about the order of the elements.
// In the comments are the total times in milliseconds
// running the functions with n = 1e6. That is generating an array of roughly
Formlet.Do {
let! n = Controls.Button "Add Tabs"
return!
List.init n (fun ix ->
("Tab " + string ix, Controls.Input "")
)
|> Controls.TabsChoose
}
var createCounter = function() {
var x = 0;
return (
function () {
x = x + 1;
return x;
}
);
}
type MyControl() =
inherit IntelliFactory.WebSharper.Web.Control ()
[<JavaScript>]
override this.Body =
let inpCtrl str =
Controls.Input str
|> Enhance.WithSubmitButton
|> Enhance.WithFormContainer
[<JavaScript>]
let InputDate () =
let inputId = NewId ()
Controls.InputDatepicker None
|> Formlet.MapElement (fun el -> Div [Attr.Id inputId] -< [el])
|>! OnAfterRender (fun el ->
JQuery.JQuery.Of("#" + inputId).Find("input").Val("").Ignore
)
open M
Foo ()
Formlet.Do {
let! x = Controls.RadioButtonGroup None [("item1","item1");("item2","item2");("item3","item3")]
return x
}
|> Formlet.Map Some
|> Formlet.InitWith None
|> Enhance.WithSubmitAndResetButtons
|> Enhance.WithFormContainer