Skip to content

Commit

Permalink
Added Success and Fail Operators #7
Browse files Browse the repository at this point in the history
  • Loading branch information
odytrice committed Oct 9, 2017
1 parent 9dee366 commit 35108a4
Show file tree
Hide file tree
Showing 11 changed files with 182 additions and 120 deletions.
54 changes: 0 additions & 54 deletions src/Operation/CriminalBind.cs

This file was deleted.

82 changes: 41 additions & 41 deletions src/Operation/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public static Operation<T> Fold<T>(this IEnumerable<Operation<T>> operations, Fu
};
}

List<Operation> badOperations = new List<Operation>();
List<Operation<T>> badOperations = new List<Operation<T>>();

//Process First Element
var result = default(T);
Expand Down Expand Up @@ -245,48 +245,48 @@ public static Operation<T> Fold<T>(this IEnumerable<Operation<T>> operations, Fu
}


public static Operation Fold(this IEnumerable<Operation> operations)
{
using (var e = operations.GetEnumerator())
{
if (!e.MoveNext())
{
var ex = new InvalidOperationException("Sequence contains no Elements");
return new Operation(ex)
{
Message = ex.Message,
};
}
//public static Operation Fold(this IEnumerable<Operation> operations)
//{
// using (var e = operations.GetEnumerator())
// {
// if (!e.MoveNext())
// {
// var ex = new InvalidOperationException("Sequence contains no Elements");
// return new Operation(ex)
// {
// Message = ex.Message,
// };
// }

List<Operation> badOperations = new List<Operation>();
do
{
//If the Current Operation did not succeed, add it
if (!e.Current.Succeeded)
{
badOperations.Add(e.Current);
};
}
while (e.MoveNext());
// List<Operation> badOperations = new List<Operation>();
// do
// {
// //If the Current Operation did not succeed, add it
// if (!e.Current.Succeeded)
// {
// badOperations.Add(e.Current);
// };
// }
// while (e.MoveNext());


//If there were any bad operations
if (badOperations.Any())
{
return new Operation
{
Message = badOperations.Select(o => o.Message).Aggregate((ag, m) => ag + ", " + m),
Succeeded = false
};
}
else
{
return new Operation()
{
Succeeded = true,
};
}
}
}
// //If there were any bad operations
// if (badOperations.Any())
// {
// return new Operation
// {
// Message = badOperations.Select(o => o.Message).Aggregate((ag, m) => ag + ", " + m),
// Succeeded = false
// };
// }
// else
// {
// return new Operation()
// {
// Succeeded = true,
// };
// }
// }
//}
}
}
2 changes: 1 addition & 1 deletion src/Operation/LinqSugar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public static Operation<IEnumerable<V>> SelectMany<T, U, V>(this Operation<T> op
}

[DebuggerHidden]
private static IEnumerable<Operation<V>> SelectMany<T, U, V>(this IEnumerable<T> operation, Func<T, Operation<U>> process, Func<T, U, V> projection)
public static IEnumerable<Operation<V>> SelectMany<T, U, V>(this IEnumerable<T> operation, Func<T, Operation<U>> process, Func<T, U, V> projection)
{
return operation.Select(x => OperationExtensions.Next(process(x), u => projection(x, u)));
}
Expand Down
46 changes: 46 additions & 0 deletions src/Operation/RegularBind.cs → src/Operation/Next.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace System
{
public static partial class OperationExtensions
{
#region Regular Bind
[DebuggerHidden]
public static Operation Next(this Operation operation, Action process)
{
Expand Down Expand Up @@ -51,5 +52,50 @@ public static Operation<U> Next<T, U>(this Operation<T> operation, Func<T, U> pr
Message = operation.Message
};
}
#endregion

#region Criminal Bind
[DebuggerHidden]
public static Operation Next(this Operation operation, Func<Operation> process)
{
if (operation.Succeeded)
return process();
return operation;
}

[DebuggerHidden]
public static Operation<T> Next<T>(this Operation operation, Func<Operation<T>> process)
{
if (operation.Succeeded)
return process();
return new Operation<T>(operation.GetException())
{
Succeeded = false,
Result = default(T),
Message = operation.Message
};
}

[DebuggerHidden]
public static Operation Next<T>(this Operation<T> operation, Func<T, Operation> process)
{
if (operation.Succeeded)
return process(operation.Result);
return operation;
}

[DebuggerHidden]
public static Operation<U> Next<T, U>(this Operation<T> operation, Func<T, Operation<U>> process)
{
if (operation.Succeeded)
return process(operation.Result);
return new Operation<U>(operation.GetException())
{
Succeeded = false,
Result = default(U),
Message = operation.Message
};
}
#endregion
}
}
20 changes: 10 additions & 10 deletions src/Operation/Operation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,33 @@ namespace System
/// <summary>
/// Represents a piece of Computation that has no Return Value
/// </summary>
public partial class Operation
public partial class Operation: Operation<object>
{
public bool Succeeded { get; set; }
public string Message { get; set; }


protected Exception _exception;
public Exception GetException() => _exception;

public Operation()
{

}
public Operation(Exception ex)

public Operation(Exception ex): base(ex)
{
_exception = ex;

}
}

