Table of Contents

Struct Mutation<T>

Namespace
LfrlAnvil.Functional
Assembly
LfrlAnvil.Functional.dll

Represents a generic tuple of (old, new) values.

public readonly struct Mutation<T> : IEquatable<Mutation<T>>

Type Parameters

T

Value type.

Implements
Inherited Members
Extension Methods

Constructors

Mutation(T, T)

Creates a new Mutation<T> instance.

public Mutation(T oldValue, T value)

Parameters

oldValue T

Old value.

value T

New value.

Fields

Empty

Represents an empty, unchanged mutation.

public static readonly Mutation<T> Empty

Field Value

Mutation<T>

Properties

HasChanged

Specifies whether or not the OldValue is different than the Value, using the Default comparer.

public bool HasChanged { get; }

Property Value

bool

OldValue

Old value.

public T OldValue { get; }

Property Value

T

Value

New value.

public T Value { get; }

Property Value

T

Methods

Bind<T2>(Func<(T OldValue, T Value), Mutation<T2>>)

Returns the result of the provided changed delegate invocation when HasChanged is equal to true, otherwise returns Empty.

[Pure]
public Mutation<T2> Bind<T2>(Func<(T OldValue, T Value), Mutation<T2>> changed)

Parameters

changed Func<(T OldValue, T Value), Mutation<T2>>

Delegate to invoke when HasChanged is equal to true.

Returns

Mutation<T2>

New Mutation<T> instance.

Type Parameters

T2

Result type.

Bind<T2>(Func<(T OldValue, T Value), Mutation<T2>>, Func<T, Mutation<T2>>)

Returns the result of the provided changed delegate invocation when HasChanged is equal to true, otherwise returns the result of the provided unchanged delegate invocation.

[Pure]
public Mutation<T2> Bind<T2>(Func<(T OldValue, T Value), Mutation<T2>> changed, Func<T, Mutation<T2>> unchanged)

Parameters

changed Func<(T OldValue, T Value), Mutation<T2>>

Delegate to invoke when HasChanged is equal to true.

unchanged Func<T, Mutation<T2>>

Delegate to invoke when HasChanged is equal to false.

Returns

Mutation<T2>

New Mutation<T> instance.

Type Parameters

T2

Result type.

Equals(Mutation<T>)

Indicates whether the current object is equal to another object of the same type.

[Pure]
public bool Equals(Mutation<T> other)

Parameters

other Mutation<T>

An object to compare with this object.

Returns

bool

true if the current object is equal to the other parameter; otherwise, false.

Equals(object?)

Indicates whether this instance and a specified object are equal.

[Pure]
public override bool Equals(object? obj)

Parameters

obj object

The object to compare with the current instance.

Returns

bool

true if obj and this instance are the same type and represent the same value; otherwise, false.

GetHashCode()

Returns the hash code for this instance.

[Pure]
public override int GetHashCode()

Returns

int

A 32-bit signed integer that is the hash code for this instance.

IfChanged(Action<(T OldValue, T Value)>)

Invokes the provided changed delegate when HasChanged is equal to true, otherwise does nothing.

public Nil IfChanged(Action<(T OldValue, T Value)> changed)

Parameters

changed Action<(T OldValue, T Value)>

Delegate to invoke when HasChanged is equal to true.

Returns

Nil

Nil.

IfChangedOrDefault<T2>(Func<(T OldValue, T Value), T2>)

Returns the result of the provided changed delegate invocation when HasChanged is equal to true, otherwise returns a default value.

[Pure]
public T2? IfChangedOrDefault<T2>(Func<(T OldValue, T Value), T2> changed)

Parameters

changed Func<(T OldValue, T Value), T2>

Delegate to invoke when HasChanged is equal to true.

Returns

T2

Delegate invocation result or a default value.

Type Parameters

T2

Result type.

IfChangedOrDefault<T2>(Func<(T OldValue, T Value), T2>, T2)

Returns the result of the provided changed delegate invocation when HasChanged is equal to true, otherwise returns a default value.

[Pure]
public T2 IfChangedOrDefault<T2>(Func<(T OldValue, T Value), T2> changed, T2 defaultValue)

Parameters

changed Func<(T OldValue, T Value), T2>

Delegate to invoke when HasChanged is equal to true.

defaultValue T2

Value to return HasChanged is equal to false.

Returns

T2

Delegate invocation result or a default value.

Type Parameters

T2

Result type.

IfChanged<T2>(Func<(T OldValue, T Value), T2?>)

Returns the result of the provided changed delegate invocation when HasChanged is equal to true, otherwise returns None.

[Pure]
public Maybe<T2> IfChanged<T2>(Func<(T OldValue, T Value), T2?> changed) where T2 : notnull

Parameters

changed Func<(T OldValue, T Value), T2>

Delegate to invoke when HasChanged is equal to true.

Returns

Maybe<T2>

New Maybe<T> instance.

Type Parameters

T2

Result type.

