Skip to content

Instantly share code, notes, and snippets.

@louthy
Created February 13, 2020 16:23
Show Gist options
  • Save louthy/6cb6c2611569bd29de48b75385c4ac57 to your computer and use it in GitHub Desktop.
Save louthy/6cb6c2611569bd29de48b75385c4ac57 to your computer and use it in GitHub Desktop.
public partial struct Subsystem<A>
{
readonly LanguageExt.Reader<TestBed.IO, A> computation;
internal Subsystem(LanguageExt.Reader<TestBed.IO, A> comp) => computation = comp;
public static Subsystem<A> Pure(A value) => new Subsystem<A>(Prelude.Reader<TestBed.IO, A>(value));
public static Subsystem<A> Fail(string message) => new Subsystem<A>(Prelude.ReaderFail<TestBed.IO, A>(message));
public static Subsystem<A> Fail(System.Exception exception) => new Subsystem<A>(Prelude.ReaderFail<TestBed.IO, A>(exception));
public static Subsystem<A> Fail(string message, System.Exception exception) => new Subsystem<A>(Prelude.ReaderFail<TestBed.IO, A>(message, exception));
public Subsystem<B> Map<B>(System.Func<A, B> f) => Bind(a => Subsystem<B>.Pure(f(a)));
public Subsystem<B> Select<B>(System.Func<A, B> f) => Map(f);
public Subsystem<B> Bind<B>(System.Func<A, Subsystem<B>> f) => new Subsystem<B>(computation.Bind(a => f(a).computation));
public Subsystem<B> SelectMany<B>(System.Func<A, Subsystem<B>> f) => Bind(f);
public Subsystem<C> SelectMany<B, C>(System.Func<A, Subsystem<B>> bind, System.Func<A, B, C> project) => Bind(a => bind(a).Map(b => project(a, b)));
public ReaderResult<A> Run(TestBed.IO env) => computation.Run(env);
public Subsystem<A> Do(System.Action<A> f) => new Subsystem<A>(computation.Do(f));
public Subsystem<A> Strict() => new Subsystem<A>(computation.Strict());
public Seq<A> ToSeq(TestBed.IO env) => computation.ToSeq(env);
public Subsystem<LanguageExt.Unit> Iter(System.Action<A> f) => new Subsystem<LanguageExt.Unit>(computation.Iter(f));
public System.Func<TestBed.IO, S> Fold<S>(S state, System.Func<S, A, S> f)
{
var self = this;
return env => self.computation.Fold(state, f).Run(env).IfFail(state);
}
public System.Func<TestBed.IO, bool> ForAll(System.Func<A, bool> f)
{
var self = this;
return env => self.computation.ForAll(f).Run(env).IfFail(false);
}
public System.Func<TestBed.IO, bool> Exists(System.Func<A, bool> f)
{
var self = this;
return env => self.computation.Exists(f).Run(env).IfFail(false);
}
public Subsystem<A> Local(System.Func<TestBed.IO, TestBed.IO> f) => new Subsystem<A>(LanguageExt.Prelude.local(computation, f));
}
public static partial class Subsystem
{
public static Subsystem<A> Pure<A>(A value) => Subsystem<A>.Pure(value);
public static Subsystem<A> Fail<A>(string message) => Subsystem<A>.Fail(message);
public static Subsystem<A> Fail<A>(System.Exception exception) => Subsystem<A>.Fail(exception);
public static Subsystem<A> Fail<A>(string message, System.Exception exception) => Subsystem<A>.Fail(message, exception);
public static Subsystem<A> asks<A>(System.Func<TestBed.IO, A> f) => new Subsystem<A>(__env => ReaderResult<A>.New(f(__env)));
public static readonly Subsystem<TestBed.IO> ask = new Subsystem<TestBed.IO>(__env => ReaderResult<TestBed.IO>.New(__env));
public static Subsystem<A> local<A>(Subsystem<A> ma, System.Func<TestBed.IO, TestBed.IO> f) => ma.Local(f);
public static Subsystem<LanguageExt.Seq<string>> ReadAllLines(string fileName) => ask.Map(__env => __env.ReadAllLines(fileName));
public static Subsystem<LanguageExt.Unit> WriteAllLines(string fileName, LanguageExt.Seq<string> lines) => ask.Map(__env => __env.WriteAllLines(fileName, lines));
public static Subsystem<TestBed.Person> ReadFromDB() => ask.Map(__env => __env.ReadFromDB());
public static Subsystem<int> Zero => ask.Map(__env => __env.Zero);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment