Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Map Method to transform Value and Exception #80

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
5 changes: 5 additions & 0 deletions src/Optional.Tests/EitherTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -653,13 +653,18 @@ public void Either_Transformation()

var noneExUpper = none.MapException(x => x.ToUpper());
var someExUpper = some.MapException(x => x.ToUpper());

var noneBothUpper = none.Map(x => string.Concat(x.Reverse()), x => x.ToUpper());
var someBothReverse = some.Map(x => string.Concat(x.Reverse()), x => x.ToUpper());

Assert.IsFalse(noneUpper.HasValue);
Assert.IsTrue(someUpper.HasValue);
Assert.AreEqual(noneUpper.ValueOr("ex"), "ex");
Assert.AreEqual(someUpper.ValueOr("ex"), "VAL");
Assert.AreEqual(noneExUpper.Match(val => val, ex => ex), "EX");
Assert.AreEqual(someExUpper.Match(val => val, ex => ex), "val");
Assert.AreEqual(noneBothUpper.Match(val => val, ex => ex), "EX");
Assert.AreEqual(someBothReverse.Match(val => val, ex => ex), "lav");

var noneNotNull = none.FlatMap(x => x.SomeNotNull<string, string>("ex1"));
var someNotNull = some.FlatMap(x => x.SomeNotNull<string, string>("ex1"));
Expand Down
17 changes: 17 additions & 0 deletions src/Optional/Option_Either.cs
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,23 @@ public Option<TResult, TException> Map<TResult>(Func<T, TResult> mapping)
);
}

/// <summary>
/// Transforms both, the inner value or the exceptional value in an optional.
/// </summary>
/// <param name="valueMapping">The transformation function for the inner value.</param>
/// <param name="exceptionMapping">The transformation function for the exceptional value.</param>
/// <returns>The transformed optional.</returns>
public Option<TResult, TExceptionResult> Map<TResult, TExceptionResult>(Func<T, TResult> valueMapping, Func<TException, TExceptionResult> exceptionMapping)
{
if (valueMapping == null) throw new ArgumentNullException(nameof(valueMapping));
if (exceptionMapping == null) throw new ArgumentNullException(nameof(exceptionMapping));

return Match(
some: value => Option.Some<TResult, TExceptionResult>(valueMapping(value)),
none: exception => Option.None<TResult, TExceptionResult>(exceptionMapping(exception))
);
}

/// <summary>
/// Transforms the exceptional value in an optional.
/// If the instance is not empty, no transformation is carried out.
Expand Down