Skip to content

Instantly share code, notes, and snippets.

@peteraritchie
Last active September 10, 2024 22:25
Show Gist options
  • Save peteraritchie/f258e7253d94d8130435186fe7faf50d to your computer and use it in GitHub Desktop.
Save peteraritchie/f258e7253d94d8130435186fe7faf50d to your computer and use it in GitHub Desktop.
/// <summary>
/// Some extensions for <seealso cref="ImmutableList{T}"/>
/// </summary>
public static class ImmutableListExtensions
{
/// <summary>
/// Replace <paramref name="value"/> in <paramref name="source"/> based on object equality
/// </summary>
/// <remarks>
/// This method depends on the <typeparamref name="T"/> implementing Object.Equals, if it does not,
/// this method will replace the same instance, defeating the purpose.
/// To replace based on equality comparison different from the <typeparamref name="T"/> implementation
/// prefer <seealso cref="ImmutableListExtensions.Replace{T}(ImmutableList{T}, T, IEqualityComparer{T})"/>
/// </remarks>
/// <typeparam name="T"></typeparam>
/// <param name="source"></param>
/// <param name="value"></param>
/// <returns></returns>
public static ImmutableList<T> Replace<T>(this ImmutableList<T> source, T value)
{
return source.Contains(value) switch
{
false => source,
_ => source.Remove(value).Add(value)
};
}
/// <summary>
/// Replace <paramref name="value"/> in <paramref name="source"/> based on
/// <paramref name="equalityComparer"/> equality
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="source"></param>
/// <param name="value"></param>
/// <param name="equalityComparer"></param>
/// <returns></returns>
public static ImmutableList<T> Replace<T>(this ImmutableList<T> source, T value, IEqualityComparer<T> equalityComparer)
{
return source.Contains(value, equalityComparer) switch
{
false => source,
_ => source.Remove(value, equalityComparer).Add(value)
};
}
}

...

Ability to define mapping from domain (semantic) type to technical types (syntaxes) with ability to map multiple architectures" per semantic type (eg: C#, open api, json, etc.)

public class ImmutableListExtensionsShould
{
[Fact]
public void WorkWithObjectEquals()
{
ImmutableList<ComplexType> collection = new List<ComplexType>([new ComplexType("Color", 1.0)]).ToImmutableList();
Assert.Single(collection);
collection = collection.Replace(new ComplexType("Region", 2));
var element = Assert.Single(collection);
Assert.Equal("Color", element.Name);
Assert.Equal(1.0, element.Weight);
collection = collection.Replace(new ComplexType("Color", 2));
element = Assert.Single(collection);
Assert.Equal("Color", element.Name);
Assert.Equal(2.0, element.Weight);
}
[Fact]
public void WorkWithEqualityComparer()
{
ImmutableList<ComplexType> collection = new List<ComplexType>([new ComplexType("Color", 1.0)]).ToImmutableList();
Assert.Single(collection);
collection = collection.Replace(new ComplexType("Region", 2), ComplexTypeNameEqualityComparer.Instance);
var element = Assert.Single(collection);
Assert.Equal("Color", element.Name);
Assert.Equal(1.0, element.Weight);
collection = collection.Replace(new ComplexType("Color", 2), ComplexTypeNameEqualityComparer.Instance);
element = Assert.Single(collection);
Assert.Equal("Color", element.Name);
Assert.Equal(2.0, element.Weight);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment