Skip to content

Instantly share code, notes, and snippets.

Jon Harrop jdh30

Block or report user

Report or block jdh30

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@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]];
@jdh30
jdh30 / Eddy.fs
Created Feb 28, 2019
Memory management benchmark - shedding Eddys
View Eddy.fs
(*
This benchmark maintains an array of roots and randomly either pushes
onto them and relinks them or empties them. If I designed it correctly
then this should rapidly create unreachable subgraphs, some of which
are cyclic.
This is a torture test for any kind of reference counting.
*)
type Vertex = { mutable Dst: Vertex }
@jdh30
jdh30 / promptness.fs
Last active Feb 28, 2019
A program for which prompt collection is practically impossible
View promptness.fs
(*
The resizable array xs is required for the first half of the execution of
this "test" function but not the last half. Collecting xs as early as possible
would mean collecting it halfway through the execution of "test" but this
is practically impossible. Doing so in general is equivalent to solving the
halting problem.
*)
let next n =
6364136223846793005UL*n + 1442695040888963407UL
You can’t perform that action at this time.