Skip to content

Commit

Permalink
Maybe interned.
Browse files Browse the repository at this point in the history
  • Loading branch information
drunkcod committed Jul 11, 2020
1 parent ec72141 commit 5cc8bd7
Show file tree
Hide file tree
Showing 15 changed files with 53 additions and 57 deletions.
2 changes: 2 additions & 0 deletions Source/Check.That/Check.That.csproj
Expand Up @@ -5,6 +5,8 @@
<RootNamespace>CheckThat</RootNamespace>
<LangVersion>latest</LangVersion>
<Version>0.0.1</Version>

<Description>Soft and expressive checking without traces of Cobol.</Description>
</PropertyGroup>

</Project>
1 change: 1 addition & 0 deletions Source/Check.That/Check.cs
Expand Up @@ -7,6 +7,7 @@
using Cone;
using Cone.Core;
using Cone.Expectations;
using CheckThat.Internals;

namespace CheckThat
{
Expand Down
2 changes: 1 addition & 1 deletion Source/Check.That/CheckResult.cs
@@ -1,4 +1,4 @@
using Cone.Core;
using CheckThat.Internals;

namespace Cone.Expectations
{
Expand Down
1 change: 1 addition & 0 deletions Source/Check.That/Expectations/BooleanExpect.cs
@@ -1,6 +1,7 @@
using System;
using System.Linq.Expressions;
using System.Reflection;
using CheckThat.Internals;
using Cone.Core;

namespace Cone.Expectations
Expand Down
3 changes: 2 additions & 1 deletion Source/Check.That/Expectations/MemberMethodExpect.cs
@@ -1,5 +1,6 @@
using System.Linq.Expressions;
using System.Linq.Expressions;
using System.Reflection;
using CheckThat.Internals;
using Cone.Core;

namespace Cone.Expectations
Expand Down
1 change: 1 addition & 0 deletions Source/Check.That/FailedExpectation.cs
@@ -1,5 +1,6 @@
using System;
using System.Linq;
using CheckThat.Internals;
using Cone.Core;
using Cone.Expectations;

Expand Down
@@ -1,6 +1,6 @@
using System;
using System;

namespace Cone.Core
namespace CheckThat.Internals
{
public struct Maybe<T> : IEquatable<Maybe<T>>
{
Expand All @@ -20,32 +20,26 @@ public struct Maybe<T> : IEquatable<Maybe<T>>

bool IsDefault => ReferenceEquals(value, Maybe.DefaultTag);

T RawValue => IsDefault ? default(T) : (T)value;
T RawValue => IsDefault ? default : (T)value;

public T Value {
get { return GetValueOrDefault(() => {
throw new InvalidOperationException("Can't get value from 'None'");
}); }
}
public T Value => IsNone
? throw new InvalidOperationException("Can't get value from 'None'")
: RawValue;

public T GetValueOrDefault(T defaultValue) =>
IsNone ? defaultValue : RawValue;

public T GetValueOrDefault(Func<T> getDefaultValue) {
return IsNone ? getDefaultValue() : RawValue;
}
public T GetValueOrDefault(Func<T> getDefaultValue) =>
IsNone ? getDefaultValue() : RawValue;

public static bool operator==(Maybe<T> left, Maybe<T> right) {
return left.Equals(right);
}
public static bool operator==(Maybe<T> left, Maybe<T> right) =>
left.Equals(right);

public static bool operator!=(Maybe<T> left, Maybe<T> right) {
return !(left == right);
}
public static bool operator!=(Maybe<T> left, Maybe<T> right) =>
!(left == right);

public override int GetHashCode() {
return IsNone ? 0 : value.GetHashCode();
}
public override int GetHashCode() =>
IsNone ? 0 : value.GetHashCode();

public override bool Equals(object obj) =>
ReferenceEquals(this, obj) || (obj is Maybe<T> && this == ((Maybe<T>)obj));
Expand All @@ -61,11 +55,9 @@ public static class Maybe
{
static internal object DefaultTag = new object();

public static Maybe<T> Some<T>(T value) { return Maybe<T>.Some(value); }
public static Maybe<T> Some<T>(T value) => Maybe<T>.Some(value);

public static Maybe<T> ToMaybe<T>(this T self) {
return Maybe<T>.Some(self);
}
public static Maybe<T> ToMaybe<T>(this T self) => Maybe<T>.Some(self);

public static Maybe<T> Try<T>(Func<T> getSome) {
try {
Expand All @@ -75,34 +67,30 @@ public static class Maybe
}
}

public static TResult Do<T, TResult>(this Maybe<T> self, Func<T, TResult> whenSome, Func<TResult> whenNone) {
return self.IsSomething
? whenSome(self.Value)
: whenNone();
}
public static TResult Do<T, TResult>(this Maybe<T> self, Func<T, TResult> whenSome, Func<TResult> whenNone) =>
self.IsSomething
? whenSome(self.Value)
: whenNone();

public static void Do<T>(this Maybe<T> self, Action<T> whenSome, Action whenNone) {
if(self.IsSomething)
whenSome(self.Value);
else whenNone();
}

public static Maybe<TResult> Map<T, TResult>(this Maybe<T> self, Func<T, TResult> projection) {
return self.IsSomething
? Maybe<TResult>.Some(projection(self.Value))
: Maybe<TResult>.None;
}
public static Maybe<TResult> Map<T, TResult>(this Maybe<T> self, Func<T, TResult> projection) =>
self.IsSomething
? Maybe<TResult>.Some(projection(self.Value))
: Maybe<TResult>.None;

public static Maybe<TResult> Map<T, TResult>(this Maybe<T> self, Func<T, Maybe<TResult>> projection) {
return self.IsSomething
? projection(self.Value)
: Maybe<TResult>.None;
}
public static Maybe<TResult> Map<T, TResult>(this Maybe<T> self, Func<T, Maybe<TResult>> projection) =>
self.IsSomething
? projection(self.Value)
: Maybe<TResult>.None;

public static Maybe<TResult> Map<T1, T2, TResult>(Maybe<T1> arg0, Maybe<T2> arg1, Func<T1,T2,TResult> projection) {
return (arg0.IsSomething && arg1.IsSomething)
? Maybe<TResult>.Some(projection(arg0.Value, arg1.Value))
: Maybe<TResult>.None;
}
public static Maybe<TResult> Map<T1, T2, TResult>(Maybe<T1> arg0, Maybe<T2> arg1, Func<T1,T2,TResult> projection) =>
(arg0.IsSomething && arg1.IsSomething)
? Maybe<TResult>.Some(projection(arg0.Value, arg1.Value))
: Maybe<TResult>.None;
}
}
3 changes: 2 additions & 1 deletion Source/Check.That/NotExpectedExpect.cs
@@ -1,4 +1,5 @@
using System.Linq.Expressions;
using System.Linq.Expressions;
using CheckThat.Internals;
using Cone.Core;
using Cone.Expectations;

Expand Down
3 changes: 2 additions & 1 deletion Source/Cone/Runners/TeamCityLogger.cs
@@ -1,8 +1,9 @@
using System;
using System;
using System.IO;
using Cone.Core;
using System.Threading;
using System.Diagnostics;
using CheckThat.Internals;

namespace Cone.Runners
{
Expand Down
1 change: 1 addition & 0 deletions Specs/Cone.Specs/BinaryExpectSpec.cs
@@ -1,6 +1,7 @@
using System;
using System.Linq.Expressions;
using CheckThat;
using CheckThat.Internals;
using Cone.Core;
using Cone.Expectations;
using Moq;
Expand Down
@@ -1,10 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CheckThat;
using Cone;

namespace Cone.Core
namespace CheckThat.Internals
{
[Describe(typeof(Maybe<>))]
public class MaybeSpec
Expand Down
1 change: 1 addition & 0 deletions Specs/Cone.Specs/CustomMethodExpectFeature.cs
Expand Up @@ -6,6 +6,7 @@
using Cone.Core;
using Cone.Expectations;
using CheckThat;
using CheckThat.Internals;

namespace Cone
{
Expand Down
1 change: 1 addition & 0 deletions Specs/Cone.Specs/ExpressionFormatterSpec.cs
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using System.Reflection;
using CheckThat;
using CheckThat.Internals;

namespace Cone.Core
{
Expand Down
5 changes: 3 additions & 2 deletions Specs/Cone.Specs/Runners/TeamCityLoggerSpec.cs
Expand Up @@ -2,10 +2,11 @@
using System.IO;
using System.Linq;
using System.Text;
using CheckThat;
using CheckThat.Internals;
using Cone.Core;
using Cone.Stubs;
using Cone.Expectations;
using CheckThat;
using Cone.Stubs;

namespace Cone.Runners
{
Expand Down
2 changes: 1 addition & 1 deletion Specs/Cone.Specs/StringContainsSpec.cs
@@ -1,7 +1,7 @@
using System;
using System.Linq.Expressions;
using CheckThat;
using Cone.Core;
using CheckThat.Internals;
using Cone.Expectations;

namespace Cone
Expand Down

0 comments on commit 5cc8bd7

Please sign in to comment.