Skip to content

Instantly share code, notes, and snippets.

@ToJans
Last active October 11, 2016 09:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ToJans/3add240f8c5948e5753263df721a230c to your computer and use it in GitHub Desktop.
Save ToJans/3add240f8c5948e5753263df721a230c to your computer and use it in GitHub Desktop.
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