/// <summary>
/// Represents a piece of Computation that has a return Value
/// </summary>
/// <typeparam name="T">Return Value Type</typeparam>
public partial class Operation<T> : Operation
public partial class Operation<T>
{
public bool Succeeded { get; set; }
public string Message { get; set; }
public T Result { get; set; }

protected Exception _exception;
public Exception GetException() => _exception;

public Operation()
{

Expand Down
48 changes: 46 additions & 2 deletions src/Operation/Unit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,57 @@ public static async Task<Operation<T>> Run<T>(Func<Task<T>> process)
}
return operation;
}

public static Operation Success() => Success<object>(null);

public static Operation<T> Success<T>(T result)
{
return new Operation<T>
{
Result = result,
Succeeded = true
};
}

public static Operation Fail(string message) => Fail<object>(message);

public static Operation<T> Fail<T>(string message)
{
var exception = new Exception(message);

return new Operation<T>(exception)
{
Message = message,
Result = default(T),
Succeeded = false,
};
}
}

public partial class Operation<T>
{
[DebuggerHidden]
public void Catch(Exception ex)
{
_exception = ex;
while (ex.InnerException != null) ex = ex.InnerException;
this.Succeeded = false;
this.Message = ex.Message;
Succeeded = false;
Message = ex.Message;
}


/// <summary>
/// Implicit Downgrade of 'T Operation to object Operation
/// </summary>
/// <param name="operation"></param>
public static implicit operator Operation(Operation<T> operation)
{
return new Operation(operation.GetException())
{
Message = operation.Message,
Result = operation.Result,
Succeeded = operation.Succeeded
};
}
}

Expand Down
10 changes: 5 additions & 5 deletions test/Tests/ExtensionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ public void AllWithFailure()
var op2 = Operation.Create(() =>
{
var condition = true; //Prevent "Unreachable Code Detected"
if(condition) throw new Exception("Something Bad Happened");
if (condition) throw new Exception("Something Bad Happened");
return "";
});

var all = new[] { op1, op2 }.Fold((a, s) => a + " " + s);


Assert.IsNull(all.Result);
Assert.IsFalse(all.Succeeded);
Assert.AreEqual("Something Bad Happened", all.Message);
Expand All @@ -81,7 +81,7 @@ public void GenericAll()
var op1 = Operation.Create(() => "Hello");
var op2 = Operation.Create(() => "World");

var all = new[] { op1, op2 }.Fold();
var all = new[] { op1, op2 }.Fold((ag, e) => ag + " " + e);

Assert.IsTrue(all.Succeeded);
}
Expand All @@ -93,11 +93,11 @@ public void GenericAllWithFailure()
var op2 = Operation.Create(() =>
{
var condition = true; //Prevent "Unreachable Code Detected"
if(condition) throw new Exception("Something Bad Happened");
if (condition) throw new Exception("Something Bad Happened");
return "";
});

var all = new[] { op1, op2 }.Fold();
var all = new[] { op1, op2 }.Fold((ag, e) => ag + " " + e);

Assert.IsFalse(all.Succeeded);
Assert.AreEqual("Something Bad Happened", all.Message);
Expand Down
8 changes: 4 additions & 4 deletions test/Tests/LinqSugarTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ from o2 in _op1
[TestMethod]
public void LinqWithEnumerableFirst()
{
var list = new[] { 1, 2, 3 };
var list = new[] { 1, 2, 3 }.Select(x => x.ToString());

var query = from x in list
from y in _op2
Expand Down Expand Up @@ -131,9 +131,9 @@ public void OperationNextRegularMethods()
public void OperationNextFailed()
{
//Act
var ops1 = _op1.Next(() => _fail).Next((op) => _op3);
var ops2 = _op2.Next((op) => _fail).Next(() => _op1);
var ops3 = _op2.Next((op) => _op1).Next(() => _fail);
var ops1 = _op1.Next(() => _fail).Next(op => _op3);
var ops2 = _op2.Next(op => _fail).Next(op => _op1);
var ops3 = _op2.Next(op => _op1).Next(() => _fail);

//Assert
Assert.IsFalse(ops1.Succeeded);
Expand Down
2 changes: 1 addition & 1 deletion test/Tests/RegularBindTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void OperationNextFailed()
{
//Act
var ops1 = _op1.Next(() => _fail).Next((op) => _op3);
var ops2 = _op2.Next((op) => _fail).Next(() => _op1);
var ops2 = _op2.Next((op) => _fail).Next(op => _op1);
var ops3 = _op2.Next((op) => _op1).Next(() => _fail);

//Assert
Expand Down
Loading

0 comments on commit 35108a4

Please sign in to comment.