Skip to content

Instantly share code, notes, and snippets.

@pqwy
pqwy / dumper.ml
Last active November 4, 2015 20:59
dumper
let encodev ?(encoding=`UTF_8) xs =
let b = Buffer.create 16 in
let e = Uutf.encoder encoding (`Buffer b) in
List.iter (fun x -> Uutf.encode e (`Uchar x) |> ignore) xs;
Uutf.encode e `End |> ignore;
Buffer.contents b
module S = struct
@pqwy
pqwy / lru.ml
Last active December 7, 2015 18:32
lru!
module Option = struct
let (>>=) a fb = match a with Some x -> fb x | _ -> None
let (>|=) a f = match a with Some x -> Some (f x) | _ -> None
end
type ('k, 'v) tree =
| Tip
| Node of 'k * 'v * int * ('k, 'v) tree * ('k, 'v) tree
type ('k, 'v) t = { gen : int ; size : int ; tree : ('k, 'v) tree }
@pqwy
pqwy / multimap.ml
Created March 13, 2016 17:15
Multimap
module MultiMap (T : Map.OrderedType) : sig
type 'a t
val of_list : (T.t list * 'a) list -> 'a t
val find : T.t -> 'a t -> 'a option
val find_update : T.t -> ('a -> 'a) -> 'a t -> ('a * 'a t) option
end =
struct
module TMap = Map.Make (T)
module IMap = Map.Make (struct type t = int let compare a b = compare a b end)
@pqwy
pqwy / delimcc.scm
Created April 25, 2017 13:24
call/cc -> delimited continuations
(define-syntax let/cc
(syntax-rules () [(_ cc e ...) (call/cc (lambda (cc) e ...))]))
(define prompt
(make-parameter (lambda _ (raise 'undelimited-shift))))
(define (call-with-reset f)
(let/cc c-frame
(parameterize