Skip to content

Commit

Permalink
Fix for the simple unfold and complex unfold migrated to use ValueTuple
Browse files Browse the repository at this point in the history
An extra yield was occuring per iteration
  • Loading branch information
louthy committed Jan 12, 2020
1 parent 8e3f28c commit d0f5eaa
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions LanguageExt.Core/DataTypes/List/Lst.Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1077,7 +1077,6 @@ public static IEnumerable<S> unfold<S>(S state, Func<S, Option<S>> unfolder)
else
{
state = res.Value;
yield return res.Value;
}
}
}
Expand All @@ -1087,12 +1086,13 @@ public static IEnumerable<S> unfold<S>(S state, Func<S, Option<S>> unfolder)
/// state value is threaded through separately to the yielded value.
/// The unfold function generates the items in the resulting list until None is returned.
/// </summary>
/// <typeparam name="A">Bound value of resulting enumerable</typeparam>
/// <typeparam name="S">State type</typeparam>
/// <param name="state">Initial state</param>
/// <param name="unfolder">Unfold function</param>
/// <returns>Unfolded enumerable</returns>
[Pure]
public static IEnumerable<T> unfold<S, T>(S state, Func<S, Option<Tuple<T, S>>> unfolder)
public static IEnumerable<A> unfold<S, A>(S state, Func<S, Option<(A, S)>> unfolder)
{
while (true)
{
Expand All @@ -1114,12 +1114,14 @@ public static IEnumerable<T> unfold<S, T>(S state, Func<S, Option<Tuple<T, S>>>
/// state value is threaded through separately to the yielded value.
/// The unfold function generates the items in the resulting list until None is returned.
/// </summary>
/// <typeparam name="S">State type</typeparam>
/// <typeparam name="A">Bound value of resulting enumerable</typeparam>
/// <typeparam name="S1">State type</typeparam>
/// <typeparam name="S2">State type</typeparam>
/// <param name="state">Initial state</param>
/// <param name="unfolder">Unfold function</param>
/// <returns>Unfolded enumerable</returns>
[Pure]
public static IEnumerable<T> unfold<S1, S2, T>(Tuple<S1, S2> state, Func<S1, S2, Option<Tuple<T, S1, S2>>> unfolder)
public static IEnumerable<A> unfold<S1, S2, A>((S1, S2) state, Func<S1, S2, Option<(A, S1, S2)>> unfolder)
{
while (true)
{
Expand All @@ -1130,7 +1132,7 @@ public static IEnumerable<T> unfold<S1, S2, T>(Tuple<S1, S2> state, Func<S1, S2,
}
else
{
state = System.Tuple.Create(res.Value.Item2, res.Value.Item3);
state = (res.Value.Item2, res.Value.Item3);
yield return res.Value.Item1;
}
}
Expand All @@ -1141,12 +1143,15 @@ public static IEnumerable<T> unfold<S1, S2, T>(Tuple<S1, S2> state, Func<S1, S2,
/// state value is threaded through separately to the yielded value.
/// The unfold function generates the items in the resulting list until None is returned.
/// </summary>
/// <typeparam name="S">State type</typeparam>
/// <typeparam name="A">Bound value of resulting enumerable</typeparam>
/// <typeparam name="S1">State type</typeparam>
/// <typeparam name="S2">State type</typeparam>
/// <typeparam name="S3">State type</typeparam>
/// <param name="state">Initial state</param>
/// <param name="unfolder">Unfold function</param>
/// <returns>Unfolded enumerable</returns>
[Pure]
public static IEnumerable<T> unfold<S1, S2, S3, T>(Tuple<S1, S2, S3> state, Func<S1, S2, S3, Option<Tuple<T, S1, S2, S3>>> unfolder)
public static IEnumerable<A> unfold<S1, S2, S3, A>((S1, S2, S3) state, Func<S1, S2, S3, Option<(A, S1, S2, S3)>> unfolder)
{
while (true)
{
Expand All @@ -1157,7 +1162,7 @@ public static IEnumerable<T> unfold<S1, S2, S3, T>(Tuple<S1, S2, S3> state, Func
}
else
{
state = System.Tuple.Create(res.Value.Item2, res.Value.Item3, res.Value.Item4);
state = (res.Value.Item2, res.Value.Item3, res.Value.Item4);
yield return res.Value.Item1;
}
}
Expand All @@ -1168,12 +1173,16 @@ public static IEnumerable<T> unfold<S1, S2, S3, T>(Tuple<S1, S2, S3> state, Func
/// state value is threaded through separately to the yielded value.
/// The unfold function generates the items in the resulting list until None is returned.
/// </summary>
/// <typeparam name="S">State type</typeparam>
/// <typeparam name="A">Bound value of resulting enumerable</typeparam>
/// <typeparam name="S1">State type</typeparam>
/// <typeparam name="S2">State type</typeparam>
/// <typeparam name="S3">State type</typeparam>
/// <typeparam name="S4">State type</typeparam>
/// <param name="state">Initial state</param>
/// <param name="unfolder">Unfold function</param>
/// <returns>Unfolded enumerable</returns>
[Pure]
public static IEnumerable<T> unfold<S1, S2, S3, S4, T>(Tuple<S1, S2, S3, S4> state, Func<S1, S2, S3, S4, Option<Tuple<T, S1, S2, S3, S4>>> unfolder)
public static IEnumerable<A> unfold<S1, S2, S3, S4, A>((S1, S2, S3, S4) state, Func<S1, S2, S3, S4, Option<(A, S1, S2, S3, S4)>> unfolder)
{
while (true)
{
Expand All @@ -1184,7 +1193,7 @@ public static IEnumerable<T> unfold<S1, S2, S3, S4, T>(Tuple<S1, S2, S3, S4> sta
}
else
{
state = System.Tuple.Create(res.Value.Item2, res.Value.Item3, res.Value.Item4, res.Value.Item5);
state = (res.Value.Item2, res.Value.Item3, res.Value.Item4, res.Value.Item5);
yield return res.Value.Item1;
}
}
Expand Down

0 comments on commit d0f5eaa

Please sign in to comment.