Skip to content

Instantly share code, notes, and snippets.

@jedahu
jedahu / 1ILanguage.cs
Last active August 29, 2015 13:57
Safe strings
using System.Diagnostics.Contracts;
[ContractClass(typeof(ILanguageContract<>))]
public interface ILanguage<A>
{
A Literal(string s);
string Escape(string s);
string Render(A a);
}

Welcome to the inaugural Functional Friday post. I intend to explore, as far as is possible in C#, what functional programming brings to the table: types, laws, and equational reasoning. Example code is in the C# project at the root of this repository.

First up: parametricity and type classes.

Parametricity

The previous post glossed over some features of C# that make functional programming difficult. Here they are explicitly addressed. Let’s call them warts. These warts make it difficult to reason about code and thus to write correct code.

Wart 1: null

Tony Hoare invented the null pointer and later apologised for doing so:

Objects conflate construction, implementation hiding, (subtype) polymorphism, and the grouping of similar functionality. Algebraic Data Types (ADTs) help untangle these concepts.

In algebraic notation

Using the following notation:

  • 1 for the unit type,

Null pointer exceptions can be prevented at compile time using a simple data type borrowed from Haskell.

data Maybe a = Nothing | Just a

The Maybe type

The type class encoding in my previous post on the subject is a bit rubbish:

  • type class instances are classes, which have a non-zero creation cost;
  • they do not have self-type parameters, so are not necessarily distinguishable at the type level; and
  • they do not provide for derived operations.

The Maybe type I described could do with some type class instances, so let’s try out a new encoding.

@jedahu
jedahu / srepl.sh
Created January 26, 2012 11:27
browser repl for clojurescript projects
#!/bin/sh
PORT=9000
OUT_DIR='out'
while getopts ":f:p:d:" opt; do
case $opt in
f)
HTML="$OPTARG"
;;
@jedahu
jedahu / pre-commit
Created January 26, 2012 23:10
git hooks for clojurescript projects
#!/bin/sh
git diff-index --check --cached $against -- && \
rm -rf out && \
git stash save --keep-index 'pre-commit unstaged changes' && \
lein clojurescript fresh test && \
git stash pop
@jedahu
jedahu / es5_ng_it.js
Last active December 13, 2015 20:58
Replacement async 'it' for testing AngularJs with Mocha.
function ngIt($injector) {
return function(text, fn) {
it(text, function(done) {
var $rootScope = $injector.get('$rootScope')
, fin
, finished = function(err) {
fin = true;
done(err);
};
fn(finished);
@jedahu
jedahu / multichannel-play-record-jack.md
Last active December 14, 2015 04:29
Multi-channel simultaneous play and record with jack.

The brief

Create an executable (recapture) that plays a signal through 24 audio ports and simultaneously records from another 24 ports. These ports come from three daisy chained Presonus FP10’s connected by firewire to a linux box. This hardware is accessed by the ffado jack drivers.

The solution