Skip to content

Instantly share code, notes, and snippets.

@kmorcinek
Created October 20, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kmorcinek/8ec29029b1a49e25186a to your computer and use it in GitHub Desktop.
Save kmorcinek/8ec29029b1a49e25186a to your computer and use it in GitHub Desktop.
using System;
using System.ComponentModel;
using System.Diagnostics;
public static class EitherExtensions
{
[DebuggerStepThrough]
public static Either<TNew, TFailure> IfSuccess<TCurrent, TNew, TFailure>(this Either<TCurrent, TFailure> either, Func<TCurrent, Either<TNew, TFailure>> continuation)
{
if (!either.Succeeded)
return new Either<TNew, TFailure>(either.FailureValue);
return continuation(either.SuccessValue);
}
[DebuggerStepThrough]
[EditorBrowsable(EditorBrowsableState.Never)]
public static Either<TNewSuccess, TFailure> SelectMany<TSuccess, TNewSuccess, TFailure>(this Either<TSuccess, TFailure> either, Func<TSuccess, Either<TNewSuccess, TFailure>> continuation)
{
return either.IfSuccess(continuation);
}
[DebuggerStepThrough]
[EditorBrowsable(EditorBrowsableState.Never)]
public static Either<TSuccess2, TFailure> SelectMany<TSuccess, TSuccess1, TSuccess2, TFailure>(this Either<TSuccess, TFailure> either, Func<TSuccess, Either<TSuccess1, TFailure>> continuation, Func<TSuccess, TSuccess1, TSuccess2> projection)
{
return IfSuccess(either, x => IfSuccess(continuation(x), y => new Either<TSuccess2, TFailure>(projection(x, y))));
}
public static TSuccess GetSuccessOrDefault<TSuccess, TFailure>(this Either<TSuccess, TFailure> either, TSuccess @default)
{
if (either.Succeeded)
return either.SuccessValue;
return @default;
}
public static Option<TSuccess> SuccessToOption<TSuccess, TFailure>(this Either<TSuccess, TFailure> either)
{
if (either.Succeeded)
return either.SuccessValue.ToOption();
return Option<TSuccess>.None;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment