Skip to content

Commit

Permalink
Move TrySingle to Experimental namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
atifaziz committed Dec 13, 2019
1 parent 349a8e6 commit b77df70
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 97 deletions.
8 changes: 4 additions & 4 deletions MoreLinq.Test/TrySingleTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
// limitations under the License.
#endregion

using NUnit.Framework.Interfaces;

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

[TestFixture]
public class TrySingleTest
Expand Down Expand Up @@ -81,7 +81,7 @@ class BreakingSingleElementCollectionBase<T> : IEnumerable<T>
public IEnumerator<T> GetEnumerator()
{
yield return _element;
throw new Exception($"{nameof(MoreEnumerable.TrySingle)} should not have attempted to consume a second element.");
throw new Exception($"{nameof(ExperimentalEnumerable.TrySingle)} should not have attempted to consume a second element.");
}

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
Expand Down Expand Up @@ -128,7 +128,7 @@ static IEnumerable<int> TestSequence()
{
yield return 1;
yield return 2;
throw new Exception(nameof(MoreEnumerable.TrySingle) + " should not have attempted to consume a third element.");
throw new Exception(nameof(ExperimentalEnumerable.TrySingle) + " should not have attempted to consume a third element.");
}

var (cardinality, value) = TestSequence().TrySingle("zero", "one", "many");
Expand Down
4 changes: 2 additions & 2 deletions MoreLinq/TrySingle.cs → MoreLinq/Experimental/TrySingle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
// limitations under the License.
#endregion

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

partial class MoreEnumerable
partial class ExperimentalEnumerable
{
/// <summary>
/// Returns a tuple with the cardinality of the sequence and the
Expand Down
82 changes: 0 additions & 82 deletions MoreLinq/Extensions.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6588,88 +6588,6 @@ public static IEnumerable<IEnumerable<T>> Transpose<T>(this IEnumerable<IEnumera

}

/// <summary><c>TrySingle</c> extension.</summary>

[GeneratedCode("MoreLinq.ExtensionsGenerator", "1.0.0.0")]
public static partial class TrySingleExtension
{
/// <summary>
/// Returns a tuple with the cardinality of the sequence and the
/// single element in the sequence if it contains exactly one element.
/// similar to <see cref="Enumerable.Single{T}(IEnumerable{T})"/>.
/// </summary>
/// <param name="source">The source sequence.</param>
/// <param name="zero">
/// The value that is returned in the tuple if the sequence has zero
/// elements.</param>
/// <param name="one">
/// The value that is returned in the tuple if the sequence has a
/// single element only.</param>
/// <param name="many">
/// The value that is returned in the tuple if the sequence has two or
/// more elements.</param>
/// <typeparam name="T">
/// The type of the elements of <paramref name="source"/>.</typeparam>
/// <typeparam name="TCardinality">
/// The type that expresses cardinality.</typeparam>
/// <returns>
/// A tuple containing the identified <typeparamref name="TCardinality"/>
/// and either the single value of <typeparamref name="T"/> in the sequence
/// or its default value.</returns>
/// <remarks>
/// This operator uses immediate execution, but never consumes more
/// than two elements from the sequence.
/// </remarks>

public static (TCardinality Cardinality, T Value)
TrySingle<T, TCardinality>(this IEnumerable<T> source,
TCardinality zero, TCardinality one, TCardinality many) => MoreEnumerable. TrySingle(source, zero, one, many);

/// <summary>
/// Returns a result projected from the the cardinality of the sequence
/// and the single element in the sequence if it contains exactly one
/// element.
/// </summary>
/// <param name="source">The source sequence.</param>
/// <param name="zero">
/// The value that is passed as the first argument to
/// <paramref name="resultSelector" /> if the sequence has zero
/// elements.</param>
/// <param name="one">
/// The value that is passed as the first argument to
/// <paramref name="resultSelector" /> if the sequence has a
/// single element only.</param>
/// <param name="many">
/// The value that is passed as the first argument to
/// <paramref name="resultSelector" /> if the sequence has two or
/// more elements.</param>
/// <param name="resultSelector">
/// A function that receives the cardinality and, if the
/// sequence has just one element, the value of that element as
/// argument and projects a resulting value of type
/// <typeparamref name="TResult"/>.</param>
/// <typeparam name="T">
/// The type of the elements of <paramref name="source"/>.</typeparam>
/// <typeparam name="TCardinality">
/// The type that expresses cardinality.</typeparam>
/// <typeparam name="TResult">
/// The type of the result value returned by the
/// <paramref name="resultSelector"/> function. </typeparam>
/// <returns>
/// The value returned by <paramref name="resultSelector"/>.
/// </returns>
/// <remarks>
/// This operator uses immediate execution, but never consumes more
/// than two elements from the sequence.
/// </remarks>

public static TResult TrySingle<T, TCardinality, TResult>(this IEnumerable<T> source,
TCardinality zero, TCardinality one, TCardinality many,
Func<TCardinality, T, TResult> resultSelector)
=> MoreEnumerable.TrySingle(source, zero, one, many, resultSelector);

}

/// <summary><c>Window</c> extension.</summary>

[GeneratedCode("MoreLinq.ExtensionsGenerator", "1.0.0.0")]
Expand Down
2 changes: 1 addition & 1 deletion MoreLinq/MoreEnumerable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace MoreLinq

public static partial class MoreEnumerable
{
static int? TryGetCollectionCount<T>(this IEnumerable<T> source)
internal static int? TryGetCollectionCount<T>(this IEnumerable<T> source)
{
if (source == null) throw new ArgumentNullException(nameof(source));

Expand Down
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -680,14 +680,6 @@ Traces the elements of a source sequence for diagnostics.

This method has 3 overloads.

### TrySingle

Returns the only element of a sequence that has just one element. If the
sequence has zero or multiple elements, then returns a user-defined value
that indicates the cardinality of the result sequence.

This method has 2 overloads.

### Unfold

Returns a sequence generated by applying a state to the generator function,
Expand Down Expand Up @@ -770,6 +762,14 @@ Creates a sequence that lazily caches the source as it is iterated for the
first time, reusing the cache thereafter for future re-iterations. If the
source is already cached or buffered then it is returned verbatim.

### TrySingle

Returns the only element of a sequence that has just one element. If the
sequence has zero or multiple elements, then returns a user-defined value
that indicates the cardinality of the result sequence.

This method has 2 overloads.


[#122]: https://github.com/morelinq/MoreLINQ/issues/122
[dict]: https://docs.microsoft.com/en-us/dotnet/api/System.Collections.Generic.Dictionary-2
Expand Down

0 comments on commit b77df70

Please sign in to comment.