Skip to content

Commit

Permalink
Clean
Browse files Browse the repository at this point in the history
  • Loading branch information
zsrdjan committed May 17, 2024
1 parent ae3ba18 commit a93532f
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 68 deletions.
68 changes: 8 additions & 60 deletions src/Fraktalio.FModel/InternalDecider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,17 @@ internal InternalDecider(Func<C, Si, IEnumerable<Eo>> decide,
/// <summary>
/// A function/lambda that takes command of type [C] and input state of type [Si] as parameters, and returns/emits the list of output events
/// </summary>
public Func<C, Si, IEnumerable<Eo>> Decide { get; }
internal Func<C, Si, IEnumerable<Eo>> Decide { get; }

/// <summary>
/// A function/lambda that takes input state of type [Si] and input event of type [Ei] as parameters, and returns the output/new state [So]
/// </summary>
public Func<Si, Ei, So> Evolve { get; }
internal Func<Si, Ei, So> Evolve { get; }

/// <summary>
/// A starting point / An initial state of type [So]
/// </summary>
public So InitialState { get; }
internal So InitialState { get; }

/// <summary>
/// Left map on C/Command parameter - Contravariant
Expand All @@ -73,7 +73,7 @@ internal InternalDecider<Cn, Si, So, Ei, Eo> MapLeftOnCommand<Cn>(Func<Cn, C> f)
/// <typeparam name="Ein">Event input new</typeparam>
/// <typeparam name="Eon">Event output new</typeparam>
/// <returns></returns>
public InternalDecider<C, Si, So, Ein, Eon> DimapOnEvent<Ein, Eon>(Func<Ein, Ei> fl, Func<Eo, Eon> fr) =>
internal InternalDecider<C, Si, So, Ein, Eon> DimapOnEvent<Ein, Eon>(Func<Ein, Ei> fl, Func<Eo, Eon> fr) =>
new(
(c, si) => Decide(c, si).Select(fr),
(si, ein) => Evolve(si, fl(ein)),
Expand All @@ -86,7 +86,7 @@ public InternalDecider<C, Si, So, Ein, Eon> DimapOnEvent<Ein, Eon>(Func<Ein, Ei>
/// <param name="f"></param>
/// <typeparam name="Ein"></typeparam>
/// <returns></returns>
public InternalDecider<C, Si, So, Ein, Eo> MapLeftOnEvent<Ein>(Func<Ein, Ei> f) =>
internal InternalDecider<C, Si, So, Ein, Eo> MapLeftOnEvent<Ein>(Func<Ein, Ei> f) =>
DimapOnEvent<Ein, Eo>(f, eo => eo);

/// <summary>
Expand All @@ -95,7 +95,7 @@ public InternalDecider<C, Si, So, Ein, Eo> MapLeftOnEvent<Ein>(Func<Ein, Ei> f)
/// <param name="f"></param>
/// <typeparam name="Eon"></typeparam>
/// <returns></returns>
public InternalDecider<C, Si, So, Ei, Eon> MapOnEvent<Eon>(Func<Eo, Eon> f) => DimapOnEvent<Ei, Eon>(ei => ei, f);
internal InternalDecider<C, Si, So, Ei, Eon> MapOnEvent<Eon>(Func<Eo, Eon> f) => DimapOnEvent<Ei, Eon>(ei => ei, f);

/// <summary>
/// Dimap on S/State parameter - Contravariant on input state (Si) and Covariant on output state (So) = Profunctor
Expand All @@ -105,7 +105,7 @@ public InternalDecider<C, Si, So, Ein, Eo> MapLeftOnEvent<Ein>(Func<Ein, Ei> f)
/// <typeparam name="Sin">State input new</typeparam>
/// <typeparam name="Son">State output new</typeparam>
/// <returns></returns>
public InternalDecider<C, Sin, Son, Ei, Eo> DimapOnState<Sin, Son>(Func<Sin, Si> fl, Func<So, Son> fr) =>
internal InternalDecider<C, Sin, Son, Ei, Eo> DimapOnState<Sin, Son>(Func<Sin, Si> fl, Func<So, Son> fr) =>
new(
(c, sin) => Decide(c, fl(sin)),
(sin, ei) => fr(Evolve(fl(sin), ei)),
Expand All @@ -118,7 +118,7 @@ public InternalDecider<C, Sin, Son, Ei, Eo> DimapOnState<Sin, Son>(Func<Sin, Si>
/// <param name="f"></param>
/// <typeparam name="Sin">Sin State input new</typeparam>
/// <returns></returns>
public InternalDecider<C, Sin, So, Ei, Eo> MapLeftOnState<Sin>(Func<Sin, Si> f) =>
internal InternalDecider<C, Sin, So, Ei, Eo> MapLeftOnState<Sin>(Func<Sin, Si> f) =>
DimapOnState<Sin, So>(f, so => so);

/// <summary>
Expand Down Expand Up @@ -151,56 +151,4 @@ private InternalDecider<C, Si, Son, Ei, Eo> ApplyOnState<Son>(InternalDecider<C,
internal InternalDecider<C, Si, Tuple<So, Son>, Ei, Eo>
ProductOnState<Son>(InternalDecider<C, Si, Son, Ei, Eo> fb) =>
ApplyOnState(fb.MapOnState(b => new Func<So, Tuple<So, Son>>(a => new Tuple<So, Son>(a, b))));
}

internal static class InternalDeciderExtensions
{
/// <summary>
/// Combine [InternalDecider]s into one big [InternalDecider]
///
/// Possible to use when:
/// - [Ei] and [Ei2] have common superclass [Ei_SUPER]
/// - [Eo] and [Eo2] have common superclass [Eo_SUPER]
/// - [C] and [C2] have common superclass [C_SUPER]
/// </summary>
/// <param name="x">First Decider</param>
/// <param name="y">Second Decider</param>
/// <typeparam name="C">Command type of the first Decider</typeparam>
/// <typeparam name="Si">Input_State type of the first Decider</typeparam>
/// <typeparam name="So">Output_State type of the first Decider</typeparam>
/// <typeparam name="Ei">Input_Event type of the first Decider</typeparam>
/// <typeparam name="Eo">Output_Event type of the first Decider</typeparam>
/// <typeparam name="C2">Command type of the second Decider</typeparam>
/// <typeparam name="Si2">Input_State type of the second Decider</typeparam>
/// <typeparam name="So2">Output_State type of the second Decider</typeparam>
/// <typeparam name="Ei2">Input_Event type of the second Decider</typeparam>
/// <typeparam name="Eo2">Output_Event type of the second Decider</typeparam>
/// <typeparam name="C_SUPER">super type of the command types C and C2</typeparam>
/// <typeparam name="Ei_SUPER">Super type of the Ei and Ei2 types</typeparam>
/// <typeparam name="Eo_SUPER">super type of the Eo and Eo2 types</typeparam>
/// <returns></returns>
public static InternalDecider<C_SUPER?, Tuple<Si, Si2>, Tuple<So, So2>, Ei_SUPER, Eo_SUPER?> Combine<C, Si, So, Ei,
Eo, C2, Si2, So2, Ei2, Eo2, C_SUPER, Ei_SUPER, Eo_SUPER>(
this InternalDecider<C?, Si, So, Ei?, Eo?> x,
InternalDecider<C2?, Si2, So2, Ei2?, Eo2?> y)
where C : class?, C_SUPER?
where C2 : class, C_SUPER
where Ei : class?, Ei_SUPER?
where Eo : Eo_SUPER
where Ei2 : class, Ei_SUPER
where Eo2 : Eo_SUPER
{
var deciderX = x.MapLeftOnCommand<C_SUPER?>(c => c as C)
.MapLeftOnState<Tuple<Si, Si2>>(pair => pair.Item1)
.DimapOnEvent<Ei_SUPER, Eo_SUPER?>(ei => ei as Ei, eo => eo);

var deciderY = y.MapLeftOnCommand<C_SUPER?>(c => c as C2)
.MapLeftOnState<Tuple<Si, Si2>>(pair => pair.Item2)
.DimapOnEvent<Ei_SUPER, Eo_SUPER?>(ei => ei as Ei2, eo => eo);

return deciderX.ProductOnState(deciderY);
}

internal static Decider<C, S, E> AsDecider<C, S, E>(this InternalDecider<C, S, S, E, E> internalDecider) =>
new(internalDecider.Decide, internalDecider.Evolve, internalDecider.InitialState);
}
53 changes: 53 additions & 0 deletions src/Fraktalio.FModel/InternalDeciderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
namespace Fraktalio.FModel;

internal static class InternalDeciderExtensions
{
/// <summary>
/// Combine [InternalDecider]s into one big [InternalDecider]
///
/// Possible to use when:
/// - [Ei] and [Ei2] have common superclass [Ei_SUPER]
/// - [Eo] and [Eo2] have common superclass [Eo_SUPER]
/// - [C] and [C2] have common superclass [C_SUPER]
/// </summary>
/// <param name="x">First Decider</param>
/// <param name="y">Second Decider</param>
/// <typeparam name="C">Command type of the first Decider</typeparam>
/// <typeparam name="Si">Input_State type of the first Decider</typeparam>
/// <typeparam name="So">Output_State type of the first Decider</typeparam>
/// <typeparam name="Ei">Input_Event type of the first Decider</typeparam>
/// <typeparam name="Eo">Output_Event type of the first Decider</typeparam>
/// <typeparam name="C2">Command type of the second Decider</typeparam>
/// <typeparam name="Si2">Input_State type of the second Decider</typeparam>
/// <typeparam name="So2">Output_State type of the second Decider</typeparam>
/// <typeparam name="Ei2">Input_Event type of the second Decider</typeparam>
/// <typeparam name="Eo2">Output_Event type of the second Decider</typeparam>
/// <typeparam name="C_SUPER">super type of the command types C and C2</typeparam>
/// <typeparam name="Ei_SUPER">Super type of the Ei and Ei2 types</typeparam>
/// <typeparam name="Eo_SUPER">super type of the Eo and Eo2 types</typeparam>
/// <returns></returns>
internal static InternalDecider<C_SUPER?, Tuple<Si, Si2>, Tuple<So, So2>, Ei_SUPER, Eo_SUPER?> Combine<C, Si, So, Ei,
Eo, C2, Si2, So2, Ei2, Eo2, C_SUPER, Ei_SUPER, Eo_SUPER>(
this InternalDecider<C?, Si, So, Ei?, Eo?> x,
InternalDecider<C2?, Si2, So2, Ei2?, Eo2?> y)
where C : class?, C_SUPER?
where C2 : class, C_SUPER
where Ei : class?, Ei_SUPER?
where Eo : Eo_SUPER
where Ei2 : class, Ei_SUPER
where Eo2 : Eo_SUPER
{
var deciderX = x.MapLeftOnCommand<C_SUPER?>(c => c as C)
.MapLeftOnState<Tuple<Si, Si2>>(pair => pair.Item1)
.DimapOnEvent<Ei_SUPER, Eo_SUPER?>(ei => ei as Ei, eo => eo);

var deciderY = y.MapLeftOnCommand<C_SUPER?>(c => c as C2)
.MapLeftOnState<Tuple<Si, Si2>>(pair => pair.Item2)
.DimapOnEvent<Ei_SUPER, Eo_SUPER?>(ei => ei as Ei2, eo => eo);

return deciderX.ProductOnState(deciderY);
}

internal static Decider<C, S, E> AsDecider<C, S, E>(this InternalDecider<C, S, S, E, E> internalDecider) =>
new(internalDecider.Decide, internalDecider.Evolve, internalDecider.InitialState);
}
1 change: 1 addition & 0 deletions test/Fraktalio.FModel.Tests/EventSourcedDeciderTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Fraktalio.FModel.Tests.Examples.Numbers;
using Fraktalio.FModel.Tests.Examples.Numbers.Even;
using Fraktalio.FModel.Tests.Examples.Numbers.Odd;
using Fraktalio.FModel.Tests.Extensions;
using EvenNumberAdded = Fraktalio.FModel.Tests.Examples.Numbers.NumberEvent.EvenNumberEvent.EvenNumberAdded;
using EvenNumberSubtracted = Fraktalio.FModel.Tests.Examples.Numbers.NumberEvent.EvenNumberEvent.EvenNumberSubtracted;
using OddNumberAdded = Fraktalio.FModel.Tests.Examples.Numbers.NumberEvent.OddNumberEvent.OddNumberAdded;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace Fraktalio.FModel.Tests.Examples.Numbers;

public sealed record EvenNumberState(Description Description, Number Value) : NumberState(Description, Value);
6 changes: 1 addition & 5 deletions test/Fraktalio.FModel.Tests/Examples/Numbers/NumberState.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
namespace Fraktalio.FModel.Tests.Examples.Numbers;

public abstract record NumberState(Description Description, Number Value);

public sealed record EvenNumberState(Description Description, Number Value) : NumberState(Description, Value);

public sealed record OddNumberState(Description Description, Number Value) : NumberState(Description, Value);
public abstract record NumberState(Description Description, Number Value);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace Fraktalio.FModel.Tests.Examples.Numbers;

public sealed record OddNumberState(Description Description, Number Value) : NumberState(Description, Value);
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Fraktalio.FModel.Contracts;

namespace Fraktalio.FModel.Tests;
namespace Fraktalio.FModel.Tests.Extensions;

public static class DeciderExtensions
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using FluentAssertions;

namespace Fraktalio.FModel.Tests;
namespace Fraktalio.FModel.Tests.Extensions;

public static class EnumerableExtensions
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Fraktalio.FModel.Contracts;

namespace Fraktalio.FModel.Tests;
namespace Fraktalio.FModel.Tests.Extensions;

public static class SagaExtensions
{
Expand Down
1 change: 1 addition & 0 deletions test/Fraktalio.FModel.Tests/SagaTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Fraktalio.FModel.Tests.Examples.Numbers;
using Fraktalio.FModel.Tests.Extensions;
using EvenNumberEvent = Fraktalio.FModel.Tests.Examples.Numbers.NumberEvent.EvenNumberEvent;
using EvenNumberAdded = Fraktalio.FModel.Tests.Examples.Numbers.NumberEvent.EvenNumberEvent.EvenNumberAdded;
using OddNumberEvent = Fraktalio.FModel.Tests.Examples.Numbers.NumberEvent.OddNumberEvent;
Expand Down
1 change: 1 addition & 0 deletions test/Fraktalio.FModel.Tests/StateStoredDeciderTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Fraktalio.FModel.Tests.Examples.Numbers;
using Fraktalio.FModel.Tests.Examples.Numbers.Even;
using Fraktalio.FModel.Tests.Examples.Numbers.Odd;
using Fraktalio.FModel.Tests.Extensions;
using EvenNumberCommand = Fraktalio.FModel.Tests.Examples.Numbers.NumberCommand.EvenNumberCommand;
using OddNumberCommand = Fraktalio.FModel.Tests.Examples.Numbers.NumberCommand.OddNumberCommand;

Expand Down

0 comments on commit a93532f

Please sign in to comment.