IfUnchanged(Action<T>)

Invokes the provided unchanged delegate when HasChanged is equal to false, otherwise does nothing.

public Nil IfUnchanged(Action<T> unchanged)

Parameters

unchanged Action<T>

Delegate to invoke when HasChanged is equal to false.

Returns

Nil

Nil.

IfUnchangedOrDefault<T2>(Func<T, T2>)

Returns the result of the provided unchanged delegate invocation when HasChanged is equal to false, otherwise returns a default value.

[Pure]
public T2? IfUnchangedOrDefault<T2>(Func<T, T2> unchanged)

Parameters

unchanged Func<T, T2>

Delegate to invoke when HasChanged is equal to false.

Returns

T2

Delegate invocation result or a default value.

Type Parameters

T2

Result type.

IfUnchangedOrDefault<T2>(Func<T, T2>, T2)

Returns the result of the provided unchanged delegate invocation when HasChanged is equal to false, otherwise returns a default value.

[Pure]
public T2 IfUnchangedOrDefault<T2>(Func<T, T2> unchanged, T2 defaultValue)

Parameters

unchanged Func<T, T2>

Delegate to invoke when HasChanged is equal to false.

defaultValue T2

Value to return HasChanged is equal to true.

Returns

T2

Delegate invocation result or a default value.

Type Parameters

T2

Result type.

IfUnchanged<T2>(Func<T, T2?>)

Returns the result of the provided unchanged delegate invocation when HasChanged is equal to false, otherwise returns None.

[Pure]
public Maybe<T2> IfUnchanged<T2>(Func<T, T2?> unchanged) where T2 : notnull

Parameters

unchanged Func<T, T2>

Delegate to invoke when HasChanged is equal to false.

Returns

Maybe<T2>

New Maybe<T> instance.

Type Parameters

T2

Result type.

Match(Action<(T OldValue, T Value)>, Action<T>)

Invokes the provided changed delegate when HasChanged is equal to true, otherwise invokes the provided unchanged delegate.

public Nil Match(Action<(T OldValue, T Value)> changed, Action<T> unchanged)

Parameters

changed Action<(T OldValue, T Value)>

Delegate to invoke when HasChanged is equal to true.

unchanged Action<T>

Delegate to invoke when HasChanged is equal to false.

Returns

Nil

Nil.

Match<T2>(Func<(T OldValue, T Value), T2>, Func<T, T2>)

Returns the result of the provided changed delegate invocation when HasChanged is equal to true, otherwise returns the result of the provided unchanged delegate invocation.

[Pure]
public T2 Match<T2>(Func<(T OldValue, T Value), T2> changed, Func<T, T2> unchanged)

Parameters

changed Func<(T OldValue, T Value), T2>

Delegate to invoke when HasChanged is equal to true.

unchanged Func<T, T2>

Delegate to invoke when HasChanged is equal to false.

Returns

T2

Delegate invocation result.

Type Parameters

T2

Result type.

Mutate(T)

Creates a new Mutation<T> instance.

[Pure]
public Mutation<T> Mutate(T newValue)

Parameters

newValue T

New value.

Returns

Mutation<T>

New Mutation<T> instance with Value as OldValue.

Replace(T)

Creates a new Mutation<T> instance.

[Pure]
public Mutation<T> Replace(T newValue)

Parameters

newValue T

New value.

Returns

Mutation<T>

New Mutation<T> instance with unchanged OldValue.

Revert()

Creates a new Mutation<T> instance.

[Pure]
public Mutation<T> Revert()

Returns

Mutation<T>

New Mutation<T> instance with OldValue as Value.

Swap()

Creates a new Mutation<T> instance.

[Pure]
public Mutation<T> Swap()

Returns

Mutation<T>

New Mutation<T> instance with OldValue and Value swapped.

ToString()

Returns a string representation of this Mutation<T> instance.

[Pure]
public override string ToString()

Returns

string

String representation.

Operators

operator ==(Mutation<T>, Mutation<T>)

Checks if a is equal to b.

[Pure]
public static bool operator ==(Mutation<T> a, Mutation<T> b)

Parameters

a Mutation<T>

First operand.

b Mutation<T>

Second operand.

Returns

bool

true when operands are equal, otherwise false.

implicit operator T(Mutation<T>)

Gets the underlying value.

[Pure]
public static implicit operator T(Mutation<T> source)

Parameters

source Mutation<T>

Returns

T

Value.

implicit operator Mutation<T>(Nil)

Converts Nil to a Mutation<T> instance.

[Pure]
public static implicit operator Mutation<T>(Nil value)

Parameters

value Nil

Value to convert.

Returns

Mutation<T>

Empty.

operator !=(Mutation<T>, Mutation<T>)

Checks if a is not equal to b.

[Pure]
public static bool operator !=(Mutation<T> a, Mutation<T> b)

Parameters

a Mutation<T>

First operand.

b Mutation<T>

Second operand.

Returns

bool

true when operands are not equal, otherwise false.