I have Monoid and Builder interfaces:
interface IMonoidFor<T>
{
T Zero { get; }
T Append(T a, T b);
}
interface IBuilderFor<T>
{
IBuilderFor<T> Append(T value);
T Result();
}
And want to implement from Monoid to Builder converting algorithm. I tried to do this, and created something like this:
class BuilderMakerFor<T>
{
public IBuilderFor<T> MakeFrom(IMonoidFor<T> monoid)
{
return new Product(monoid);
}
class Product
: IBuilderFor<T>
{
readonly IMonoidFor<T> _monoid;
T _result;
public Product(
IMonoidFor<T> monoid)
{
_monoid = monoid;
_result = _monoid.Zero;
}
public IBuilderFor<T> Append(T value)
{
_result = _monoid.Append(_result, value);
return this;
}
public T Result()
{
return _result;
}
}
}
But in real task, I need Builder that can accumulate items to list of this items, and others Builders with same principle of accumulation. My Builders must realize the next interface:
interface IBuilderFor<TSource, TResult>
{
IBuilderFor<TSource, TResult> Append(TSource value);
TResult Result();
}
And I need to have possibility to create Builders from { zero, append } signature. Like this:
interface X<TSource, TResult>
{
TResult Zero { get; }
TResult Append(TResult a, TSource b);
}
I implemented next BuilderMakerFor for new situation:
class BuilderMakerFor<TSource, TResult>
{
public IBuilderFor<TSource, TResult> MakeFrom(X<TSource, TResult> x)
{
return new Product(x);
}
class Product
: IBuilderFor<TSource, TResult>
{
readonly X<TSource, TResult> _x;
TResult _result;
public Product(
X<TSource, TResult> x)
{
_x = x;
_result = _x.Zero;
}
public IBuilderFor<TSource, TResult> Append(TSource value)
{
_result = _x.Append(_result, value);
return this;
}
public TResult Result()
{
return _result;
}
}
}
All good, except one point: I don't know how to name X.
zero : α
append : α -> α -> α
name is Monoid.
What is
zero : α
append : α -> β -> α
name?
No answer during long period. And I think signature
zero : α
append : α -> β -> α
has no name in math.
I named it AggregatingSource or FoldingSource.