Skip to content

Instantly share code, notes, and snippets.

Keybase proof

I hereby claim:

  • I am jedahu on github.
  • I am jedahu (https://keybase.io/jedahu) on keybase.
  • I have a public key ASAEDZtMXX9XenWwUEArDrIcIWeR7Zpc2RIfw-UPq5ILjgo

To claim this, I am signing this object:

@jedahu
jedahu / enum_example.js
Created November 13, 2012 10:22
sweet.js simple enum macro
macro $enum {
case $name:ident {$val:ident (,) ...} => {
var $name = {$($val: null) (,) ...};
for (var k in $name) {
$name[k] = k;
}
Object.freeze($name);
}
}
@jedahu
jedahu / 1Valid.cs
Last active February 5, 2019 14:11
C# type-safe data validation
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
[ContractClass(typeof(IValidatorContract<,>))]
public interface IValidator<E, in A>
{
IEnumerable<E> Validate(A a);
}
@jedahu
jedahu / Maybe.scala
Last active August 25, 2016 06:56
Get Typed (Scala)
package gettyped
sealed abstract class Maybe[A] {
def fold[B](nothing: => B, just: A => B): B = this match {
case Nothing() => nothing
case Just(a) => just(a)
}
}
private final case class Nothing[A]() extends Maybe[A] {}
@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

@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 / 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 / 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"
;;

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.

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