Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@louthy
Created February 27, 2020 22:06
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 louthy/574b70bda8fb4fd4cd922e7fb840ffb6 to your computer and use it in GitHub Desktop.
Save louthy/574b70bda8fb4fd4cd922e7fb840ffb6 to your computer and use it in GitHub Desktop.
[System.Serializable]
public sealed class Pure<T> : FreeIO<T>, System.IEquatable<Pure<T>>, System.IComparable<Pure<T>>, System.IComparable
{
public readonly T Value;
public Pure(T Value)
{
this.Value = Value;
}
public static Pure<T> New(T Value) => new Pure<T>(Value);
public void Deconstruct(out T Value)
{
Value = this.Value;
}
private Pure(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
{
this.Value = (T)info.GetValue("value", typeof(T));
}
public void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
{
info.AddValue("value", this.Value);
}
public static bool operator ==(Pure<T> x, Pure<T> y) => ReferenceEquals(x, y) || (x?.Equals(y) ?? false);
public static bool operator !=(Pure<T> x, Pure<T> y) => !(x == y);
public static bool operator>(Pure<T> x, Pure<T> y) => !ReferenceEquals(x, y) && !ReferenceEquals(x, null) && x.CompareTo(y) > 0;
public static bool operator <(Pure<T> x, Pure<T> y) => !ReferenceEquals(x, y) && (ReferenceEquals(x, null) && !ReferenceEquals(y, null) || x.CompareTo(y) < 0);
public static bool operator >=(Pure<T> x, Pure<T> y) => ReferenceEquals(x, y) || (!ReferenceEquals(x, null) && x.CompareTo(y) >= 0);
public static bool operator <=(Pure<T> x, Pure<T> y) => ReferenceEquals(x, y) || (ReferenceEquals(x, null) && !ReferenceEquals(y, null) || x.CompareTo(y) <= 0);
public bool Equals(Pure<T> other)
{
if (LanguageExt.Prelude.isnull(other))
return false;
if (!default(LanguageExt.ClassInstances.EqDefault<T>).Equals(this.Value, other.Value))
return false;
return true;
}
public override bool Equals(object obj) => obj is Pure<T> tobj && Equals(tobj);
public int CompareTo(object obj) => obj is FreeIO<T> p ? CompareTo(p) : 1;
public int CompareTo(Pure<T> other)
{
if (LanguageExt.Prelude.isnull(other))
return 1;
int cmp = 0;
cmp = default(LanguageExt.ClassInstances.OrdDefault<T>).Compare(this.Value, other.Value);
if (cmp != 0)
return cmp;
return 0;
}
public override int GetHashCode()
{
const int fnvOffsetBasis = -2128831035;
const int fnvPrime = 16777619;
int state = fnvOffsetBasis;
unchecked
{
state = (default(LanguageExt.ClassInstances.HashableDefault<T>).GetHashCode(this.Value) ^ state) * fnvPrime;
}
return state;
}
public override string ToString()
{
var sb = new System.Text.StringBuilder();
sb.Append("Pure(");
sb.Append(LanguageExt.Prelude.isnull(Value) ? $"Value: [null]" : $"Value: {Value}");
sb.Append(")");
return sb.ToString();
}
T FreeIO<T>.Pure(T value) => throw new System.NotSupportedException();
T FreeIO<T>.Fail(Error error) => throw new System.NotSupportedException();
string FreeIO<T>.ReadAllText(string path) => throw new System.NotSupportedException();
Unit FreeIO<T>.WriteAllText(string path, string text) => throw new System.NotSupportedException();
}
[System.Serializable]
public sealed class Fail<T> : FreeIO<T>, System.IEquatable<Fail<T>>, System.IComparable<Fail<T>>, System.IComparable
{
public readonly Error Error;
public Fail(Error Error)
{
this.Error = Error;
}
public static Fail<T> New(Error Error) => new Fail<T>(Error);
public void Deconstruct(out Error Error)
{
Error = this.Error;
}
private Fail(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
{
this.Error = (Error)info.GetValue("error", typeof(Error));
}
public void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
{
info.AddValue("error", this.Error);
}
public static bool operator ==(Fail<T> x, Fail<T> y) => ReferenceEquals(x, y) || (x?.Equals(y) ?? false);
public static bool operator !=(Fail<T> x, Fail<T> y) => !(x == y);
public static bool operator>(Fail<T> x, Fail<T> y) => !ReferenceEquals(x, y) && !ReferenceEquals(x, null) && x.CompareTo(y) > 0;
public static bool operator <(Fail<T> x, Fail<T> y) => !ReferenceEquals(x, y) && (ReferenceEquals(x, null) && !ReferenceEquals(y, null) || x.CompareTo(y) < 0);
public static bool operator >=(Fail<T> x, Fail<T> y) => ReferenceEquals(x, y) || (!ReferenceEquals(x, null) && x.CompareTo(y) >= 0);
public static bool operator <=(Fail<T> x, Fail<T> y) => ReferenceEquals(x, y) || (ReferenceEquals(x, null) && !ReferenceEquals(y, null) || x.CompareTo(y) <= 0);
public bool Equals(Fail<T> other)
{
if (LanguageExt.Prelude.isnull(other))
return false;
if (!default(LanguageExt.ClassInstances.EqDefault<Error>).Equals(this.Error, other.Error))
return false;
return true;
}
public override bool Equals(object obj) => obj is Fail<T> tobj && Equals(tobj);
public int CompareTo(object obj) => obj is FreeIO<T> p ? CompareTo(p) : 1;
public int CompareTo(Fail<T> other)
{
if (LanguageExt.Prelude.isnull(other))
return 1;
int cmp = 0;
cmp = default(LanguageExt.ClassInstances.OrdDefault<Error>).Compare(this.Error, other.Error);
if (cmp != 0)
return cmp;
return 0;
}
public override int GetHashCode()
{
const int fnvOffsetBasis = -2128831035;
const int fnvPrime = 16777619;
int state = fnvOffsetBasis;
unchecked
{
state = (default(LanguageExt.ClassInstances.HashableDefault<Error>).GetHashCode(this.Error) ^ state) * fnvPrime;
}
return state;
}
public override string ToString()
{
var sb = new System.Text.StringBuilder();
sb.Append("Fail(");
sb.Append(LanguageExt.Prelude.isnull(Error) ? $"Error: [null]" : $"Error: {Error}");
sb.Append(")");
return sb.ToString();
}
T FreeIO<T>.Pure(T value) => throw new System.NotSupportedException();
T FreeIO<T>.Fail(Error error) => throw new System.NotSupportedException();
string FreeIO<T>.ReadAllText(string path) => throw new System.NotSupportedException();
Unit FreeIO<T>.WriteAllText(string path, string text) => throw new System.NotSupportedException();
}
[System.Serializable]
public sealed class ReadAllText<T> : FreeIO<T>, System.IEquatable<ReadAllText<T>>, System.IComparable<ReadAllText<T>>, System.IComparable
{
public readonly string Path;
public readonly System.Func<string, FreeIO<T>> Next;
public ReadAllText(string Path, System.Func<string, FreeIO<T>> Next)
{
this.Path = Path;
this.Next = Next;
}
public static ReadAllText<T> New(string Path, System.Func<string, FreeIO<T>> Next) => new ReadAllText<T>(Path, Next);
public void Deconstruct(out string Path, out System.Func<string, FreeIO<T>> Next)
{
Path = this.Path;
Next = this.Next;
}
private ReadAllText(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
{
this.Path = (string)info.GetValue("path", typeof(string));
this.Next = (System.Func<string, FreeIO<T>>)info.GetValue("next", typeof(System.Func<string, FreeIO<T>>));
}
public void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
{
info.AddValue("path", this.Path);
info.AddValue("next", this.Next);
}
public static bool operator ==(ReadAllText<T> x, ReadAllText<T> y) => ReferenceEquals(x, y) || (x?.Equals(y) ?? false);
public static bool operator !=(ReadAllText<T> x, ReadAllText<T> y) => !(x == y);
public static bool operator>(ReadAllText<T> x, ReadAllText<T> y) => !ReferenceEquals(x, y) && !ReferenceEquals(x, null) && x.CompareTo(y) > 0;
public static bool operator <(ReadAllText<T> x, ReadAllText<T> y) => !ReferenceEquals(x, y) && (ReferenceEquals(x, null) && !ReferenceEquals(y, null) || x.CompareTo(y) < 0);
public static bool operator >=(ReadAllText<T> x, ReadAllText<T> y) => ReferenceEquals(x, y) || (!ReferenceEquals(x, null) && x.CompareTo(y) >= 0);
public static bool operator <=(ReadAllText<T> x, ReadAllText<T> y) => ReferenceEquals(x, y) || (ReferenceEquals(x, null) && !ReferenceEquals(y, null) || x.CompareTo(y) <= 0);
public bool Equals(ReadAllText<T> other)
{
if (LanguageExt.Prelude.isnull(other))
return false;
if (!default(LanguageExt.ClassInstances.EqDefault<string>).Equals(this.Path, other.Path))
return false;
if (!default(LanguageExt.ClassInstances.EqDefault<System.Func<string, FreeIO<T>>>).Equals(this.Next, other.Next))
return false;
return true;
}
public override bool Equals(object obj) => obj is ReadAllText<T> tobj && Equals(tobj);
public int CompareTo(object obj) => obj is FreeIO<T> p ? CompareTo(p) : 1;
public int CompareTo(ReadAllText<T> other)
{
if (LanguageExt.Prelude.isnull(other))
return 1;
int cmp = 0;
cmp = default(LanguageExt.ClassInstances.OrdDefault<string>).Compare(this.Path, other.Path);
if (cmp != 0)
return cmp;
cmp = default(LanguageExt.ClassInstances.OrdDefault<System.Func<string, FreeIO<T>>>).Compare(this.Next, other.Next);
if (cmp != 0)
return cmp;
return 0;
}
public override int GetHashCode()
{
const int fnvOffsetBasis = -2128831035;
const int fnvPrime = 16777619;
int state = fnvOffsetBasis;
unchecked
{
state = (default(LanguageExt.ClassInstances.HashableDefault<string>).GetHashCode(this.Path) ^ state) * fnvPrime;
state = (default(LanguageExt.ClassInstances.HashableDefault<System.Func<string, FreeIO<T>>>).GetHashCode(this.Next) ^ state) * fnvPrime;
}
return state;
}
public override string ToString()
{
var sb = new System.Text.StringBuilder();
sb.Append("ReadAllText(");
sb.Append(LanguageExt.Prelude.isnull(Path) ? $"Path: [null]" : $"Path: {Path}");
sb.Append($", ");
sb.Append(LanguageExt.Prelude.isnull(Next) ? $"Next: [null]" : $"Next: {Next}");
sb.Append(")");
return sb.ToString();
}
T FreeIO<T>.Pure(T value) => throw new System.NotSupportedException();
T FreeIO<T>.Fail(Error error) => throw new System.NotSupportedException();
string FreeIO<T>.ReadAllText(string path) => throw new System.NotSupportedException();
Unit FreeIO<T>.WriteAllText(string path, string text) => throw new System.NotSupportedException();
}
[System.Serializable]
public sealed class WriteAllText<T> : FreeIO<T>, System.IEquatable<WriteAllText<T>>, System.IComparable<WriteAllText<T>>, System.IComparable
{
public readonly string Path;
public readonly string Text;
public readonly System.Func<Unit, FreeIO<T>> Next;
public WriteAllText(string Path, string Text, System.Func<Unit, FreeIO<T>> Next)
{
this.Path = Path;
this.Text = Text;
this.Next = Next;
}
public static WriteAllText<T> New(string Path, string Text, System.Func<Unit, FreeIO<T>> Next) => new WriteAllText<T>(Path, Text, Next);
public void Deconstruct(out string Path, out string Text, out System.Func<Unit, FreeIO<T>> Next)
{
Path = this.Path;
Text = this.Text;
Next = this.Next;
}
private WriteAllText(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
{
this.Path = (string)info.GetValue("path", typeof(string));
this.Text = (string)info.GetValue("text", typeof(string));
this.Next = (System.Func<Unit, FreeIO<T>>)info.GetValue("next", typeof(System.Func<Unit, FreeIO<T>>));
}
public void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
{
info.AddValue("path", this.Path);
info.AddValue("text", this.Text);
info.AddValue("next", this.Next);
}
public static bool operator ==(WriteAllText<T> x, WriteAllText<T> y) => ReferenceEquals(x, y) || (x?.Equals(y) ?? false);
public static bool operator !=(WriteAllText<T> x, WriteAllText<T> y) => !(x == y);
public static bool operator>(WriteAllText<T> x, WriteAllText<T> y) => !ReferenceEquals(x, y) && !ReferenceEquals(x, null) && x.CompareTo(y) > 0;
public static bool operator <(WriteAllText<T> x, WriteAllText<T> y) => !ReferenceEquals(x, y) && (ReferenceEquals(x, null) && !ReferenceEquals(y, null) || x.CompareTo(y) < 0);
public static bool operator >=(WriteAllText<T> x, WriteAllText<T> y) => ReferenceEquals(x, y) || (!ReferenceEquals(x, null) && x.CompareTo(y) >= 0);
public static bool operator <=(WriteAllText<T> x, WriteAllText<T> y) => ReferenceEquals(x, y) || (ReferenceEquals(x, null) && !ReferenceEquals(y, null) || x.CompareTo(y) <= 0);
public bool Equals(WriteAllText<T> other)
{
if (LanguageExt.Prelude.isnull(other))
return false;
if (!default(LanguageExt.ClassInstances.EqDefault<string>).Equals(this.Path, other.Path))
return false;
if (!default(LanguageExt.ClassInstances.EqDefault<string>).Equals(this.Text, other.Text))
return false;
if (!default(LanguageExt.ClassInstances.EqDefault<System.Func<Unit, FreeIO<T>>>).Equals(this.Next, other.Next))
return false;
return true;
}
public override bool Equals(object obj) => obj is WriteAllText<T> tobj && Equals(tobj);
public int CompareTo(object obj) => obj is FreeIO<T> p ? CompareTo(p) : 1;
public int CompareTo(WriteAllText<T> other)
{
if (LanguageExt.Prelude.isnull(other))
return 1;
int cmp = 0;
cmp = default(LanguageExt.ClassInstances.OrdDefault<string>).Compare(this.Path, other.Path);
if (cmp != 0)
return cmp;
cmp = default(LanguageExt.ClassInstances.OrdDefault<string>).Compare(this.Text, other.Text);
if (cmp != 0)
return cmp;
cmp = default(LanguageExt.ClassInstances.OrdDefault<System.Func<Unit, FreeIO<T>>>).Compare(this.Next, other.Next);
if (cmp != 0)
return cmp;
return 0;
}
public override int GetHashCode()
{
const int fnvOffsetBasis = -2128831035;
const int fnvPrime = 16777619;
int state = fnvOffsetBasis;
unchecked
{
state = (default(LanguageExt.ClassInstances.HashableDefault<string>).GetHashCode(this.Path) ^ state) * fnvPrime;
state = (default(LanguageExt.ClassInstances.HashableDefault<string>).GetHashCode(this.Text) ^ state) * fnvPrime;
state = (default(LanguageExt.ClassInstances.HashableDefault<System.Func<Unit, FreeIO<T>>>).GetHashCode(this.Next) ^ state) * fnvPrime;
}
return state;
}
public override string ToString()
{
var sb = new System.Text.StringBuilder();
sb.Append("WriteAllText(");
sb.Append(LanguageExt.Prelude.isnull(Path) ? $"Path: [null]" : $"Path: {Path}");
sb.Append($", ");
sb.Append(LanguageExt.Prelude.isnull(Text) ? $"Text: [null]" : $"Text: {Text}");
sb.Append($", ");
sb.Append(LanguageExt.Prelude.isnull(Next) ? $"Next: [null]" : $"Next: {Next}");
sb.Append(")");
return sb.ToString();
}
T FreeIO<T>.Pure(T value) => throw new System.NotSupportedException();
T FreeIO<T>.Fail(Error error) => throw new System.NotSupportedException();
string FreeIO<T>.ReadAllText(string path) => throw new System.NotSupportedException();
Unit FreeIO<T>.WriteAllText(string path, string text) => throw new System.NotSupportedException();
}
public static partial class FreeIO
{
public static FreeIO<T> Pure<T>(T value) => new Pure<T>(value);
public static FreeIO<T> Fail<T>(Error error) => new Fail<T>(error);
public static FreeIO<string> ReadAllText(string path) => new ReadAllText<string>(path, Pure);
public static FreeIO<Unit> WriteAllText(string path, string text) => new WriteAllText<Unit>(path, text, Pure);
public static FreeIO<U> Bind<T, U>(this FreeIO<T> ma, System.Func<T, FreeIO<U>> f) => ma switch
{
Pure<T> v => f(v.Value),
Fail<T> v => new Fail<U>(v.Error),
ReadAllText<T> v => new ReadAllText<U>(v.Path, n => v.Next(n).Map(f).Bind(LanguageExt.Prelude.identity)),
WriteAllText<T> v => new WriteAllText<U>(v.Path, v.Text, n => v.Next(n).Map(f).Bind(LanguageExt.Prelude.identity)),
_ => throw new System.NotSupportedException()
};
public static FreeIO<U> Map<T, U>(this FreeIO<T> ma, System.Func<T, U> f) => ma switch
{
Pure<T> v => new Pure<U>(f(v.Value)),
Fail<T> v => new Fail<U>(v.Error),
ReadAllText<T> v => new ReadAllText<U>(v.Path, n => v.Next(n).Map(f)),
WriteAllText<T> v => new WriteAllText<U>(v.Path, v.Text, n => v.Next(n).Map(f)),
_ => throw new System.NotSupportedException()
};
public static FreeIO<U> Select<T, U>(this FreeIO<T> ma, System.Func<T, U> f) => Map(ma, f);
public static FreeIO<U> SelectMany<T, U>(this FreeIO<T> ma, System.Func<T, FreeIO<U>> f) => Bind(ma, f);
public static FreeIO<V> SelectMany<T, U, V>(this FreeIO<T> ma, System.Func<T, FreeIO<U>> bind, System.Func<T, U, V> project) => Bind(ma, a => Map(bind(a), b => project(a, b)));
public static FreeIO<T> Flatten<T>(this FreeIO<FreeIO<T>> mma) => Bind(mma, LanguageExt.Prelude.identity);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment