Skip to content

Commit

Permalink
Revert removal of "Append" and "Prepend"
Browse files Browse the repository at this point in the history
This reverts commits:

- Remove unused "Append"/"Prepend"supporting types:
  b9316f7

- Remove "Append" and "Prepend":
  ca3a8e2
  • Loading branch information
atifaziz committed Jun 25, 2023
1 parent b9316f7 commit cf55bab
Show file tree
Hide file tree
Showing 11 changed files with 534 additions and 24 deletions.
96 changes: 96 additions & 0 deletions MoreLinq.Test/AppendTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#region License and Terms
// MoreLINQ - Extensions to LINQ to Objects
// Copyright (c) 2008 Jonathan Skeet. 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

#if !NET471_OR_GREATER && !NETSTANDARD1_6_OR_GREATER && !NETCOREAPP2_0_OR_GREATER

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

[TestFixture]
public class AppendTest
{
#region Append with single head and tail sequence
[Test]
public void AppendWithNonEmptyHeadSequence()
{
var head = new[] { "first", "second" };
var tail = "third";
var whole = head.Append(tail);
whole.AssertSequenceEqual("first", "second", "third");
}

[Test]
public void AppendWithEmptyHeadSequence()
{
string[] head = { };
var tail = "first";
var whole = head.Append(tail);
whole.AssertSequenceEqual("first");
}

[Test]
public void AppendWithNullTail()
{
var head = new[] { "first", "second" };
string? tail = null;
var whole = head.Append(tail);
whole.AssertSequenceEqual("first", "second", null);
}

[Test]
public void AppendIsLazyInHeadSequence()
{
_ = new BreakingSequence<string>().Append("tail");
}
#endregion

[TestCaseSource(nameof(ContactManySource))]
public void AppendMany(int[] head, int[] tail)
{
tail.Aggregate(head.AsEnumerable(), (xs, x) => xs.Append(x))
.AssertSequenceEqual(head.Concat(tail));
}

public static IEnumerable<object> ContactManySource =>
from x in Enumerable.Range(0, 11)
from y in Enumerable.Range(1, 20 - x)
select new
{
Head = Enumerable.Range(1, x).ToArray(),
Tail = Enumerable.Range(x + 1, y).ToArray(),
}
into e
select new TestCaseData(e.Head,
e.Tail).SetName("Head = [" + string.Join(", ", e.Head) + "], " +
"Tail = [" + string.Join(", ", e.Tail) + "]");

[Test]
public void AppendWithSharedSource()
{
var first = new[] { 1 }.Append(2);
var second = first.Append(3).Append(4);
var third = first.Append(4).Append(8);

second.AssertSequenceEqual(1, 2, 3, 4);
third.AssertSequenceEqual(1, 2, 4, 8);
}
}
}

#endif
95 changes: 95 additions & 0 deletions MoreLinq.Test/PrependTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#region License and Terms
// MoreLINQ - Extensions to LINQ to Objects
// Copyright (c) 2008 Jonathan Skeet. 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

#if !NET471_OR_GREATER && !NETSTANDARD1_6_OR_GREATER && !NETCOREAPP2_0_OR_GREATER

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

[TestFixture]
public class PrependTest
{
[Test]
public void PrependWithNonEmptyTailSequence()
{
string[] tail = { "second", "third" };
var head = "first";
var whole = tail.Prepend(head);
whole.AssertSequenceEqual("first", "second", "third");
}

[Test]
public void PrependWithEmptyTailSequence()
{
string[] tail = { };
var head = "first";
var whole = tail.Prepend(head);
whole.AssertSequenceEqual("first");
}

[Test]
public void PrependWithNullHead()
{
string[] tail = { "second", "third" };
string? head = null;
var whole = tail.Prepend(head);
whole.AssertSequenceEqual(null, "second", "third");
}

[Test]
public void PrependIsLazyInTailSequence()
{
_ = new BreakingSequence<string>().Prepend("head");
}

[TestCaseSource(nameof(PrependManySource))]
public int[] PrependMany(int[] head, int[] tail)
{
return tail.Aggregate(head.AsEnumerable(), MoreEnumerable.Prepend).ToArray();
}

public static IEnumerable<ITestCaseData> PrependManySource =>
from x in Enumerable.Range(0, 11)
from y in Enumerable.Range(1, 11)
select new
{
Head = Enumerable.Range(0, y).Select(n => 0 - n).ToArray(),
Tail = Enumerable.Range(1, x).ToArray(),
}
into e
select new TestCaseData(e.Head, e.Tail)
.SetName("Head = [" + string.Join(", ", e.Head) + "], " +
"Tail = [" + string.Join(", ", e.Tail) + "]")
.Returns(e.Tail.Reverse().Concat(e.Head).ToArray());

[Test]
public void PrependWithSharedSource()
{
var first = new[] { 1 }.Prepend(2);
var second = first.Prepend(3).Prepend(4);
var third = first.Prepend(4).Prepend(8);

second.AssertSequenceEqual(4, 3, 2, 1);
third.AssertSequenceEqual(8, 4, 2, 1);
}
}
}

