Skip to content

Instantly share code, notes, and snippets.

Last active Oct 11, 2016
What would you like to do?
Functor, applicative and monad interfaces
// In C# we can't enforce generic classes in interfaces, so we can't use interfaces to define the contract.
// I can only give examples of the minimal code it needs to implement.
class FunctorImpl<T> {
FunctorImpl<T> Pure(T val);
FunctorImpl<S> Apply<S>(Func<T, S> fn);
// Requirement: Pure(val).Apply(x=>x) == Pure(val)
class ApplicativeImpl<T> where {
ApplicativeImpl<T> Pure(T val);
ApplicativeImpl<S> Apply<S>(Func<ApplicativeImpl<T>, ApplicativeImpl<S>> fn)
static Func<ApplicativeImpl<T>, ApplicativeImpl<S>> Lift<S>(Func<T, S> fn);
// Requirement: Pure(val).Apply(Lift(x => x)) == Pure(val)
class MonadImpl<T> {
MonadImpl<T> Pure(T val);
MonadImpl<S> Apply<S>(Func<T, MonadImpl<S>> fn);
static Func<S, MonadImpl<T>> Lift<S>(Func<T, MonadImpl<S>> fn);
// Requirement: Pure(val).Apply(Lift(x => x)) == Pure(val)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment