Skip to content

Instantly share code, notes, and snippets.

{-# OPTIONS_GHC -fno-warn-orphans #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverloadedStrings #-}
import Control.Monad (void)
import Ivory.Language
import Ivory.Compile.C.CmdlineFrontend
import Ivory.Compile.C.Modules
-- Fixed point registers
data Rx = R0 | R1 | R2 | R3 | R4 | R5 | R6 | R7 deriving (Show, Eq, Ord)
-- Floating point registers
data Fx = F0 | F1 | F2 | F3 | F4 | F5 | F6 | F7 deriving (Show, Eq, Ord)
data Expr :: * -> * where
AddI :: Rx -> Rx -> Expr Integer
SubI :: Rx -> Rx -> Expr Integer
AddF :: Fx -> Fx -> Expr Float
SubF :: Fx -> Fx -> Expr Float
{-#LANGUAGE GADTs, KindSignatures, FlexibleInstances, FunctionalDependencies, NoMonomorphismRestriction #-}
-- DOMAIN SPECIFIC LANGUAGES IN HASKELL
-- Fredrik Dyrkell
-- @lexicallyscoped | lexicallyscoped.com
@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 :: *
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 / 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))))
#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

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.
(*
INTRODUCTION TO OCAML
Fredrik Dyrkell
@lexicallyscoped
www.lexicallyscoped.com
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)