Skip to content

Instantly share code, notes, and snippets.

Avatar

Jon Harrop jdh30

View GitHub Profile
@jdh30
jdh30 / rewrite.md
Last active Apr 9, 2020
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>
@jdh30
jdh30 / RegExBenchmark.fs
Created May 9, 2019
Regular expression benchmark in F#
View RegExBenchmark.fs
open System.Text.RegularExpressions
let str = System.IO.File.ReadAllText "C:\\Users\\Jon\\Documents\\pg10.txt"
let re = System.IO.File.ReadAllText "C:\\Users\\Jon\\Documents\\re.txt"
for _ in 1..3 do
let timer = System.Diagnostics.Stopwatch.StartNew()
let re = Regex(re, RegexOptions.Compiled)
let res = Array.Parallel.init 4 (fun _ -> re.Split str |> Seq.sumBy (fun m -> m.Length))
printfn "%A %fs" res timer.Elapsed.TotalSeconds
@jdh30
jdh30 / binarytrees.fs
Created May 9, 2019
Hans Boehm's binary trees benchmark in F#
View binarytrees.fs
[<CompilationRepresentation(CompilationRepresentationFlags.UseNullAsTrueValue)>]
type Tree = Empty | Node of Tree * Tree
let rec make depth =
if depth=0 then Empty else Node(make (depth-1), make (depth-1))
let rec check = function
| Empty -> 1
| Node(l, r) -> 1 + check l + check r
@jdh30
jdh30 / binarytrees.cpp
Created May 9, 2019
Hans Boehm's binary trees benchmark in C++ using the default new+delete
View binarytrees.cpp
#include <algorithm>
#include <iostream>
#include <chrono>
struct Node {
Node *l, *r;
Node() : l(0), r(0) {}
Node(Node* l2, Node* r2) : l(l2), r(r2) {}
~Node() { delete l; delete r; }
int check() const {
@jdh30
jdh30 / gist:438afaed15d3bb7af3bfaedf54c4852b
Created Apr 28, 2019
McCarthy's definition of LISP 1.5 in itself
View gist:438afaed15d3bb7af3bfaedf54c4852b
; 20LOC to define a useful language
; However, no strings, IO, closure semantics or GC.
apply[fn;x;a] =
[atom[fn] -> [eq[fn;CAR] -> caar[x];
eq[fn;CDR] -> cdar[x];
eq[fn;CONS] -> cons[car[x];cadr[x]];
eq[fn;ATOM] -> atom[car[x]];
eq[fn;EQ] -> eq[car[x];cadr[x]];
T -> apply[eval[fn;a];x;a]];
You can’t perform that action at this time.