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 / 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] {}

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

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,

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:

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

@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);
}
@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 / 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