Skip to content

Instantly share code, notes, and snippets.

@fredyr
fredyr / biquad.ml
Created January 4, 2016 11:29
Tiny biquad filter
(*
Tiny Biquad filter implementation in OCaml.
Ported straight from http://www.musicdsp.org/files/biquad.c
*)
type biquad = {
a0 : float;
a1 : float;
a2 : float;
a3 : float;
@fredyr
fredyr / tree.ex
Last active December 8, 2015 20:31
# center a string s on position c
# cent("0", 5) => " 0 "
# cent("000", 5) => " 000 "
# but we ignore right side spaces
cent = fn(s, c) ->
String.duplicate(" ", div(c - String.length(s), 2)) <> s
end
# piping through partials?
# SAAD PUPPY :'(
import math
import numpy
import matplotlib.pyplot as plt
def sine(frequency, length, rate):
length = int(length * rate)
factor = float(frequency) * (math.pi * 2) / rate
return numpy.sin(numpy.arange(length) * factor)
(*
INTRODUCTION TO OCAML
Fredrik Dyrkell
@lexicallyscoped
www.lexicallyscoped.com

Learning OCaml

Similarly to what exists for Haskell, I wanted to make a curated collection of good resources for getting started with OCaml.

Installation and getting started

Follow the installation instructions from the Real World OCaml book available here. This will get you accustomed to

  • Opam, the package manager, which also gives to the possibility of switching between OCaml versions.
  • Utop, a modern interactive toplevel
  • Set up of your enviroment, in particular #use "topfind" so that Utop finds your packages installed by Opam.
#include <stdio.h>
// hook implementation
typedef void (*void_func_t)(void);
typedef struct tHookElem {
unsigned int id; // dispatch id
void_func_t func;
} HookElem;
#define HOOKLIST_STATIC_SIZE 20
@fredyr
fredyr / flatfile.clj
Last active August 9, 2019 16:15
Parsing flat files in Clojure
;; Code for blog article at
;; http://www.lexicallyscoped.com/2015/01/05/parsing-flat-files-in-clojure.html
(ns flatfile.core
(:use [clojure.java.io])
(:import [java.io PushbackReader]))
(defn load-rulebook [file]
(with-open [r (reader file)]
(read (PushbackReader. r))))
import socket
import csv
def load_params():
with open('params.csv', 'rb') as f:
reader = csv.reader(f, delimiter=';', quoting=csv.QUOTE_NONE)
# Drop first two rows (crap+headers)
rows = [row for row in reader if len(row) > 15][2:]
# print rows
defaults = {r[4]: r[15] for r in rows}
@fredyr
fredyr / Vector.purs
Last active January 1, 2016 10:56
Persistent vector in Purescript
-- Playing around with persistent vectors in Purescript
-- Imitiates the interface from Data.Array, but uses Mori.js persistent
-- vector implementation e.g. to avoid copy-on-write etc.
module Data.Vector where
import Data.Maybe
-- LOL I have no idea what I'm doing, but reverse engineering from the
-- JS output, this actually seems to work
foreign import data Mori :: *
{-#LANGUAGE GADTs, KindSignatures, FlexibleInstances, FunctionalDependencies, NoMonomorphismRestriction #-}
-- DOMAIN SPECIFIC LANGUAGES IN HASKELL
-- Fredrik Dyrkell
-- @lexicallyscoped | lexicallyscoped.com