Skip to content

Instantly share code, notes, and snippets.

@ENAML
ENAML / diff_map.ml
Created February 8, 2019 04:52
simple diffing of two ordered maps
open Base
let left x = `Left(x)
let right x = `Right(x)
let change a b = `Change(a, b)
let rec diff_seqs ?(acc = []) s0 s1 =
let cmp = Poly.compare in
let v_eq = Poly.equal in
match Sequence.next s0, Sequence.next s1 with
@ENAML
ENAML / stringify.ml
Last active February 5, 2019 04:21
Ocaml Stringifier GADT
(* Stringify (WIP)
------------------------------------------------------------------
Generic, type-safe [to_string] utility.
Made for fun while reading about GADTs in Ocaml :-)
GADT References:
- https://en.wikipedia.org/wiki/Generalized_algebraic_data_type
- https://caml.inria.fr/pub/docs/manual-ocaml/extn.html#s%3Agadts
- https://blog.janestreet.com/why-gadts-matter-for-performance/
@ENAML
ENAML / make_tbl.ml
Created December 16, 2018 06:09
Creates an ascii table from a 2D list mtx of strings.
open Core
(* Creates an ascii table from a 2D list mtx of strings. *)
let make_tbl data =
(* common vars *)
let (fold, map, map2) = List.(fold, map, map2_exn) in
let (sep_h, sep_v, sep_x) = ('-', '|', '+') in
let text_pad = 2 in
(* compute max length of each column in tbl *)
@ENAML
ENAML / settings.json
Last active February 9, 2019 02:57
VSCode User Settings
{
/*
==============================
Base Config
==============================
*/
"workbench.activityBar.visible": true,
"workbench.statusBar.visible": true,
"editor.tabSize": 2,
"editor.rulers": [80],
@ENAML
ENAML / CustomOps.re
Created September 29, 2018 20:14
custom operators in ReasonML
/*
See: http://2ality.com/2017/12/functions-reasonml.html#operators
*/
module MapOps {
module HashMap = Belt.HashMap.String;
let ( @@ ) = (map, key) => {
map
|> HashMap.get(_, key)
|> Belt.Option.getExn
@ENAML
ENAML / ClassTest.re
Created September 29, 2018 20:12
ReasonML class example
/**
* Class test:
* ---------------
* (see: https://v1.realworldocaml.org/v1/en/html/classes.html)
*/
class stack ('a) (init) = {
as _self;
val mutable v: list('a) = init;
pub pop = () => {
@ENAML
ENAML / HashableComparableEx.re
Last active December 11, 2020 11:52
creating custom ReasonML comparable / hashable types that can be used with Bucklescript's Belt Hash/Map/Set types
/*
ocaml src: https://bucklescript.github.io/bucklescript/api/Belt.html
*/
/* base type */
type intTuple = (int, int);
/* hashable tuple */
module TupleHashable = Belt.Id.MakeHashable({
@ENAML
ENAML / justify.js
Created August 28, 2018 02:59
justify-text-js
const { log } = console;
const SPACE = " ";
const isString = (val) => typeof val === 'string' || val instanceof String;
/**
* Justify Text
* ============
* TODO: description
*/
@ENAML
ENAML / game_of_life.re
Last active August 19, 2018 20:19
Conway's Game of Life in Reason.ml
/**
* Game of Life in Reason.ml
* =========================
*/
/**
* Coord
*/
module Coord = {
/* Type defs: */
// run
const ctx = initCtx();
const text = `hello my friends`;
draw({ size: 16, fontFace: `monospace` }, text, 10, 50);
// initialize canvas context
function initCtx(w = 600, h = 400) {
// create canvas & ctx
const canvas = document.createElement('canvas');
document.body.appendChild(canvas);