#endif
46 changes: 46 additions & 0 deletions MoreLinq/Append.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#region License and Terms
// MoreLINQ - Extensions to LINQ to Objects
// Copyright (c) 2008 Jonathan Skeet. 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;

static partial class MoreEnumerable
{
/// <summary>
/// Returns a sequence consisting of the head elements and the given tail element.
/// </summary>
/// <typeparam name="T">Type of sequence</typeparam>
/// <param name="head">All elements of the head. Must not be null.</param>
/// <param name="tail">Tail element of the new sequence.</param>
/// <returns>A sequence consisting of the head elements and the given tail element.</returns>
/// <remarks>This operator uses deferred execution and streams its results.</remarks>

#if NET471_OR_GREATER || NETSTANDARD1_6_OR_GREATER || NETCOREAPP2_0_OR_GREATER
public static IEnumerable<T> Append<T>(IEnumerable<T> head, T tail)
#else
public static IEnumerable<T> Append<T>(this IEnumerable<T> head, T tail)
#endif
{
if (head == null) throw new ArgumentNullException(nameof(head));
return head is PendNode<T> node
? node.Concat(tail)
: PendNode<T>.WithSource(head).Concat(tail);
}
}
}
48 changes: 48 additions & 0 deletions MoreLinq/Extensions.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,25 @@ public static TResult AggregateRight<TSource, TAccumulate, TResult>(this IEnumer

}

/// <summary><c>Append</c> extension.</summary>

[GeneratedCode("MoreLinq.ExtensionsGenerator", "1.0.0.0")]
public static partial class AppendExtension
{
/// <summary>
/// Returns a sequence consisting of the head elements and the given tail element.
/// </summary>
/// <typeparam name="T">Type of sequence</typeparam>
/// <param name="head">All elements of the head. Must not be null.</param>
/// <param name="tail">Tail element of the new sequence.</param>
/// <returns>A sequence consisting of the head elements and the given tail element.</returns>
/// <remarks>This operator uses deferred execution and streams its results.</remarks>

public static IEnumerable<T> Append<T>(this IEnumerable<T> head, T tail)
=> MoreEnumerable.Append(head, tail);

}

/// <summary><c>Assert</c> extension.</summary>

[GeneratedCode("MoreLinq.ExtensionsGenerator", "1.0.0.0")]
Expand Down Expand Up @@ -4540,6 +4559,35 @@ public static IEnumerable<T> Pipe<T>(this IEnumerable<T> source, Action<T> actio

}

/// <summary><c>Prepend</c> extension.</summary>

[GeneratedCode("MoreLinq.ExtensionsGenerator", "1.0.0.0")]
public static partial class PrependExtension
{
/// <summary>
/// Prepends a single value to a sequence.
/// </summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam>
/// <param name="source">The sequence to prepend to.</param>
/// <param name="value">The value to prepend.</param>
/// <returns>
/// Returns a sequence where a value is prepended to it.
/// </returns>
/// <remarks>
/// This operator uses deferred execution and streams its results.
/// </remarks>
/// <code><![CDATA[
/// int[] numbers = { 1, 2, 3 };
/// var result = numbers.Prepend(0);
/// ]]></code>
/// The <c>result</c> variable, when iterated over, will yield
/// 0, 1, 2 and 3, in turn.

public static IEnumerable<TSource> Prepend<TSource>(this IEnumerable<TSource> source, TSource value)
=> MoreEnumerable.Prepend(source, value);

}

/// <summary><c>PreScan</c> extension.</summary>

[GeneratedCode("MoreLinq.ExtensionsGenerator", "1.0.0.0")]
Expand Down
Loading

0 comments on commit cf55bab

Please sign in to comment.