Skip to content

Instantly share code, notes, and snippets.

@hackwaly
hackwaly / avltree2.ml
Last active August 25, 2023 14:56
Treap map
type ('a, 'b) tree =
| Empty
| Node of
{ left : ('a, 'b) tree
; right : ('a, 'b) tree
; key : 'a
; value : 'b
; height : int
}
@hackwaly
hackwaly / HAMT.ml
Created August 8, 2023 02:57 — forked from Guest0x0/HAMT.ml
Hash Array Mapped Trie implementation & benchmark in OCaml
(* HAMT v.s. AVL benchmark.
test for their usage as string map.
usage:
> ocamlopt HAMT.ml -o <executable-name>
> <executable-name> (avl|hamt) (random|ordered) <key-length>
time data of the form:
<tree-size> <add-time> <find-time>
@hackwaly
hackwaly / dune
Last active August 6, 2023 04:13
ppx for polymorphism gadt
(library
(name ppx_poly_gadt)
(kind ppx_rewriter)
(preprocess
(pps ppxlib.metaquot))
(libraries ppxlib))
@hackwaly
hackwaly / MutList.re
Created June 10, 2023 19:09
Efficent mutable list in reasonml
let dummy_arr = [||];
module type ELT = {
type t;
let uninitialized: t;
};
module Make = (Elt: ELT) => {
type t('a) = {
type ('data, 'index) node =
{ left : ('data, 'index) node option
; right : ('data, 'index) node option
; priority : int
; mutable data : 'data
; mutable index : 'index
}
type ('a, 'b) t = ('a, 'b) node option
@hackwaly
hackwaly / Dllist.ml
Created August 18, 2022 07:21
Doubly linked list in OCaml
(* Doubly linked list *)
type 'a node = { mutable prev : 'a node; mutable next : 'a node; data : 'a }
type 'a list = List of 'a node [@@unboxed]
let create () =
let rec root = { prev = root; next = root; data = Obj.magic `Dummy } in
List root
let iter (List root) f =
@hackwaly
hackwaly / Treap.ts
Last active January 26, 2022 13:33
Generic persistent treap
import { assert } from "./Errors";
export type treap<a, b> = node<a, b> | undefined;
export type node<a, b> = {
left: treap<a, b>;
right: treap<a, b>;
priority: number;
data: a;
subtreeData: b;
// Generated by ReScript, PLEASE EDIT WITH CARE
import * as Caml_array from "rescript/lib/es6/caml_array.js";
function cons(_record, _map) {
while(true) {
var map = _map;
var record = _record;
if (!map) {
return {
@hackwaly
hackwaly / Lwt_saga.ml
Created January 11, 2021 19:32
OCaml saga powered by lwt
type 'a t = {
f : unit -> 'a Lwt.t;
}
type 'a task = <
is_running : bool;
is_cancelled : bool;
result : 'a option;
error : (exn * Printexc.raw_backtrace) option;
done_ : unit Lwt.t;
%{
[@@@warning "-39"]
%}
%start <unit> file
%%
file:
| MODULE UIDENT item* EOF {}