Skip to content

Instantly share code, notes, and snippets.

@atavener
atavener / benchtreap1.ml
Created May 15, 2018 12:58
OCaml versions of an "unscientific benchmark" treap; first one translated from the Rust version, and the second more idiomatic OCaml in a functional style. See: https://github.com/frol/completely-unscientific-benchmarks
(* Translated from Rust version at https://github.com/frol/completely-unscientific-benchmarks *)
type nodecell = node option
and node =
{ x: int;
y: int;
mutable left: nodecell;
mutable right: nodecell }
module Node = struct
@atavener
atavener / ev.ml
Created January 12, 2015 22:55
Safer and easier interface layer atop Tsdl.Sdl.Event
(* __ Interface-layer atop Tsdl.Sdl.Event _______________
*
*
* Rationale for not using Tsdl directly for events:
*
* -Tsdl events are too raw -- easily able to read incorrect fields without
* generating a compile-time error.
*
* -Tsdl events are cumbersome to use: "Sdl.Event.(get e long_field_name)",
* and event container "e" must be in scope.
@atavener
atavener / monad.ml
Last active September 22, 2018 14:43
A few monads to ultimately provide a Resource monad. This was created to work with Tsdl (tiny SDL bindings) which uses a result type (matching the Result monad here). I've found the Resource monad to be useful for a chain of dependent initializations (each step must succeed to continue), with the return value being the pair of a final result, an…
(*
A few monads... the incentive was to have an easy way to work with Tsdl.
The "Result" monad corresponds to the result type returned by most Tsdl calls.
"Release" accumulates a chain of "clean-up" functions.
Together they form "Resource" which handles Ok/Error results and accumulates
clean-up functions which are returned.
@atavener
atavener / hashes.c
Created October 25, 2014 00:04
Some hash functions to generate pseudo-random values per-coordinate, in C, with OCaml FFI.
/* Some hash functions to generate pseudo-random values per-coordinate.
* Useful for spatial synthesis, such as procedural textures.
*/
/*** Bob Jenkins' mix and final ***/
#define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
#define mix(a,b,c) \
{ \
@atavener
atavener / forest.ml
Created June 6, 2014 05:24
Magical Forest: Goats, Wolves, and Lions
(* "Magical Forest: Goats, Wolves, and Lions"
*
* Inspired by this reddit post:
* http://www.reddit.com/r/programming/comments/27e7r3/performance_comparison_of_java_8_c_11_and_the/
*
* The following code can be pasted into an OCaml REPL.
* Then a solution is called for like this:
*
* magical_forest (683, 1054, 1290);;
*
@atavener
atavener / database.ml
Created September 12, 2013 07:24
OCaml in-memory "database" -- I use this for component-based game-objects.
(* Database provides key-generation and table-instantiation,
* so that a key can be associated to various properties.
*)
(* This is for a fully-controlled specification...
*
* module Db = Database.Make (Database.IntKey)
* module Prop = Db.MultiInherit
* module PropHash = Prop.Table(Database.Hash)
* module Size = (val PropHash.create ~default:0 () : Db.Sig with type t = int)