Skip to content

Instantly share code, notes, and snippets.

@jdh30
jdh30 / hashTable.ml
Created February 21, 2022 00:37
Open addressed hash table in OCaml
let primes =
[|3; 7; 13; 31; 61; 127; 251; 509; 1021; 2039; 4093; 8191; 16381; 32749;
65521; 131071; 262139; 524287; 1048573; 2097143; 4194301; 8388593;
16777213; 33554393; 67108859; 134217689; 268435399; 536870909; 1073741789;
2147483647|]
let mods =
[|(fun n -> n mod 3);
(fun n -> n mod 7);
(fun n -> n mod 13);
@jdh30
jdh30 / upload.sh
Last active May 17, 2021 21:51
Upload files from this machine to another without clobbering.
# Upload files from this machine to another without clobbering.
# Usage:
# ./upload.sh <srcdir> <user@host> <dstdir>
tar -cz $1 | ssh $2 "tar -kzxf - -C ${3}"
@jdh30
jdh30 / MoreArrayParallel.fs
Created March 15, 2021 11:12
More functions for F#'s Array.Parallel module
open System.Threading
/// Spawn a new Task.
let task f x =
Tasks.Task<_>.Factory.StartNew(fun () -> f x)
module internal Internal =
/// Compute "map f [i0, i2) |> reduce g" in parallel using divide and conquer.
/// Assumes "f" is associative but does not assume that it is commutative.
/// Therefore, this function can return "f (f 0 1) (f 2 3)" but not "f 1 0" etc.
@jdh30
jdh30 / SpellCorrektor.fs
Created March 14, 2021 18:24
Peter Norvig's spelling corrector written in F#
open System.Text.RegularExpressions
let alphabet = ['a'..'z']
let edits1 (w: string) =
seq { for i in 0 .. w.Length do
if i < w.Length then
yield w.[0..i-1] + w.[i+1..] // Delete
if i < w.Length-1 then
yield w.[0..i-1] + w.[i+1..i+1] + w.[i..i] + w.[i+2..] // Swap
@jdh30
jdh30 / compiler7.fs
Created March 14, 2021 17:42
F# version of my tiny ARM A32 compiler version 7
(*
This tiny 211-line compiler converts programs written in a little ML dialect into 32-bit ARM
assembly.
This program is most easily run using FSI with flags to turn off FSI's output so the output
of this program can be piped into a file:
dotnet fsi --quiet --exec compiler7.fs >fib.s
That file can then be compiled with:
@jdh30
jdh30 / Init.fs
Last active February 28, 2020 01:05
Avoiding the undebuggable type initialization exception in F#
let mutable globalException : System.Exception = null // Compiler generated
let myGlobal =
if isNull globalException then // Compiler generated
try // Compiler generated
System.IO.File.ReadAllText "DoesNotExist.txt"
with e -> // Compiler generated
globalException <- e // Compiler generated
Unchecked.defaultof<_> // Compiler generated
else Unchecked.defaultof<_> // Compiler generated
@jdh30
jdh30 / DraggableRectangles.fs
Created September 17, 2019 01:23
F#+WPF solution to the Draggable Rectangles challenge by Panicz Godek
open System.Windows
let goldenRatio = (1.0 + sqrt 5.0) / 2.0
let newBrush =
let mutable hue = 0.0
fun () ->
hue <- hue + System.Math.PI / goldenRatio
let s x = byte(255.0 * x)
let c x = 0.5 * (cos x + 1.0)
@jdh30
jdh30 / AsyncEcho.fs
Created August 14, 2019 14:08
Async echo server in F#
do
use listener = new System.Net.HttpListener()
listener.Prefixes.Add "http://localhost:8080/"
listener.Start()
while true do
let context = listener.GetContext()
async { use input = context.Request.InputStream
use output = context.Response.OutputStream
input.CopyTo output }
|> Async.Start
@jdh30
jdh30 / AsyncEcho.cpp
Created August 14, 2019 14:07
Async echo server in C++ (from the Boost docs)
//
// async_tcp_echo_server.cpp
// ~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2012 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
@jdh30
jdh30 / RegExBenchmark.cpp
Created May 9, 2019 10:19
Regular expression benchmark in C++
#include "stdafx.h"
#include <windows.h>
#include <regex>
#include <vector>
#include <string>
#include <fstream>
#include <cstdio>
#include <codecvt>