Skip to content

Instantly share code, notes, and snippets.

View keleshev's full-sized avatar
🐪

Vladimir Keleshev keleshev

🐪
View GitHub Profile
function verboseRegExp(input) {
if (input.raw.length !== 1) {
throw Error('verboseRegExp: interpolation is not supported');
}
let source = input.raw[0];
let regexp = /(?<!\\)\s|[/][/].*|[/][*][\s\S]*[*][/]/g;
let result = source.replace(regexp, '');

Comparision of JaneStreet Base and OCaml Stdlib

How to use Base

Either use Base selectively:

# #show List.hd;;
@keleshev
keleshev / fold.ml
Last active November 20, 2024 09:53
(* From blog post Interpretations of Fold,
https://keleshev.com/interpretations-of-fold *)
let (=>) left right = print_char (if left = right then '.' else 'F')
open Printf
let id x = x
let const x = fun _ -> x
let sum = List.fold_left (+) 0
let (>>) f g x = g (f x)
@keleshev
keleshev / Makefile
Last active November 16, 2024 20:43
ast.exe: ast.c
gcc -Werror -Wswitch -Wimplicit-fallthrough ast.c -o ast.exe
clean:
rm -f *.o *.exe *.s a.out
test.s: ast.exe
./ast.exe > test.s
@keleshev
keleshev / Lexer.l
Last active September 27, 2024 08:13
%{
#include <stdio.h>
#include "parser.h"
%}
%%
[ \r\n\t]* { continue; /* Skip blanks. */ }
[0-9]+ { sscanf(yytext, "%d", &yylval->value);
return TOKEN_NUMBER; }
(* Pretty-printing in OCaml: A Format Primer
https://keleshev.com/pretty-printing-in-ocaml-a-format-primer *)
let printf = Format.printf
let fprintf = Format.fprintf
let sprintf = Format.asprintf
let pp_string ppf string = fprintf ppf "%S" string
let pp_print_list ~sep pp_item =
let distance' ~bound a b =
let n = String.length a and m = String.length b in
assert (n >= m);
if n - m > bound then None else
let fill_bound = min m bound in
let previous = Array.make (m + 1) max_int in
module Cortege0 = struct
type _ t =
| [] : unit t
| (::) : 'a * 'b t -> ('a -> 'b) t
end
let printf = Format.printf
let fprintf = Format.fprintf
let dprintf = Format.dprintf
let pp_list ~sep pp =
Format.pp_print_list ~pp_sep:(fun ppf () -> Format.fprintf ppf sep) pp
let pp_string ppf string = fprintf ppf "%S" string
let many = [
open Printf
module Syntax = struct
type t =
| Unit
| Boolean of bool
| Number of int
| Name of string
| Divide of t * t