Skip to content

Instantly share code, notes, and snippets.

@jtpaasch
jtpaasch / simpletimer2.ml
Created May 4, 2018 00:40
A simple module to time functions (OCaml)
(* Assume a module called [Proc.Cmd] with a [run] function is loaded. *)
(* Compile with the [unix.cma/cmxa] module. *)
module Timer = struct
let time f x =
let start = Unix.gettimeofday () in
let result = f x in
let stop = Unix.gettimeofday () in
let delta = stop -. start in
@jtpaasch
jtpaasch / simplelog.ml
Created May 4, 2018 02:12
A simple logging module (OCaml)
module Logs = struct
let hash = Hashtbl.create 256
let log name msg =
let ch = Hashtbl.find hash name in
Printf.fprintf ch "%s\n%!" msg
let close name =
let ch = Hashtbl.find hash name in
@jtpaasch
jtpaasch / simplecram.ml
Created May 10, 2018 15:49
A simple cram test runner (OCaml)
module Files = struct
let line c =
try
let result = input_line c in
Some result
with End_of_file -> None
let rec read c acc =
match line c with
@jtpaasch
jtpaasch / thunksandhashes.ml
Created May 11, 2018 12:20
An example of deferred execution with thunks and hashtables (OCaml)
module Logs = struct
let hash : (string, string) Hashtbl.t = Hashtbl.create 256
let show_all = fun () ->
Printf.printf "- - - HASH CONTENTS:\n%!";
Hashtbl.iter (fun key value -> Printf.printf "- - - '%s': '%s'\n%!" key value) hash
let register_msg key value =
Printf.printf "- Registering '%s': '%s'\n%!" key value;
@jtpaasch
jtpaasch / hashofchannels.ml
Last active May 11, 2018 18:06
Store out channels in a hash table.
module Logs = struct
exception NoSuchLog of string
let file_opts = [Unix.O_WRONLY; Unix.O_APPEND; Unix.O_CREAT]
let file_perm = 0o600
let hash : (string, out_channel) Hashtbl.t = Hashtbl.create 256
let register key oc = Hashtbl.add hash key oc
@jtpaasch
jtpaasch / handlecustomerrors.ml
Last active May 17, 2018 11:06
Handle custom errors (OCaml)
exception Foo of string
let main () =
print_endline "Running";
raise (Foo "Something went wrong")
let handle_errors f () =
Printexc.register_printer
(function
| Foo msg ->
@jtpaasch
jtpaasch / simplematch.ml
Created May 17, 2018 12:18
A simple string matcher (OCaml). Tries a literal match first, then a regex match.
let has_match tests =
let matches = List.filter (fun s -> s = true) tests in
List.length matches > 0
let is_exact_match str_1 str_2 = str_1 = str_2
let is_regex_match str_1 str_2 =
let r = Str.regexp str_1 in
Str.string_match r str_2 0
@jtpaasch
jtpaasch / colorizer.ml
Created May 18, 2018 12:41
Simple colorizer/formatter (Ocaml)
type ttyfmt =
| Plain
| Red
| Green
| Warn
| Emph
let reset_tag = "\x1b[0m"
let start_of fmt =
@jtpaasch
jtpaasch / tty_str.ml
Created May 18, 2018 20:29
Another colorizer/formatter for TTYs (OCaml)
module Tty_str = struct
type ttyfmt =
| Plain
| Red
| Green
| Yellow
| Bold
| Dim
| Italic
@jtpaasch
jtpaasch / tty_table.ml
Created May 27, 2018 16:08
Construct a TTY-printable table string from a table, i.e., a list of string lists (OCaml).
(* A utility for building a table that can be printed on a TTY.
Use it by calling [create], and passing it a list of string lists.
The argument is a list of rows, where each row is a list of strings.
The [create] function returns a string you can print to a TTY. *)
let rec longest items res =
match items with
| [] -> res
| hd :: tl ->
match hd > res with