Skip to content

Commit

Permalink
Improve README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
futurum-dev committed Apr 8, 2023
1 parent 46b3e7b commit 068653e
Show file tree
Hide file tree
Showing 8 changed files with 848 additions and 238 deletions.
813 changes: 703 additions & 110 deletions README.md

Large diffs are not rendered by default.

42 changes: 42 additions & 0 deletions src/Futurum.Core/Option/Option.Dictionary.TryGetValue.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
namespace Futurum.Core.Option;

public static partial class OptionDictionaryExtensions
{
/// <summary>
/// Get the value associated with the specified key as an Option.
/// <list type="bullet">
/// <item>
/// <description>
/// If the key is found, return the value as <see cref="Option{T}"/>.
/// </description>
/// </item>
/// <item>
/// <description>
/// If the key is not found, return the value as <see cref="Option{T}"/> <see cref="Option{T}.None"/>.
/// </description>
/// </item>
/// </list>
/// </summary>
public static Option<TValue> TryGetValue<TKey, TValue>(this Dictionary<TKey, TValue> source, TKey key)
where TKey : notnull =>
source.TryGetValue(key, out var value) ? Option<TValue>.From(value) : Option<TValue>.None;

/// <summary>
/// Get the value associated with the specified key as an Option.
/// <list type="bullet">
/// <item>
/// <description>
/// If the key is found, return the value as <see cref="Option{T}"/>.
/// </description>
/// </item>
/// <item>
/// <description>
/// If the key is not found, return the value as <see cref="Option{T}"/> <see cref="Option{T}.None"/>.
/// </description>
/// </item>
/// </list>
/// </summary>
public static Option<TValue> TryGetValue<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> source, TKey key)
where TKey : notnull =>
source.TryGetValue(key, out var value) ? Option<TValue>.From(value) : Option<TValue>.None;
}
8 changes: 5 additions & 3 deletions src/Futurum.Core/Option/Option.Enumerable.Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ namespace Futurum.Core.Option;
public static partial class OptionEnumerableExtensions
{
/// <summary>
/// Transforms each element of a sequence into a new form.
/// Transforms each element of a sequence that matches <see cref="Option.FilterHasValue"/> predicate into a new form.
/// </summary>
public static IEnumerable<TR> Map<TSource, TR>(this IEnumerable<Option<TSource>> source, Func<Option<TSource>, TR> func) =>
source.Select(func);
public static IEnumerable<TR> Map<TSource, TR>(this IEnumerable<Option<TSource>> source, Func<TSource, TR> func) =>
source.Where(Option.FilterHasValue)
.Select(option => option.Value)
.Select(func);
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,9 @@
using Futurum.Core.Result;
namespace Futurum.Core.Result;

namespace Futurum.Core.Option;

public static class OptionDictionaryExtensions
public static partial class ResultDictionaryExtensions
{
/// <summary>
/// Get the value associated with the specified key as an Option.
/// <list type="bullet">
/// <item>
/// <description>
/// If the key is found, return the value as <see cref="Option{T}"/>.
/// </description>
/// </item>
/// <item>
/// <description>
/// If the key is not found, return the value as <see cref="Option{T}"/> <see cref="Option{T}.None"/>.
/// </description>
/// </item>
/// </list>
/// </summary>
public static Option<TValue> TryGetValue<TKey, TValue>(this Dictionary<TKey, TValue> source, TKey key)
where TKey : notnull =>
source.TryGetValue(key, out var value) ? Option<TValue>.From(value) : Option<TValue>.None;

/// <summary>
/// Get the value associated with the specified key as an Option.
/// Get the value associated with the specified key as an Result.
/// <list type="bullet">
/// <item>
/// <description>
Expand All @@ -40,10 +19,10 @@ public static Option<TValue> TryGetValue<TKey, TValue>(this Dictionary<TKey, TVa
/// </summary>
public static Result<TValue> TryGetValue<TKey, TValue>(this Dictionary<TKey, TValue> source, TKey key, string errorMessage)
where TKey : notnull =>
TryGetValue(source, key).ToResult(errorMessage);
source.TryGetValue(key, out var value) ? Result.Ok(value) : Result.Fail<TValue>(errorMessage);

/// <summary>
/// Get the value associated with the specified key as an Option.
/// Get the value associated with the specified key as an Result.
/// <list type="bullet">
/// <item>
/// <description>
Expand All @@ -59,29 +38,10 @@ public static Result<TValue> TryGetValue<TKey, TValue>(this Dictionary<TKey, TVa
/// </summary>
public static Result<TValue> TryGetValue<TKey, TValue>(this Dictionary<TKey, TValue> source, TKey key, Func<string> errorMessage)
where TKey : notnull =>
TryGetValue(source, key).ToResult(errorMessage);

/// <summary>
/// Get the value associated with the specified key as an Option.
/// <list type="bullet">
/// <item>
/// <description>
/// If the key is found, return the value as <see cref="Option{T}"/>.
/// </description>
/// </item>
/// <item>
/// <description>
/// If the key is not found, return the value as <see cref="Option{T}"/> <see cref="Option{T}.None"/>.
/// </description>
/// </item>
/// </list>
/// </summary>
public static Option<TValue> TryGetValue<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> source, TKey key)
where TKey : notnull =>
source.TryGetValue(key, out var value) ? Option<TValue>.From(value) : Option<TValue>.None;
source.TryGetValue(key, out var value) ? Result.Ok(value) : Result.Fail<TValue>(errorMessage());

/// <summary>
/// Get the value associated with the specified key as an Option.
/// Get the value associated with the specified key as an Result.
/// <list type="bullet">
/// <item>
/// <description>
Expand All @@ -97,10 +57,10 @@ public static Option<TValue> TryGetValue<TKey, TValue>(this IReadOnlyDictionary<
/// </summary>
public static Result<TValue> TryGetValue<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> source, TKey key, string errorMessage)
where TKey : notnull =>
TryGetValue(source, key).ToResult(errorMessage);
source.TryGetValue(key, out var value) ? Result.Ok(value) : Result.Fail<TValue>(errorMessage);

/// <summary>
/// Get the value associated with the specified key as an Option.
/// Get the value associated with the specified key as an Result.
/// <list type="bullet">
/// <item>
/// <description>
Expand All @@ -116,5 +76,5 @@ public static Result<TValue> TryGetValue<TKey, TValue>(this IReadOnlyDictionary<
/// </summary>
public static Result<TValue> TryGetValue<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> source, TKey key, Func<string> errorMessage)
where TKey : notnull =>
TryGetValue(source, key).ToResult(errorMessage);
source.TryGetValue(key, out var value) ? Result.Ok(value) : Result.Fail<TValue>(errorMessage());
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System.Collections.ObjectModel;
using System.Linq;

using Futurum.Core.Option;
using Futurum.Core.Tests.Helper.Option;

using Xunit;

namespace Futurum.Core.Tests.Option;

public class OptionDictionaryExtensionsTryGetValueTests
{
private const string ErrorMessage = "ERROR_MESSAGE";

public class Dictionary
{
[Fact]
public void HasNoValue()
{
var values = Enumerable.Range(0, 10)
.ToDictionary(x => x, x => x);

var option = values.TryGetValue(100);

option.ShouldBeHasNoValue();
}

[Fact]
public void HasValue()
{
var values = Enumerable.Range(0, 10)
.ToDictionary(x => x, x => x);

var option = values.TryGetValue(2);

option.ShouldBeHasValueWithValue(2);
}
}

public class IReadOnlyDictionary
{
[Fact]
public void HasNoValue()
{
var values = new ReadOnlyDictionary<int, int>(Enumerable.Range(0, 10)
.ToDictionary(x => x, x => x));

var option = values.TryGetValue(100);

option.ShouldBeHasNoValue();
}

[Fact]
public void HasValue()
{
var values = new ReadOnlyDictionary<int, int>(Enumerable.Range(0, 10)
.ToDictionary(x => x, x => x));

var option = values.TryGetValue(2);

option.ShouldBeHasValueWithValue(2);
}
}
}
16 changes: 5 additions & 11 deletions test/Futurum.Core.Tests/Option/OptionTests.Enumerable.Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using FluentAssertions;

using Futurum.Core.Option;
using Futurum.Core.Tests.Helper.Option;

using Xunit;

Expand All @@ -14,17 +13,12 @@ public class OptionEnumerableExtensionsMapTests
[Fact]
public void Basic()
{
var options = Enumerable.Range(0, 5)
.Select(i => i % 2 == 0 ? i.ToOption() : Core.Option.Option.None<int>());
var numbers = Enumerable.Range(0, 5);
var options = numbers.Select(i => i.ToOption());

var values = options.Map(x => x.HasValue ? (x.Value * 2).ToOption() : Core.Option.Option.None<int>())
.ToList();
var values = options.Map(x => x + 1);

values.Count.Should().Be(5);
values[0].Should().Be(0);
values[1].ShouldBeHasNoValue();
values[2].Should().Be(4);
values[3].ShouldBeHasNoValue();
values[4].Should().Be(8);
values.Count().Should().Be(5);
values.Should().BeEquivalentTo(numbers.Select(x => x + 1));
}
}
Loading

0 comments on commit 068653e

Please sign in to comment.