Last active
October 11, 2016 09:50
-
-
Save ToJans/3add240f8c5948e5753263df721a230c to your computer and use it in GitHub Desktop.
Functor, applicative and monad interfaces
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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