Skip to content
Open
Show file tree
Hide file tree
Changes from all 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);
}
}
5 changes: 2 additions & 3 deletions Functional.Maybe/Functional.Maybe.csproj
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard1.0</TargetFramework>
<LangVersion>7.2</LangVersion>
<Version>2.0.0</Version>
<Version>2.1.0</Version>
<Authors>Andrey Tsvetkov; William Cassarin</Authors>
<PackageId>Functional.Maybe</PackageId>
<Company>Functional.Maybe</Company>
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
2 changes: 1 addition & 1 deletion Functional.Maybe/Functional.Maybe.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<title>Functional.Maybe — Option type for C# with LINQ support and rich fluent syntax for many popular uses</title>
<authors>Andrey Tsvetkov; original version by William Casarin</authors>
<owners>Andrey Tsvetkov</owners>
<license>https://raw.githubusercontent.com/AndreyTsvetkov/Functional.Maybe/master/License.txt</license>
<license type="expression">Apache-2.0</license>
<projectUrl>https://github.com/AndreyTsvetkov/Functional.Maybe</projectUrl>
<iconUrl>http://findicons.com/files/icons/1462/candy_hearts/32/maybe.png</iconUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
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
Loading