Skip to content

Instantly share code, notes, and snippets.

Avatar
😃

Davide Lettieri davidelettieri

😃
View GitHub Profile
@davidelettieri
davidelettieri / wf-visitor-pattern.cs
Created Feb 21, 2021
Sample code for workflows with visitor pattern
View wf-visitor-pattern.cs
public class Claim
{
public Workflow Workflow { get; }
}
public abstract class Workflow
{
public abstract void Fire(Transition transition, Claim claim);
}
public class FullWF : Workflow
View VisitorImpl.cs
class VisitorImpl : PolynomialBaseVisitor<INode>
{
public override INode VisitPlusminus([NotNull] PolynomialParser.PlusminusContext context)
{
var left = context.expr()[0];
var right = context.expr()[1];
if (context.MINUS() != null)
return new SubtractNode(Visit(left), Visit(right));
View NodeEx.cs
public interface INode
{
double Eval(Dictionary<char, double> variablesValue);
}
public class AddNode : INode
{
private readonly INode _left;
private readonly INode _right;
View Rop9.cs
public static Func<T, T> ToFunc<T>(Action<T> a)
{
return t =>
{
a(t);
return t;
};
}
View Rop8.cs
public static Func<T, Result<S, TError>> ToSwitchFunction<T, S, TError>(Func<T, S> a)
{
return t =>
{
return a(t);
};
}
public static Func<T, Result<T, TError>> ToSwitchFunction<T, TError>(Action<T> a)
{
View Rop7.cs
public static Result<S, TError> Then<S, TValue, TError>(this Result<TValue, TError> r, Func<TValue, Result<S, TError>> f)
=> Bind(f)(r);
View Rop6.cs
public static Func<Result<TSuccess, TError>, Result<TSuccess, TError>> Bind<TSuccess, TError>(Func<TSuccess, Result<TSuccess, TError>> f)
{
return r => r.Match(t => f(t), e => new Result<TSuccess, TError>(e));
}
View Rop5.cs
Union<Customer,Failure> ValidateEmail(Customer c) {
if(c.Email is null)
return new Failure("Email is not valid");
return c;
}
Union<Customer,Failure> ValidateNameSurname(Customer c) {
if(c.Name is null)
return new Failure("Name is not valid");
View Rop4.cs
Union<Success,Failure> ValidateEmail(Customer c) { ... }
public class Success {}
public class Failure {
public string Message {get;}
public Failure(string message) {
Message = message;
}
View Rop3.cs
Result ValidateEmail(Customer c) { ... }
Result ValidateNameSurname(Customer c) { ... }
Result SendEmail(Customer c) { ... }
Result SendEmailToCustomerIfValid(Customer c) {
var validateSurname = ValidateNameSurname(c);
if(!validateSurname.Success)
return validateSurname;
var validateEmail = ValidateEmail(c);