Skip to content

Instantly share code, notes, and snippets.

Avatar

Jonathan Harrop jdh30

View GitHub Profile
@jdh30
jdh30 / upload.sh
Last active May 17, 2021
Upload files from this machine to another without clobbering.
View upload.sh
# 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 Mar 15, 2021
More functions for F#'s Array.Parallel module
View MoreArrayParallel.fs
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 Mar 14, 2021
Peter Norvig's spelling corrector written in F#
View SpellCorrektor.fs
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 Mar 14, 2021
F# version of my tiny ARM A32 compiler version 7
View compiler7.fs
(*
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 / rewrite.md
Last active Feb 2, 2021
Introduction to term rewriting
View rewrite.md

Creating a new term-rewrite language

This article walks through the design and implementation of a simple but remarkably powerful new programming language that uses term rewriting as an evaluation model and, consequently, is well suited to computer algebra.

The term rewriter

We begin with the definition of an expression type Expr:

  type Expr =
    | Int of System.Numerics.BigInteger
@jdh30
jdh30 / Init.fs
Last active Feb 28, 2020
Avoiding the undebuggable type initialization exception in F#
View Init.fs
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 Sep 17, 2019
F#+WPF solution to the Draggable Rectangles challenge by Panicz Godek
View DraggableRectangles.fs
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 Aug 14, 2019
Async echo server in F#
View AsyncEcho.fs
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 Aug 14, 2019
Async echo server in C++ (from the Boost docs)
View AsyncEcho.cpp
//
// 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
Regular expression benchmark in C++
View RegExBenchmark.cpp
#include "stdafx.h"
#include <windows.h>
#include <regex>
#include <vector>
#include <string>
#include <fstream>
#include <cstdio>
#include <codecvt>