-
-
Save dimitris-papadimitriou-chr/3657a8d11d47c7a3a04f36131ca5fe5a to your computer and use it in GitHub Desktop.
Free Monad as Functor
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
public abstract class Free<T> | |
{ | |
public abstract Free<T1> Map<T1>(Func<T, T1> map); | |
} | |
public class Pure<T> : Free<T> | |
{ | |
private T source; | |
public Pure(T v) => source = v; | |
public override Free<T1> Map<T1>(Func<T, T1> map) => new Pure<T1>(map(source)); | |
} | |
public class Roll<T, F> : Free<T> where F : IFunctor<Free<T>> | |
{ | |
private F source; | |
public Roll(F v) => source = v; | |
public override Free<T1> Map<T1>(Func<T, T1> f) => new Roll<T1, IFunctor<Free<T1>>>(source.Map(x => x.Map(f))); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment