Saturday, August 15, 2020

Railway Oriented Programming In CSharp

 Railway Oriented Programming My Implementation In CSharp





Error handling using ROP



Implementation of Either Monad using Interface

public interface IResult<T>
{
TResult Match<TResult>(
Func<T, TResult> onSuccess,
Func<ErrorType, TResult> onError
);
IResult<TResult> Map<TResult>(Func<T, TResult> f);
IResult<TResult> Bind<TResult>(Func<T, IResult<TResult>> f);
}
public class Success<T> : IResult<T>
{
private readonly T _results;
public Success(T results)
{
_results = results;
}
public TResult Match<TResult>(Func<T, TResult> onSuccess, Func<ErrorType, TResult> _) => onSuccess(_results);
public IResult<TResult> Map<TResult>(Func<T, TResult> f) => new Success<TResult>(f(_results));
public IResult<TResult> Bind<TResult>(Func<T, IResult<TResult>> f) => f(_results);
}
public class Error<T> : IResult<T>
{
private readonly ErrorType _error;
public Error(ErrorType error)
{
_error = error;
}
public TResult Match<TResult>(Func<T, TResult> _, Func<ErrorType, TResult> onError) => onError(_error);
public IResult<TResult> Map<TResult>(Func<T, TResult> _) => new Error<TResult>(_error);
public IResult<TResult> Bind<TResult>(Func<T, IResult<TResult>> _) => new Error<TResult>(_error);
}
view raw Result.cs hosted with ❤ by GitHub

Keeping all our errors in a single enum

public enum ErrorType
{
NameCanNotBeBlank,
EmailCanNotBeBlank,
EmailNotValid,
UserNotFound,
DatabaseUpdateError,
EmailNotSend
}
view raw ErrorType.cs hosted with ❤ by GitHub

Github Repo

Example taken from Railway Oriented Programming



Football Player Transfer Prediction

Football Player Transfer Prediction Using Different Classifiers Project Report :  Football Player Transfer Prediction Report ...