Skip to content

Instantly share code, notes, and snippets.

View davesnx's full-sized avatar

David Sancho davesnx

View GitHub Profile
@davesnx
davesnx / main.ml
Created September 28, 2025 16:31
Simple typed lambda calculus interpreter with de bruijn indices and beta reduction
module Index = struct
type t = int * string
let create i s = (i, s)
let eq t1 t2 = fst t1 = fst t2
let enter_level (i, s) = (i + 1, s)
let pp_index i = Format.sprintf "%s" (snd i)
end
type typ = Tvar | Tpoly | Tfun of typ * typ
@davesnx
davesnx / main.ml
Created September 28, 2025 16:17
effects.ml
type _ io =
| Sync : (unit -> 'a) -> 'a io
| FlatMap : 'a io * ('a -> 'b io) -> 'b io
| Async : ((('a -> unit) -> unit) -> unit) -> 'a io
| All : 'a io list -> 'a list io
| Sleep : float -> unit io
let flatMap f io = FlatMap (io, f)
let map f io = flatMap (fun a -> Sync (fun () -> f a)) io
[@@@warning "-27"]
let get_memory_info () =
let ic = open_in "/proc/self/status" in
let rec read_lines rss vmsize =
try
let line = input_line ic in
if String.starts_with ~prefix:"VmRSS:" line then
let rss_kb = String.sub line 6 (String.length line - 9) |> String.trim |> int_of_string in
read_lines (Some rss_kb) vmsize
@davesnx
davesnx / ppx.ml
Created February 3, 2025 15:07
a few functions to replace of_yojson dependency
(*
TODO: If we want to remove the dependency on of_json, we need to implement the json decoder manually.
TODO: Maybe use a custom deriving called "rsc" or similar where it handles the JSON/Promise/React.element. *)
let rec make_of_json ~loc (type_ : core_type) value =
match type_.ptyp_desc with
| Ptyp_constr ({ txt = Lident "int"; _ }, _) -> value
| Ptyp_constr ({ txt = Lident "string"; _ }, _) -> value
| Ptyp_constr ({ txt = Lident "bool"; _ }, _) -> value
| Ptyp_constr ({ txt = Lident "float"; _ }, _) -> value
@davesnx
davesnx / Responsive.re
Created July 25, 2024 11:00
Random design for "Adaptative" component
/* This type defines a value that 'depends' on the screen size, and changes based on `window.matchMedia` */
type dependant('value) = {
default: 'value,
mobile: option('value), /* 800px */
desktop: option('value), /* 1200px */
huge: option('value) /* 1900px */
};
type t('value) =
| Fixed('value)
@davesnx
davesnx / Makefile
Last active January 3, 2024 17:14
importMaps are cool
dev:
npx http-server -o .
open Ppxlib
module Builder = Ast_builder.Default
type target = Native | Js
let mode = ref Native
module Effect = struct
(* TODO: [%effect] is a little incomplete, only works with useEffect0 (not other useEffectX, neither useLayoutEffects) *)
let extractor = Ast_pattern.(__')
@davesnx
davesnx / print-rescript-parsetree.sh
Last active December 4, 2023 14:11
ReScript Parsetree print
npx bsc -dparsetree wat.res
npx bsc -dparsetree wat.ml
npx bsc -format wat.ml
@davesnx
davesnx / index.md
Last active October 10, 2024 16:04
Safer Tailwind with OCaml-derived languages

Safer Tailwind with OCaml-derived languages (OCaml, Reason, ReScript or Melange)

Tailwind is optimised to work with JavaScript/TypeScript ecosystems, but other languages might have good integrations also.

This is the case for OCaml-derived languages that are used to do Frontend development. For the most part, a tighter integration might not be needed and using the Tailwind setup guide and regular classNames with strings is good enought.

let make = (~text) => {
  <h1 className="text-3xl font-bold underline">{React.string(text)}<h1>
box(
{
padding: 5,
width: "320px",
border: "sm",
onClick: onClick,
onMouseLeave: onMouseLeave,
},
[
stack({ gap: 2, align: "center" }, [