Skip to content

Instantly share code, notes, and snippets.

@louthy
Created February 13, 2020 16:26
Show Gist options
  • Save louthy/1c308a5772d0cc8248de30ef3db8a53b to your computer and use it in GitHub Desktop.
Save louthy/1c308a5772d0cc8248de30ef3db8a53b to your computer and use it in GitHub Desktop.
[System.Serializable]
public sealed class Rectangle<NumA, A> : _ShapeBase<NumA, A>, System.IEquatable<Rectangle<NumA, A>>, System.IComparable<Rectangle<NumA, A>>, System.IComparable where NumA : struct, Num<A>
{
public readonly A Width;
public readonly A Length;
public override int @Tag => 1;
public Rectangle(A Width, A Length)
{
this.Width = Width;
this.Length = Length;
}
public void Deconstruct(out A Width, out A Length)
{
Width = this.Width;
Length = this.Length;
}
private Rectangle(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
{
this.Width = (A)info.GetValue("width", typeof(A));
this.Length = (A)info.GetValue("length", typeof(A));
}
public void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
{
info.AddValue("width", this.Width);
info.AddValue("length", this.Length);
}
public static bool operator ==(Rectangle<NumA, A> x, Rectangle<NumA, A> y) => ReferenceEquals(x, y) || (x?.Equals(y) ?? false);
public static bool operator !=(Rectangle<NumA, A> x, Rectangle<NumA, A> y) => !(x == y);
public static bool operator>(Rectangle<NumA, A> x, Rectangle<NumA, A> y) => !ReferenceEquals(x, y) && !ReferenceEquals(x, null) && x.CompareTo(y) > 0;
public static bool operator <(Rectangle<NumA, A> x, Rectangle<NumA, A> y) => !ReferenceEquals(x, y) && (ReferenceEquals(x, null) && !ReferenceEquals(y, null) || x.CompareTo(y) < 0);
public static bool operator >=(Rectangle<NumA, A> x, Rectangle<NumA, A> y) => ReferenceEquals(x, y) || (!ReferenceEquals(x, null) && x.CompareTo(y) >= 0);
public static bool operator <=(Rectangle<NumA, A> x, Rectangle<NumA, A> y) => ReferenceEquals(x, y) || (ReferenceEquals(x, null) && !ReferenceEquals(y, null) || x.CompareTo(y) <= 0);
public bool Equals(Rectangle<NumA, A> other)
{
if (LanguageExt.Prelude.isnull(other))
return false;
if (!default(LanguageExt.ClassInstances.EqDefault<A>).Equals(this.Width, other.Width))
return false;
if (!default(LanguageExt.ClassInstances.EqDefault<A>).Equals(this.Length, other.Length))
return false;
return true;
}
public override bool Equals(object obj) => obj is Rectangle<NumA, A> tobj && Equals(tobj);
public override bool Equals(Shape<NumA, A> obj) => obj is Rectangle<NumA, A> tobj && Equals(tobj);
public override int CompareTo(object obj) => obj is Shape<NumA, A> p ? CompareTo(p) : 1;
public override int CompareTo(Shape<NumA, A> obj) => obj is Rectangle<NumA, A> tobj ? CompareTo(tobj) : obj is null ? 1 : @Tag.CompareTo(obj.@Tag);
public int CompareTo(Rectangle<NumA, A> other)
{
if (LanguageExt.Prelude.isnull(other))
return 1;
int cmp = 0;
cmp = default(LanguageExt.ClassInstances.OrdDefault<A>).Compare(this.Width, other.Width);
if (cmp != 0)
return cmp;
cmp = default(LanguageExt.ClassInstances.OrdDefault<A>).Compare(this.Length, other.Length);
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.EqDefault<A>).GetHashCode(this.Width) ^ state) * fnvPrime;
state = (default(LanguageExt.ClassInstances.EqDefault<A>).GetHashCode(this.Length) ^ state) * fnvPrime;
}
return state;
}
public override string ToString()
{
var sb = new System.Text.StringBuilder();
sb.Append("Rectangle(");
sb.Append(LanguageExt.Prelude.isnull(width) ? $"width: [null]" : $"width: {width}");
sb.Append($", ");
sb.Append(LanguageExt.Prelude.isnull(length) ? $"length: [null]" : $"length: {length}");
sb.Append(")");
return sb.ToString();
}
public Rectangle<NumA, A> With(A Width = default(A), A Length = default(A)) => new Rectangle<NumA, A>(Width ?? this.Width, Length ?? this.Length);
public static Lens<Rectangle<NumA, A>, A> width => __LensFields.width;
public static Lens<Rectangle<NumA, A>, A> length => __LensFields.length;
static class __LensFields
{
public static readonly Lens<Rectangle<NumA, A>, A> width = Lens<Rectangle<NumA, A>, A>.New(_x => _x.Width, _x => _y => _y.With(Width: _x));
public static readonly Lens<Rectangle<NumA, A>, A> length = Lens<Rectangle<NumA, A>, A>.New(_x => _x.Length, _x => _y => _y.With(Length: _x));
}
}
[System.Serializable]
public sealed class Circle<NumA, A> : _ShapeBase<NumA, A>, System.IEquatable<Circle<NumA, A>>, System.IComparable<Circle<NumA, A>>, System.IComparable where NumA : struct, Num<A>
{
public readonly A Radius;
public override int @Tag => 2;
public Circle(A Radius)
{
this.Radius = Radius;
}
public void Deconstruct(out A Radius)
{
Radius = this.Radius;
}
private Circle(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
{
this.Radius = (A)info.GetValue("radius", typeof(A));
}
public void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
{
info.AddValue("radius", this.Radius);
}
public static bool operator ==(Circle<NumA, A> x, Circle<NumA, A> y) => ReferenceEquals(x, y) || (x?.Equals(y) ?? false);
public static bool operator !=(Circle<NumA, A> x, Circle<NumA, A> y) => !(x == y);
public static bool operator>(Circle<NumA, A> x, Circle<NumA, A> y) => !ReferenceEquals(x, y) && !ReferenceEquals(x, null) && x.CompareTo(y) > 0;
public static bool operator <(Circle<NumA, A> x, Circle<NumA, A> y) => !ReferenceEquals(x, y) && (ReferenceEquals(x, null) && !ReferenceEquals(y, null) || x.CompareTo(y) < 0);
public static bool operator >=(Circle<NumA, A> x, Circle<NumA, A> y) => ReferenceEquals(x, y) || (!ReferenceEquals(x, null) && x.CompareTo(y) >= 0);
public static bool operator <=(Circle<NumA, A> x, Circle<NumA, A> y) => ReferenceEquals(x, y) || (ReferenceEquals(x, null) && !ReferenceEquals(y, null) || x.CompareTo(y) <= 0);
public bool Equals(Circle<NumA, A> other)
{
if (LanguageExt.Prelude.isnull(other))
return false;
if (!default(LanguageExt.ClassInstances.EqDefault<A>).Equals(this.Radius, other.Radius))
return false;
return true;
}
public override bool Equals(object obj) => obj is Circle<NumA, A> tobj && Equals(tobj);
public override bool Equals(Shape<NumA, A> obj) => obj is Circle<NumA, A> tobj && Equals(tobj);
public override int CompareTo(object obj) => obj is Shape<NumA, A> p ? CompareTo(p) : 1;
public override int CompareTo(Shape<NumA, A> obj) => obj is Circle<NumA, A> tobj ? CompareTo(tobj) : obj is null ? 1 : @Tag.CompareTo(obj.@Tag);
public int CompareTo(Circle<NumA, A> other)
{
if (LanguageExt.Prelude.isnull(other))
return 1;
int cmp = 0;
cmp = default(LanguageExt.ClassInstances.OrdDefault<A>).Compare(this.Radius, other.Radius);
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.EqDefault<A>).GetHashCode(this.Radius) ^ state) * fnvPrime;
}
return state;
}
public override string ToString()
{
var sb = new System.Text.StringBuilder();
sb.Append("Circle(");
sb.Append(LanguageExt.Prelude.isnull(radius) ? $"radius: [null]" : $"radius: {radius}");
sb.Append(")");
return sb.ToString();
}
public Circle<NumA, A> With(A Radius = default(A)) => new Circle<NumA, A>(Radius ?? this.Radius);
public static Lens<Circle<NumA, A>, A> radius => __LensFields.radius;
static class __LensFields
{
public static readonly Lens<Circle<NumA, A>, A> radius = Lens<Circle<NumA, A>, A>.New(_x => _x.Radius, _x => _y => _y.With(Radius: _x));
}
}
[System.Serializable]
public sealed class Prism<NumA, A> : _ShapeBase<NumA, A>, System.IEquatable<Prism<NumA, A>>, System.IComparable<Prism<NumA, A>>, System.IComparable where NumA : struct, Num<A>
{
public readonly A Width;
public readonly A Height;
public override int @Tag => 3;
public Prism(A Width, A Height)
{
this.Width = Width;
this.Height = Height;
}
public void Deconstruct(out A Width, out A Height)
{
Width = this.Width;
Height = this.Height;
}
private Prism(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
{
this.Width = (A)info.GetValue("width", typeof(A));
this.Height = (A)info.GetValue("height", typeof(A));
}
public void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
{
info.AddValue("width", this.Width);
info.AddValue("height", this.Height);
}
public static bool operator ==(Prism<NumA, A> x, Prism<NumA, A> y) => ReferenceEquals(x, y) || (x?.Equals(y) ?? false);
public static bool operator !=(Prism<NumA, A> x, Prism<NumA, A> y) => !(x == y);
public static bool operator>(Prism<NumA, A> x, Prism<NumA, A> y) => !ReferenceEquals(x, y) && !ReferenceEquals(x, null) && x.CompareTo(y) > 0;
public static bool operator <(Prism<NumA, A> x, Prism<NumA, A> y) => !ReferenceEquals(x, y) && (ReferenceEquals(x, null) && !ReferenceEquals(y, null) || x.CompareTo(y) < 0);
public static bool operator >=(Prism<NumA, A> x, Prism<NumA, A> y) => ReferenceEquals(x, y) || (!ReferenceEquals(x, null) && x.CompareTo(y) >= 0);
public static bool operator <=(Prism<NumA, A> x, Prism<NumA, A> y) => ReferenceEquals(x, y) || (ReferenceEquals(x, null) && !ReferenceEquals(y, null) || x.CompareTo(y) <= 0);
public bool Equals(Prism<NumA, A> other)
{
if (LanguageExt.Prelude.isnull(other))
return false;
if (!default(LanguageExt.ClassInstances.EqDefault<A>).Equals(this.Width, other.Width))
return false;
if (!default(LanguageExt.ClassInstances.EqDefault<A>).Equals(this.Height, other.Height))
return false;
return true;
}
public override bool Equals(object obj) => obj is Prism<NumA, A> tobj && Equals(tobj);
public override bool Equals(Shape<NumA, A> obj) => obj is Prism<NumA, A> tobj && Equals(tobj);
public override int CompareTo(object obj) => obj is Shape<NumA, A> p ? CompareTo(p) : 1;
public override int CompareTo(Shape<NumA, A> obj) => obj is Prism<NumA, A> tobj ? CompareTo(tobj) : obj is null ? 1 : @Tag.CompareTo(obj.@Tag);
public int CompareTo(Prism<NumA, A> other)
{
if (LanguageExt.Prelude.isnull(other))
return 1;
int cmp = 0;
cmp = default(LanguageExt.ClassInstances.OrdDefault<A>).Compare(this.Width, other.Width);
if (cmp != 0)
return cmp;
cmp = default(LanguageExt.ClassInstances.OrdDefault<A>).Compare(this.Height, other.Height);
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.EqDefault<A>).GetHashCode(this.Width) ^ state) * fnvPrime;
state = (default(LanguageExt.ClassInstances.EqDefault<A>).GetHashCode(this.Height) ^ state) * fnvPrime;
}
return state;
}
public override string ToString()
{
var sb = new System.Text.StringBuilder();
sb.Append("Prism(");
sb.Append(LanguageExt.Prelude.isnull(width) ? $"width: [null]" : $"width: {width}");
sb.Append($", ");
sb.Append(LanguageExt.Prelude.isnull(height) ? $"height: [null]" : $"height: {height}");
sb.Append(")");
return sb.ToString();
}
public Prism<NumA, A> With(A Width = default(A), A Height = default(A)) => new Prism<NumA, A>(Width ?? this.Width, Height ?? this.Height);
public static Lens<Prism<NumA, A>, A> width => __LensFields.width;
public static Lens<Prism<NumA, A>, A> height => __LensFields.height;
static class __LensFields
{
public static readonly Lens<Prism<NumA, A>, A> width = Lens<Prism<NumA, A>, A>.New(_x => _x.Width, _x => _y => _y.With(Width: _x));
public static readonly Lens<Prism<NumA, A>, A> height = Lens<Prism<NumA, A>, A>.New(_x => _x.Height, _x => _y => _y.With(Height: _x));
}
}
public static partial class Shape
{
public static Shape<NumA, A> Rectangle<NumA, A>(A width, A length)
where NumA : struct, Num<A> => new Rectangle<NumA, A>(width, length);
public static Shape<NumA, A> Circle<NumA, A>(A radius)
where NumA : struct, Num<A> => new Circle<NumA, A>(radius);
public static Shape<NumA, A> Prism<NumA, A>(A width, A height)
where NumA : struct, Num<A> => new Prism<NumA, A>(width, height);
}
[System.Serializable]
public abstract partial class Shape<NumA, A> : System.IEquatable<Shape<NumA, A>>, System.IComparable<Shape<NumA, A>>, System.IComparable
{
public abstract int @Tag
{
get;
}
public abstract int CompareTo(object obj);
public abstract int CompareTo(Shape<NumA, A> other);
public abstract bool Equals(Shape<NumA, A> other);
public override bool Equals(object obj) => obj is Shape<NumA, A> tobj && Equals(tobj);
public override int GetHashCode() => throw new System.NotSupportedException();
public static bool operator ==(Shape<NumA, A> x, Shape<NumA, A> y) => ReferenceEquals(x, y) || (x?.Equals(y) ?? false);
public static bool operator !=(Shape<NumA, A> x, Shape<NumA, A> y) => !(x == y);
public static bool operator>(Shape<NumA, A> x, Shape<NumA, A> y) => !ReferenceEquals(x, y) && !ReferenceEquals(x, null) && x.CompareTo(y) > 0;
public static bool operator <(Shape<NumA, A> x, Shape<NumA, A> y) => !ReferenceEquals(x, y) && (ReferenceEquals(x, null) && !ReferenceEquals(y, null) || x.CompareTo(y) < 0);
public static bool operator >=(Shape<NumA, A> x, Shape<NumA, A> y) => ReferenceEquals(x, y) || (!ReferenceEquals(x, null) && x.CompareTo(y) >= 0);
public static bool operator <=(Shape<NumA, A> x, Shape<NumA, A> y) => ReferenceEquals(x, y) || (ReferenceEquals(x, null) && !ReferenceEquals(y, null) || x.CompareTo(y) <= 0);
}
public abstract partial class _ShapeBase<NumA, A> : Shape<NumA, A> where NumA : struct, Num<A>
{
public override Shape<NumA, A> Rectangle(A width, A length) => throw new System.NotSupportedException();
public override Shape<NumA, A> Circle(A radius) => throw new System.NotSupportedException();
public override Shape<NumA, A> Prism(A width, A height) => throw new System.NotSupportedException();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment