Skip to content

Instantly share code, notes, and snippets.

@ozw-sei
Created August 30, 2016 09:22
Show Gist options
  • Save ozw-sei/f63b27ae0e2eb4ed5068e8c8d498dfd0 to your computer and use it in GitHub Desktop.
Save ozw-sei/f63b27ae0e2eb4ed5068e8c8d498dfd0 to your computer and use it in GitHub Desktop.
using System;
namespace LSD
{
public interface IEither<T1, T2>
{
U Use<U>(Func<T1, U> ofLeft, Func<T2, U> ofRight);
bool IsRight{
get;
}
bool IsLeft{
get;
}
}
public static class Either{
public static IEither<T1, T2> Left<T1, T2>(T1 value){
return new Either.LeftEither<T1, T2>(value);
}
public static IEither<T1, T2> Right<T1, T2>(T2 value){
return new Either.RightEither<T1, T2>(value);
}
sealed class LeftEither<T1, T2>: IEither<T1, T2>{
private readonly T1 value;
public LeftEither(T1 value)
{
this.value = value;
}
public U Use<U>(Func<T1, U> ofLeft, Func<T2, U> ofRight){
if (ofLeft == null)
throw new ArgumentNullException();
return ofLeft(value);
}
public bool IsRight{
get{
return false;
}
}
public bool IsLeft{
get{
return true;
}
}
}
sealed class RightEither<T1, T2>: IEither<T1, T2>{
private readonly T2 value;
public RightEither(T2 value)
{
this.value = value;
}
public U Use<U>(Func<T1, U> ofLeft, Func<T2, U> ofRight){
if (ofRight == null)
throw new ArgumentNullException();
return ofRight(value);
}
public bool IsRight{
get{
return true;
}
}
public bool IsLeft{
get{
return false;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment