Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Functional.Maybe.Json/MaybeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Concurrent;
using System.Linq;
using Newtonsoft.Json;
#pragma warning disable 618

namespace Functional.Maybe.Json
{
Expand Down
288 changes: 144 additions & 144 deletions Functional.Maybe/Either.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,147 +2,147 @@

namespace Functional.Either
{
/// <summary>
/// A functional monadic concept Either to make validation code more expressive and easier to maintain.
/// <typeparam name="TResult">Type of Result item</typeparam>
/// <typeparam name="TError">Type of Error item</typeparam>
/// </summary>
public readonly struct Either<TResult, TError>
{
private readonly TResult _resultValue;
private readonly TError _errorValue;
private readonly bool _success;

private Either(TResult result, TError error, bool success)
{
_success = success;

if (success)
{
_resultValue = result;
_errorValue = default;
}
else
{
_errorValue = error;
_resultValue = default;
}
}

/// <summary>
/// Constructs new <see cref="Either{TResult, TError}"/> with the Result part defined.
/// </summary>
public static Either<TResult, TError> Result(TResult result)
{
return new Either<TResult, TError>(result, default, true);
}

/// <summary>
/// Constructs new <see cref="Either{TResult, TError}"/> with the Error part defined.
/// </summary>
public static Either<TResult, TError> Error(TError error)
{
return new Either<TResult, TError>(default, error, false);
}

/// <summary>
/// Executes result or error function depending on the Either state.
/// </summary>
public T Match<T>(Func<TResult, T> resultFunc, Func<TError, T> errorFunc)
{
if (resultFunc == null)
{
throw new ArgumentNullException(nameof(resultFunc));
}

if (errorFunc == null)
{
throw new ArgumentNullException(nameof(errorFunc));
}

return _success ? resultFunc(_resultValue) : errorFunc(_errorValue);
}

/// <summary>
/// Executes result or error function depending on the Either state.
/// </summary>
public T Match<T>(Func<T> leftFunc, Func<T> rightFunc)
{
if (leftFunc == null)
{
throw new ArgumentNullException(nameof(leftFunc));
}

if (rightFunc == null)
{
throw new ArgumentNullException(nameof(rightFunc));
}

return _success ? leftFunc() : rightFunc();
}

/// <summary>
/// Executes result or error action depending on the Either state.
/// </summary>
public void Match(Action<TResult> resultAction, Action<TError> errorAction)
{
if (resultAction == null)
{
throw new ArgumentNullException(nameof(resultAction));
}

if (errorAction == null)
{
throw new ArgumentNullException(nameof(errorAction));
}

if (_success)
{
resultAction(_resultValue);
}
else
{
errorAction(_errorValue);
}
}

/// <summary>
/// Executes result or error action depending on the Either state.
/// </summary>
public void Match(Action resultAction, Action errorAction)
{
if (resultAction == null)
{
throw new ArgumentNullException(nameof(resultAction));
}

if (errorAction == null)
{
throw new ArgumentNullException(nameof(errorAction));
}

if (_success)
{
resultAction();
}
else
{
errorAction();
}
}

public TResult ResultOrDefault() => Match(res => res, err => default);
public TError ErrorOrDefault() => Match(res => default, err => err);
public TResult ResultOrDefault(TResult defaultValue) => Match(res => res, err => defaultValue);
public TError ErrorOrDefault(TError defaultValue) => Match(res => defaultValue, err => err);

public static implicit operator Either<TResult, TError>(TResult result) => Result(result);
public static implicit operator Either<TResult, TError>(TError error) => Error(error);
}

public static class EitherExtensions
{
public static Either<TL, TR> ToResult<TL, TR>(this TL result) => Either<TL, TR>.Result(result);
public static Either<TL, TR> ToError<TL, TR>(this TR error) => Either<TL, TR>.Error(error);
}
}
/// <summary>
/// A functional monadic concept Either to make validation code more expressive and easier to maintain.
/// <typeparam name="TResult">Type of Result item</typeparam>
/// <typeparam name="TError">Type of Error item</typeparam>
/// </summary>
public readonly struct Either<TResult, TError>
{
private readonly TResult _resultValue;
private readonly TError _errorValue;
private readonly bool _success;

private Either(TResult result, TError error, bool success)
{
_success = success;

if (success)
{
_resultValue = result;
_errorValue = default;
}
else
{
_errorValue = error;
_resultValue = default;
}
}

/// <summary>
/// Constructs new <see cref="Either{TResult, TError}"/> with the Result part defined.
/// </summary>
public static Either<TResult, TError> Result(TResult result)
{
return new Either<TResult, TError>(result, default, true);
}

/// <summary>
/// Constructs new <see cref="Either{TResult, TError}"/> with the Error part defined.
/// </summary>
public static Either<TResult, TError> Error(TError error)
{
return new Either<TResult, TError>(default, error, false);
}

/// <summary>
/// Executes result or error function depending on the Either state.
/// </summary>
public T Match<T>(Func<TResult, T> resultFunc, Func<TError, T> errorFunc)
{
if (resultFunc == null)
{
throw new ArgumentNullException(nameof(resultFunc));
}

if (errorFunc == null)
{
throw new ArgumentNullException(nameof(errorFunc));
}

return _success ? resultFunc(_resultValue) : errorFunc(_errorValue);
}

/// <summary>
/// Executes result or error function depending on the Either state.
/// </summary>
public T Match<T>(Func<T> leftFunc, Func<T> rightFunc)
{
if (leftFunc == null)
{
throw new ArgumentNullException(nameof(leftFunc));
}

if (rightFunc == null)
{
throw new ArgumentNullException(nameof(rightFunc));
}

return _success ? leftFunc() : rightFunc();
}

/// <summary>
/// Executes result or error action depending on the Either state.
/// </summary>
public void Match(Action<TResult> resultAction, Action<TError> errorAction)
{
if (resultAction == null)
{
throw new ArgumentNullException(nameof(resultAction));
}

if (errorAction == null)
{
throw new ArgumentNullException(nameof(errorAction));
}

if (_success)
{
resultAction(_resultValue);
}
else
{
errorAction(_errorValue);
}
}

/// <summary>
/// Executes result or error action depending on the Either state.
/// </summary>
public void Match(Action resultAction, Action errorAction)
{
if (resultAction == null)
{
throw new ArgumentNullException(nameof(resultAction));
}

if (errorAction == null)
{
throw new ArgumentNullException(nameof(errorAction));
}

if (_success)
{
resultAction();
}
else
{
errorAction();
}
}

public TResult ResultOrDefault() => Match(res => res, err => default);
public TError ErrorOrDefault() => Match(res => default, err => err);
public TResult ResultOrDefault(TResult defaultValue) => Match(res => res, err => defaultValue);
public TError ErrorOrDefault(TError defaultValue) => Match(res => defaultValue, err => err);

public static implicit operator Either<TResult, TError>(TResult result) => Result(result);
public static implicit operator Either<TResult, TError>(TError error) => Error(error);
}

public static class EitherExtensions
{
public static Either<TL, TR> ToResult<TL, TR>(this TL result) => Either<TL, TR>.Result(result);
public static Either<TL, TR> ToError<TL, TR>(this TR error) => Either<TL, TR>.Error(error);
}
}
3 changes: 1 addition & 2 deletions Functional.Maybe/Functional.Maybe.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard1.0</TargetFramework>
Expand All @@ -21,7 +21,6 @@
both =&gt; Console.WriteLine("Result is: {0}", both),
@else: () =&gt; Console.WriteLine("Result is Nothing, as one of the inputs was Nothing")
);</Description>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageLicenseUrl>https://raw.githubusercontent.com/AndreyTsvetkov/Functional.Maybe/master/License.txt</PackageLicenseUrl>
<PackageProjectUrl>https://github.com/AndreyTsvetkov/Functional.Maybe</PackageProjectUrl>
<PackageIconUrl>http://findicons.com/files/icons/1462/candy_hearts/32/maybe.png</PackageIconUrl>
Expand Down
5 changes: 3 additions & 2 deletions Functional.Maybe/Maybe.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
#pragma warning disable 618

