Created
February 13, 2020 16:25
-
-
Save louthy/b2a494aa93e055aefc2103ca9fca7ecf to your computer and use it in GitHub Desktop.
This file contains 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
[System.Serializable] | |
public sealed class Just<A> : Maybe<A>, System.IEquatable<Just<A>>, System.IComparable<Just<A>>, System.IComparable | |
{ | |
public readonly A Value; | |
public Just(A Value) | |
{ | |
this.Value = Value; | |
} | |
public void Deconstruct(out A Value) | |
{ | |
Value = this.Value; | |
} | |
private Just(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) | |
{ | |
this.Value = (A)info.GetValue("value", typeof(A)); | |
} | |
public void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) | |
{ | |
info.AddValue("value", this.Value); | |
} | |
public static bool operator ==(Just<A> x, Just<A> y) => ReferenceEquals(x, y) || (x?.Equals(y) ?? false); | |
public static bool operator !=(Just<A> x, Just<A> y) => !(x == y); | |
public static bool operator>(Just<A> x, Just<A> y) => !ReferenceEquals(x, y) && !ReferenceEquals(x, null) && x.CompareTo(y) > 0; | |
public static bool operator <(Just<A> x, Just<A> y) => !ReferenceEquals(x, y) && (ReferenceEquals(x, null) && !ReferenceEquals(y, null) || x.CompareTo(y) < 0); | |
public static bool operator >=(Just<A> x, Just<A> y) => ReferenceEquals(x, y) || (!ReferenceEquals(x, null) && x.CompareTo(y) >= 0); | |
public static bool operator <=(Just<A> x, Just<A> y) => ReferenceEquals(x, y) || (ReferenceEquals(x, null) && !ReferenceEquals(y, null) || x.CompareTo(y) <= 0); | |
public bool Equals(Just<A> other) | |
{ | |
if (LanguageExt.Prelude.isnull(other)) | |
return false; | |
if (!default(LanguageExt.ClassInstances.EqDefault<A>).Equals(this.Value, other.Value)) | |
return false; | |
return true; | |
} | |
public override bool Equals(object obj) => obj is Just<A> tobj && Equals(tobj); | |
public int CompareTo(object obj) => obj is Maybe<A> p ? CompareTo(p) : 1; | |
public int CompareTo(Just<A> other) | |
{ | |
if (LanguageExt.Prelude.isnull(other)) | |
return 1; | |
int cmp = 0; | |
cmp = default(LanguageExt.ClassInstances.OrdDefault<A>).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.EqDefault<A>).GetHashCode(this.Value) ^ state) * fnvPrime; | |
} | |
return state; | |
} | |
public override string ToString() | |
{ | |
var sb = new System.Text.StringBuilder(); | |
sb.Append("Just("); | |
sb.Append(LanguageExt.Prelude.isnull(value) ? $"value: [null]" : $"value: {value}"); | |
sb.Append(")"); | |
return sb.ToString(); | |
} | |
Maybe<A> Maybe<A>.Just(A value) => throw new System.NotSupportedException(); | |
Maybe<A> Maybe<A>.Nothing() => throw new System.NotSupportedException(); | |
public Just<A> With(A Value = default(A)) => new Just<A>(Value ?? this.Value); | |
public static Lens<Just<A>, A> value => __LensFields.value; | |
static class __LensFields | |
{ | |
public static readonly Lens<Just<A>, A> value = Lens<Just<A>, A>.New(_x => _x.Value, _x => _y => _y.With(Value: _x)); | |
} | |
} | |
[System.Serializable] | |
public sealed class Nothing<A> : Maybe<A>, System.IEquatable<Nothing<A>>, System.IComparable<Nothing<A>>, System.IComparable | |
{ | |
public Nothing() | |
{ | |
} | |
public void Deconstruct() | |
{ | |
} | |
private Nothing(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) | |
{ | |
} | |
public void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) | |
{ | |
} | |
public static bool operator ==(Nothing<A> x, Nothing<A> y) => ReferenceEquals(x, y) || (x?.Equals(y) ?? false); | |
public static bool operator !=(Nothing<A> x, Nothing<A> y) => !(x == y); | |
public static bool operator>(Nothing<A> x, Nothing<A> y) => !ReferenceEquals(x, y) && !ReferenceEquals(x, null) && x.CompareTo(y) > 0; | |
public static bool operator <(Nothing<A> x, Nothing<A> y) => !ReferenceEquals(x, y) && (ReferenceEquals(x, null) && !ReferenceEquals(y, null) || x.CompareTo(y) < 0); | |
public static bool operator >=(Nothing<A> x, Nothing<A> y) => ReferenceEquals(x, y) || (!ReferenceEquals(x, null) && x.CompareTo(y) >= 0); | |
public static bool operator <=(Nothing<A> x, Nothing<A> y) => ReferenceEquals(x, y) || (ReferenceEquals(x, null) && !ReferenceEquals(y, null) || x.CompareTo(y) <= 0); | |
public bool Equals(Nothing<A> other) | |
{ | |
if (LanguageExt.Prelude.isnull(other)) | |
return false; | |
return true; | |
} | |
public override bool Equals(object obj) => obj is Nothing<A> tobj && Equals(tobj); | |
public int CompareTo(object obj) => obj is Maybe<A> p ? CompareTo(p) : 1; | |
public int CompareTo(Nothing<A> other) | |
{ | |
if (LanguageExt.Prelude.isnull(other)) | |
return 1; | |
return 0; | |
} | |
public override int GetHashCode() | |
{ | |
return 0; | |
} | |
public override string ToString() | |
{ | |
return "Nothing"; | |
} | |
Maybe<A> Maybe<A>.Just(A value) => throw new System.NotSupportedException(); | |
Maybe<A> Maybe<A>.Nothing() => throw new System.NotSupportedException(); | |
public Nothing<A> With() => new Nothing<A>(); | |
static class __LensFields | |
{ | |
} | |
} | |
public static partial class Maybe | |
{ | |
public static Maybe<A> Just<A>(A value) => new Just<A>(value); | |
public static Maybe<A> Nothing<A>() => new Nothing<A>(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment