Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Step extension #788

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions MoreLinq.Test/StepTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#region License and Terms
// MoreLINQ - Extensions to LINQ to Objects
// Copyright (c) 2021 Atif Aziz. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion

namespace MoreLinq.Test
{
using System.Collections.Generic;
using NUnit.Framework;

[TestFixture]
public class StepTest
{
static readonly IEnumerable<TestCaseData> TestData = new []
{
new TestCaseData(new int[0], new[] { 1, 1 })
{
ExpectedResult = new(bool, int)[0]
},
new TestCaseData(new int[0], new[] { 0, 0 })
{
ExpectedResult = new(bool, int)[0]
},
new TestCaseData(new[] { 42 }, new[] { 0, 0, 1 })
{
ExpectedResult = new[] { (42, 1), (42, 0), (42, 0) }
},
new TestCaseData(new[] { 1, 2, 3, 4, 5 }, new[] { 1, 1, 1, 1, 1 })
{
ExpectedResult = new[] { (1, 1), (2, 1), (3, 1), (4, 1), (5, 1) }
},
new TestCaseData(new[] { 1, 2, 3, 4, 5 }, new[] { 2, 2, 2, 2, 2 })
{
ExpectedResult = new[] { (1, 1), (3, 2), (5, 2) }
},
new TestCaseData(new[] { 1, 2, 3, 4, 5 }, new[] { 10 })
{
ExpectedResult = new[] { (1, 1) }
},
new TestCaseData(new[] { 1, 2, 3, 4, 5 }, new[] { 1, 1 })
{
ExpectedResult = new[] { (1, 1), (2, 1), (3, 1) }
},
new TestCaseData(new[] { 1, 2, 3 }, new[] { 1, 1, 1, 1, 1 })
{
ExpectedResult = new[] { (1, 1), (2, 1), (3, 1) }
},
new TestCaseData(new[] { 42 }, new[] { 0, 0, 0 })
{
ExpectedResult = new[] { (42, 1), (42, 0), (42, 0), (42, 0) }
},
new TestCaseData(new[] { 1, 2, 3 }, new[] { 1, 0, 1, 0, 1, 0 })
{
ExpectedResult = new[] { (1, 1), (2, 1), (2, 0), (3, 1), (3, 0) }
},
new TestCaseData(new[] { 1, 2, 3 }, new[] { 1, -1, 1, -1, 1, -1 })
{
ExpectedResult = new[] { (1, 1), (2, 1), (2, 0), (3, 1), (3, 0) }
}
};

[TestCaseSource(nameof(TestData))]
public object Step(int[] xs, int[] steps)
{
using var source = xs.AsTestingSequence();
using var ts = steps.AsTestingSequence();
return source.Step(ts).ToArray();
}
}
}
26 changes: 26 additions & 0 deletions MoreLinq/Extensions.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5567,6 +5567,32 @@ public static bool StartsWith<T>(this IEnumerable<T> first, IEnumerable<T> secon

}

/// <summary><c>Step</c> extension.</summary>

[GeneratedCode("MoreLinq.ExtensionsGenerator", "1.0.0.0")]
public static partial class StepExtension
{
/// <summary>
/// Steps through a sequence based on another sequence of zero or
/// positive steps to take between elements.
/// </summary>
/// <typeparam name="T">The type of elements in <paramref name="source"/>.</typeparam>
/// <param name="source">The source sequence.</param>
/// <param name="steps">
/// Sequence of zero or positive steps to take where a negative step is
/// treated the same as zero.</param>
/// <returns>
/// A sequence of items from <paramref name="source"/> paired with
/// steps from <paramref name="steps"/>.
/// </returns>
/// <remarks>This operator uses deferred execution and streams its results.</remarks>

public static IEnumerable<(T Item, int Step)>
Step<T>(this IEnumerable<T> source, IEnumerable<int> steps)
=> MoreEnumerable. Step(source, steps);

}

/// <summary><c>Subsets</c> extension.</summary>

[GeneratedCode("MoreLinq.ExtensionsGenerator", "1.0.0.0")]
Expand Down
74 changes: 74 additions & 0 deletions MoreLinq/Step.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#region License and Terms
// MoreLINQ - Extensions to LINQ to Objects
// Copyright (c) 2021 Atif Aziz. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion

namespace MoreLinq
{
using System;
using System.Collections.Generic;

partial class MoreEnumerable
{
/// <summary>
/// Steps through a sequence based on another sequence of zero or
/// positive steps to take between elements.
/// </summary>
/// <typeparam name="T">The type of elements in <paramref name="source"/>.</typeparam>
/// <param name="source">The source sequence.</param>
/// <param name="steps">
/// Sequence of zero or positive steps to take where a negative step is
/// treated the same as zero.</param>
/// <returns>
/// A sequence of items from <paramref name="source"/> paired with
/// steps from <paramref name="steps"/>.
/// </returns>
/// <remarks>This operator uses deferred execution and streams its results.</remarks>

public static IEnumerable<(T Item, int Step)>
Step<T>(this IEnumerable<T> source, IEnumerable<int> steps)
{
if (source is null) throw new ArgumentNullException(nameof(source));
if (steps is null) throw new ArgumentNullException(nameof(steps));

return _(); IEnumerable<(T, int)> _()
{
using var item = source.GetEnumerator();

if (!item.MoveNext())
yield break;
yield return (item.Current, 1);

foreach (var step in steps)
{
if (step > 0)
{
for (var i = 0; i < step; i++)
{
if (!item.MoveNext())
yield break;
}

yield return (item.Current, step);
}
else
{
yield return (item.Current, 0);
}
}
}
}
}
}