namespace Functional.Maybe
{
Expand Down Expand Up @@ -34,7 +34,8 @@ namespace Functional.Maybe
/// <summary>
/// The value, stored in the monad. Can be accessed only if is really present, otherwise throws
/// </summary>
/// <exception cref="InvalidOperationException"> is thrown if not value is present</exception>
/// <exception cref="InvalidOperationException"> is thrown if not value is present</exception>
[Obsolete("Use Select, SelectMany wherever possible, otherwise suppress error explicitly")]
public T Value
{
get
Expand Down
3 changes: 2 additions & 1 deletion Functional.Maybe/MaybeAsync.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using System;
using System.Threading.Tasks;
#pragma warning disable 618

namespace Functional.Maybe
{
public static class MaybeAsync
{
/// <summary>
/// Flips Maybe and Task: instead of having Maybe&lt;Task&lt;T&gt;&gt; (as in case of Select) we get Task&lt;Maybe&lt;T&gt;&gt; and have possibility to await on it.
/// Flips Maybe and Task: instead of having Maybe{Task{T}} (as in case of Select) we get Task{Maybe{T}} and have possibility to await on it.
/// </summary>
/// <typeparam name="T">source type</typeparam>
/// <typeparam name="TR">async result type</typeparam>
Expand Down
3 changes: 2 additions & 1 deletion Functional.Maybe/MaybeBoolean.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using System;
#pragma warning disable 618

namespace Functional.Maybe
{
/// <summary>
/// Ternary logic with Maybe&lt;bool&gt; and combining T and bool to a Maybe value
/// Ternary logic with Maybe{bool}; and combining T and bool to a Maybe value
/// </summary>
public static class MaybeBoolean
{
Expand Down
1 change: 1 addition & 0 deletions Functional.Maybe/MaybeCompositions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
// ReSharper disable MemberCanBePrivate.Global
#pragma warning disable 618

namespace Functional.Maybe
{
Expand Down
1 change: 1 addition & 0 deletions Functional.Maybe/MaybeConvertions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
#pragma warning disable 618

namespace Functional.Maybe
{
Expand Down
9 changes: 5 additions & 4 deletions Functional.Maybe/MaybeEnumerable.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
#pragma warning disable 618

namespace Functional.Maybe
{
Expand Down Expand Up @@ -233,10 +234,10 @@ public static IEnumerable<T> Union<T>(this Maybe<T> @this, IEnumerable<T> others
/// Combines the current value, if exists, with passed IEnumerable
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="this"></param>
/// <param name="others"></param>
/// <param name="these"></param>
/// <param name="other"></param>
/// <returns></returns>
public static IEnumerable<T> Union<T>(this IEnumerable<T> @these, Maybe<T> other) =>
@these.Union(other.ToEnumerable());
public static IEnumerable<T> Union<T>(this IEnumerable<T> these, Maybe<T> other) =>
these.Union(other.ToEnumerable());
}
}
Loading