Skip to content

Instantly share code, notes, and snippets.

@dimitris-papadimitriou-chr
Created June 25, 2019 19:32
Show Gist options
  • Save dimitris-papadimitriou-chr/3657a8d11d47c7a3a04f36131ca5fe5a to your computer and use it in GitHub Desktop.
Save dimitris-papadimitriou-chr/3657a8d11d47c7a3a04f36131ca5fe5a to your computer and use it in GitHub Desktop.
Free Monad as Functor
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