Skip to content

Instantly share code, notes, and snippets.

Avatar

Jonathan Harrop jdh30

View GitHub Profile
@jdh30
jdh30 / JsonParser.fs
Last active Jun 11, 2021
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 / 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 / InstallOCaml.sh
Last active Dec 13, 2020
Script to install and initialize OCaml for microservice development (TCP+HTTP+JSON) on an Ubuntu Linux box (e.g. in Amazon's AWS Cloud)
View InstallOCaml.sh
# .NET Core 2.1 with Ubuntu Server 18.04 - Version 1.0 - ami-f4f4cf91
sh <(curl -sL https://raw.githubusercontent.com/ocaml/opam/master/shell/install.sh)
sudo apt-get update
sudo apt-get install make m4 gcc zip aspcud emacs libssl-dev ncurses-dev pkg-config tuareg-mode libffi-dev bubblewrap linux-tools-common linux-tools-aws
opam init
eval $(opam env)
opam install ocamlfind core async async_ssl cohttp yojson ppx_deriving ocp-indent merlin cohttp-async
@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 / 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 / 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