FuncSharp icon indicating copy to clipboard operation
FuncSharp copied to clipboard

Aggregate multiple options

Open KaliCZ opened this issue 4 years ago • 0 comments

Just like Try.Aggregate except with options. if all options have value, return an option with all the values. I would expect 2 overloads:

  • One returning a tuple of all the option values
  • One with a parameter for a function to map the tuple into a TResult.

I would expect overloads for 2 3 4 5 6 options (go up to 9 if you want)

Example signature:

public static IOption<Tuple<T1, T2>> Aggregate<T1, T2>(IOption<T1>option1, IOption<T2>option2)
{
    return option1.Flatmap(v1 => option2.Map(v2 => Tuple.Create(v1, v2)));
}

and

public static IOption<T> Aggregate<T, T1, T2>(IOption<T1>option1, IOption<T2>option2, Func<T1, T2, T> func)
{
    return Aggregate(option1, option2).Map(tuple => func(tuple.Item1, tuple.Item2));
}

KaliCZ avatar Apr 01 '21 12:04 KaliCZ