Skip to content

Instantly share code, notes, and snippets.

@rapha
rapha / main.ml
Created May 8, 2012 02:54
Demo argv parsing in Ocaml
module Args = struct
type 'a arg_spec = { name: string; desc: string; of_string: (string -> 'a) }
exception RequiredArgMissing of string (* name *)
exception BadArgValue of string * string (* name, value *)
let find_given_value spec =
(* TODO allow user to specify argv *)
let arg_list = Array.to_list Sys.argv in
let flag = "-" ^ (spec.name) in
let rec find_value_in = function
@rapha
rapha / uniq.ml
Created September 13, 2011 19:26
Ocaml hack to remove duplicate lines in input
let rec next_line seen =
let line = read_line () in
if not (List.mem line seen) then print_endline line;
next_line (line :: seen)
in
try next_line [] with End_of_file -> ()
@rapha
rapha / life.ml
Created May 18, 2011 01:20
Conway's Game of Life in OCaml
open Batteries_uni
open List
type cell = Dead | Alive
type grid = cell list list
let set_value grid (row,col) value =
let old_row = at grid row in
let new_row = (take col old_row) @ [value] @ (drop (col + 1) old_row) in
(take row grid) @ [new_row] @ (drop (row + 1) grid)
@rapha
rapha / Makefile
Created March 31, 2011 16:53
Bowling kata in Ocaml
all: spec
spec: game.cmo spec.ml
ospecl spec.ml
game.cmo:
ocamlc -c game.ml
clean:
rm *.cm*
@rapha
rapha / init.js
Created September 6, 2010 12:22
using rhino continuations to speed up start-up time of env.js tests
(function() {
var snap = function(filename) {
return new Continuation()
}
var top = {};
load.call(top, 'env.rhino.js');
load.call(top, 'jquery-1.4.2.js');
top.window.location = 'page.html';
top.__context__ = null;
@rapha
rapha / simulation.ml
Created March 17, 2010 10:25
Simulation in OCaml of commit contention to a central repository between multiple parallel builds. Based on a similar Python version by Julio Maia.
open List
let num_devs = 20
let failure_rate = 0.20
let build_time = (15. (* mins *) /. 60.) (* hours *)
let commit_wait_timeslots = 4. /. build_time (* on average *)
let working_hours = 8.
let num_timeslots = working_hours /. build_time |> int_of_float
type build_status = Idle | Building | Pass | Retry | Fail
@rapha
rapha / word_count.ml
Created January 1, 2010 06:47
word count in OCaml
open Batteries
let rec files path =
if Shell.is_directory path then
path |> Shell.readdir |> Array.enum |> map ((^) (path ^ "/")) |> map files |> Enum.flatten
else
Enum.singleton path
let read_file filename =
File.with_file_in filename IO.read_all
@rapha
rapha / lookup.ml
Created December 2, 2009 00:19
Dictionary using closures in OCaml
let empty key = None
let put key value lookup =
(fun k -> if (k = key) then Some value else (lookup k))
(* use *)
let (|>) x f = f x
let lookup = empty |> put "a" 1 |> put "b" 2 |> put "c" 3;;
@rapha
rapha / chop.scala
Created October 14, 2009 12:55
Karate Chop Kata in Scala
def chop(needle : Int, haystack : Array[Int]) : Int = {
if (haystack.length == 0) return -1
val midPoint = haystack.length / 2
val midValue = haystack(midPoint)
if (midValue == needle)
midPoint
else if (needle < midValue)
chop(needle, haystack.slice(0, midPoint))
else
@rapha
rapha / mixin.js
Created August 10, 2009 09:52
Javascript mixin implementation
function mixable() {
var parents = [];
return {
mixin: function(parent) { parents.push(parent); },
__noSuchMethod__: function(id, args) {
for each (var parent in parents) {
if (typeof parent[id] === 'function') {
return parent[id].apply(this, args);
}
}