Skip to content

Instantly share code, notes, and snippets.

Avatar

Jon Harrop jdh30

View GitHub Profile
@jdh30
jdh30 / MiniML.ml
Created Mar 4, 2018
LLVM-based compiler written in ~100 lines of OCaml
View MiniML.ml
(* See https://groups.google.com/forum/#!msg/fa.caml/i6IgSFX8XkY/4khF8z1V7loJ *)
type expr =
| Int of int
| Var of string
| BinOp of [ `Add | `Sub | `Leq ] * expr * expr
| If of expr * expr * expr
| Apply of expr * expr
type defn = LetRec of string * string * expr
View CleanerCode.fs
type AccountKind = Simple | Valuable | MostValuable
type CustomerStatus =
| Unregistered
| Registered of AccountKind * Years:int
let accountFactor kind =
match kind with
| Simple -> 0.9m
| Valuable -> 0.7m
@jdh30
jdh30 / JsonParser.fs
Last active Jun 6, 2020
Simple JSON parser written in F# using active patterns
View JsonParser.fs
type Json =
| Null
| Bool of bool
| Number of float
| String of string
| Array of Json list
| Object of (string * Json) list
type Bracket = Open | Close
@jdh30
jdh30 / FemtoMLParserUsingFParsec.fs
Last active Jun 6, 2020
FemtoML parser in F# using FParsec
View FemtoMLParserUsingFParsec.fs
> let pInt : Parser<_, unit> = puint32 |>> int .>> spaces;;
> let str s = pstring s >>. spaces;;
> let keywords =
"if then else let rec in fun".Split ' '
|> set;;
> let pIdent : Parser<_, unit> =
@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 / ConcestorMap.fs
Created Oct 9, 2017
Persistent Map implemented as a Concestor data structure
View ConcestorMap.fs
// This code implements a persistent dictionary similar to Map except the
// internal representation is a Dictionary + diffs. This offers different
// performance tradeoffs and, in particular, much faster add and remove when
// used linearly.
//
// For more details, see the following articles from the F# Journal:
//
// http://fsharpnews.blogspot.co.uk/2017/10/the-concestor-set.html
// http://fsharpnews.blogspot.co.uk/2017/10/a-simple-concestor-dictionary.html
@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 / ray.jl
Last active Sep 17, 2019
Ray tracer in Julia
View ray.jl
// Translated from the Ray Tracer Language Comparison
// http://www.ffconsultancy.com/languages/ray_tracer/benchmark.html
import Printf
struct Hit
l ::Float64
d ::Array{Float64, 1}
end
@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
You can’t perform that action at this time.