Example #1
0
        /// <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> Concat <T>(this IEnumerable <T> head, T tail)
        {
            if (head == null)
            {
                throw new ArgumentNullException(nameof(head));
            }
            return(LinqEnumerable.Concat(head, LinqEnumerable.Repeat(tail, 1)));
        }
Example #2
0
 /// <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>
 /// int[] numbers = { 1, 2, 3 };
 /// IEnumerable&lt;int&gt; 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)
 {
     if (source == null)
     {
         throw new ArgumentNullException("source");
     }
     return(LinqEnumerable.Concat(LinqEnumerable.Repeat(value, 1), source));
 }
 public void IndexOfTest()
 {
     Assert.AreEqual(-1, Enumerable.Range(0, 5).IndexOf(-1));
     Assert.AreEqual(-1, Enumerable.Range(0, 5).IndexOf(6));
     Assert.AreEqual(0, Enumerable.Range(0, 5).IndexOf(0));
     Assert.AreEqual(1, Enumerable.Range(0, 5).IndexOf(1));
     Assert.AreEqual(-1, Enumerable.Repeat("a", 5).IndexOf("A"));
     Assert.AreEqual(0, Enumerable.Repeat("a", 5).IndexOf("A", comparer: StringComparer.OrdinalIgnoreCase));
 }
Example #4
0
 private void NullCheckValueChanged(object sender, ValueChangedEventArgs e)
 {
     if (nullCheck.Value)
     {
         OnAddItems(SL.Repeat(default(T), nulls));
     }
     else
     {
         OnRemoveItems(SL.Repeat(default(T), nulls));
     }
 }
 public void LastIndexOfTest()
 {
     Assert.AreEqual(-1, Enumerable.Range(0, 5).LastIndexOf(-1));
     Assert.AreEqual(-1, Enumerable.Range(0, 5).LastIndexOf(6));
     Assert.AreEqual(0, Enumerable.Range(0, 5).LastIndexOf(0));
     Assert.AreEqual(1, Enumerable.Range(0, 5).LastIndexOf(1));
     Assert.AreEqual(4, Enumerable.Repeat(0, 5).LastIndexOf(0));
     Assert.AreEqual(6, Enumerable.Repeat(0, 5).Concat(Enumerable.Range(0, 5)).LastIndexOf(1));
     Assert.AreEqual(-1, Enumerable.Repeat("a", 5).LastIndexOf("A"));
     Assert.AreEqual(4, Enumerable.Repeat("a", 5).LastIndexOf("A", StringComparer.OrdinalIgnoreCase));
 }
Example #6
0
        private void LambdaChanged(object sender, ValueChangedEventArgs e)
        {
            var lambdaResult = sender as TaggedObservableValue <bool, ItemMultiplicity>;

            if (lambdaResult.Value)
            {
                OnAddItems(SL.Repeat(lambdaResult.Tag.Item, lambdaResult.Tag.Multiplicity));
            }
            else
            {
                OnRemoveItems(SL.Repeat(lambdaResult.Tag.Item, lambdaResult.Tag.Multiplicity));
            }
        }
Example #7
0
        private static IEnumerable <SyntaxTrivia> GetUseTabsModeIndentTrivias(int indentSize, int tabSize)
        {
            foreach (SyntaxTrivia indentTabTrivia in Enumerable.Repeat(SyntaxFactory.Tab, indentSize / tabSize))
            {
                yield return(indentTabTrivia);
            }

            int remainingSpaceTrivia = indentSize % tabSize;

            if (remainingSpaceTrivia != 0)
            {
                yield return(SyntaxFactory.Whitespace(new string(' ', remainingSpaceTrivia)));
            }
        }
Example #8
0
        public static string GenerateReceiptId()
        {
            var currentUtcTime = DateTime.Now;

            return("SAI" + currentUtcTime.ToString("yyyy") +
                   string.Concat(Enumerable.Repeat('0', 3 - currentUtcTime.DayOfYear.ToString().Length)) +
                   currentUtcTime.DayOfYear +
                   string.Concat(Enumerable.Repeat('0', 2 - currentUtcTime.Hour.ToString().Length)) +
                   currentUtcTime.Hour +
                   string.Concat(Enumerable.Repeat('0', 2 - currentUtcTime.Minute.ToString().Length)) +
                   currentUtcTime.Minute +
                   string.Concat(Enumerable.Repeat('0', 2 - currentUtcTime.Second.ToString().Length)) +
                   currentUtcTime.Second);
        }
 public void RemoveAllTest()
 {
     EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 2, 3, 4 }, Enumerable.Range(0, 5).RemoveAll(-1));
     EnumerableAssert.AreSequentialEqual(new int[] { 1, 2, 3, 4 }, Enumerable.Range(0, 5).RemoveAll(0));
     EnumerableAssert.AreSequentialEqual(new int[] { 0, 2, 3, 4 }, Enumerable.Range(0, 5).RemoveAll(1));
     EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 3, 4 }, Enumerable.Range(0, 5).RemoveAll(2));
     EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 2, 4 }, Enumerable.Range(0, 5).RemoveAll(3));
     EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 2, 3 }, Enumerable.Range(0, 5).RemoveAll(4));
     EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 2, 3, 4 }, Enumerable.Range(0, 5).RemoveAll(5));
     EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 2, 3, 4 }, Enumerable.Range(0, 5).RemoveAll(6));
     EnumerableAssert.AreSequentialEqual(new int[] { }, Enumerable.Repeat(0, 5).RemoveAll(0));
     EnumerableAssert.AreSequentialEqual(new int[] { 0, 0, 0, 0, 0 }, Enumerable.Repeat(0, 5).RemoveAll(1));
     EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 2, 3, 0, 1, 2, 3 }, Enumerable.Range(0, 5).Concat(Enumerable.Range(0, 5)).RemoveAll(4));
     EnumerableAssert.AreSequentialEqual(new int[] { 1, 2, 3, 4, 1, 2, 3, 4 }, Enumerable.Range(0, 5).Concat(Enumerable.Range(0, 5)).RemoveAll(0));
 }
Example #10
0
        public void RepeatTest()
        {
            EnumerableAssert.AreSequentialEqual(
                Enumerable.Repeat(1, 5),
                IteratorPattern.Repeat(1, 5));
            EnumerableAssert.AreSequentialEqual(
                Enumerable.Repeat(1, 0),
                IteratorPattern.Repeat(1, 0));
            EnumerableAssert.AreSequentialEqual(
                Enumerable.Repeat(1, 1),
                IteratorPattern.Repeat(1, 1));

            object value = new object();

            EnumerableAssert.AreSequentialEqual(
                Enumerable.Repeat(value, 10),
                IteratorPattern.Repeat(value, 10));
        }
Example #11
0
 /// <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> Concat <T> (this IEnumerable <T> head, T tail)
 {
     head.ThrowIfNull("head");
     return(LinqEnumerable.Concat(head, LinqEnumerable.Repeat(tail, 1)));
 }
        public void RemoveAllTest()
        {
            try
            {
                int[] removeAll = Enumerable.Range(0, 5).RemoveAll(-1).ToArray();
                Assert.Fail(string.Join(", ", removeAll.Select(int32 => int32.ToString())));
            }
            catch (ArgumentOutOfRangeException exception)
            {
                Assert.AreEqual("remove", exception.ParamName);
            }
            EnumerableAssert.AreSequentialEqual(new int[] { 1, 2, 3, 4 }, Enumerable.Range(0, 5).RemoveAll(0));
            EnumerableAssert.AreSequentialEqual(new int[] { 0, 2, 3, 4 }, Enumerable.Range(0, 5).RemoveAll(1));
            EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 3, 4 }, Enumerable.Range(0, 5).RemoveAll(2));
            EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 2, 4 }, Enumerable.Range(0, 5).RemoveAll(3));
            EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 2, 3 }, Enumerable.Range(0, 5).RemoveAll(4));
            try
            {
                int[] removeAll = Enumerable.Range(0, 5).RemoveAll(5).ToArray();
                Assert.Fail(string.Join(", ", removeAll.Select(int32 => int32.ToString())));
            }
            catch (ArgumentOutOfRangeException exception)
            {
                Assert.AreEqual("remove", exception.ParamName);
            }
            try
            {
                int[] removeAll = Enumerable.Range(0, 5).RemoveAll(6).ToArray();
                Assert.Fail(string.Join(", ", removeAll.Select(int32 => int32.ToString())));
            }
            catch (ArgumentOutOfRangeException exception)
            {
                Assert.AreEqual("remove", exception.ParamName);
            }
            EnumerableAssert.AreSequentialEqual(Enumerable.Empty <int>(), Enumerable.Repeat(0, 5).RemoveAll(0));
            try
            {
                int[] removeAll = Enumerable.Repeat(0, 5).RemoveAll(1).ToArray();
                Assert.Fail(string.Join(", ", removeAll.Select(int32 => int32.ToString())));
            }
            catch (ArgumentOutOfRangeException exception)
            {
                Assert.AreEqual("remove", exception.ParamName);
            }
            EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 2, 3, 0, 1, 2, 3 }, Enumerable.Range(0, 5).Concat(Enumerable.Range(0, 5)).RemoveAll(4));
            EnumerableAssert.AreSequentialEqual(new int[] { 1, 2, 3, 4, 1, 2, 3, 4 }, Enumerable.Range(0, 5).Concat(Enumerable.Range(0, 5)).RemoveAll(0));

            EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 2, 3, 4 }, Enumerable.Range(0, 5).RemoveAll(-1, null, ListQueryMode.Normalize));
            EnumerableAssert.AreSequentialEqual(new int[] { 1, 2, 3, 4 }, Enumerable.Range(0, 5).RemoveAll(0, null, ListQueryMode.Normalize));
            EnumerableAssert.AreSequentialEqual(new int[] { 0, 2, 3, 4 }, Enumerable.Range(0, 5).RemoveAll(1, null, ListQueryMode.Normalize));
            EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 3, 4 }, Enumerable.Range(0, 5).RemoveAll(2, null, ListQueryMode.Normalize));
            EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 2, 4 }, Enumerable.Range(0, 5).RemoveAll(3, null, ListQueryMode.Normalize));
            EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 2, 3 }, Enumerable.Range(0, 5).RemoveAll(4, null, ListQueryMode.Normalize));
            EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 2, 3, 4 }, Enumerable.Range(0, 5).RemoveAll(5, null, ListQueryMode.Normalize));
            EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 2, 3, 4 }, Enumerable.Range(0, 5).RemoveAll(6, null, ListQueryMode.Normalize));
            EnumerableAssert.AreSequentialEqual(Enumerable.Empty <int>(), Enumerable.Repeat(0, 5).RemoveAll(0, null, ListQueryMode.Normalize));
            EnumerableAssert.AreSequentialEqual(Enumerable.Repeat(0, 5), Enumerable.Repeat(0, 5).RemoveAll(1, null, ListQueryMode.Normalize));
            EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 2, 3, 0, 1, 2, 3 }, Enumerable.Range(0, 5).Concat(Enumerable.Range(0, 5)).RemoveAll(4, null, ListQueryMode.Normalize));
            EnumerableAssert.AreSequentialEqual(new int[] { 1, 2, 3, 4, 1, 2, 3, 4 }, Enumerable.Range(0, 5).Concat(Enumerable.Range(0, 5)).RemoveAll(0, null, ListQueryMode.Normalize));

            EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 2, 3, 4 }, Enumerable.Range(0, 5).RemoveAll(-1, null, ListQueryMode.Ignore));
            EnumerableAssert.AreSequentialEqual(new int[] { 1, 2, 3, 4 }, Enumerable.Range(0, 5).RemoveAll(0, null, ListQueryMode.Ignore));
            EnumerableAssert.AreSequentialEqual(new int[] { 0, 2, 3, 4 }, Enumerable.Range(0, 5).RemoveAll(1, null, ListQueryMode.Ignore));
            EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 3, 4 }, Enumerable.Range(0, 5).RemoveAll(2, null, ListQueryMode.Ignore));
            EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 2, 4 }, Enumerable.Range(0, 5).RemoveAll(3, null, ListQueryMode.Ignore));
            EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 2, 3 }, Enumerable.Range(0, 5).RemoveAll(4, null, ListQueryMode.Ignore));
            EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 2, 3, 4 }, Enumerable.Range(0, 5).RemoveAll(5, null, ListQueryMode.Ignore));
            EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 2, 3, 4 }, Enumerable.Range(0, 5).Remove(6, null, ListQueryMode.Ignore));
            EnumerableAssert.AreSequentialEqual(Enumerable.Empty <int>(), Enumerable.Repeat(0, 5).RemoveAll(0, null, ListQueryMode.Ignore));
            EnumerableAssert.AreSequentialEqual(Enumerable.Repeat(0, 5), Enumerable.Repeat(0, 5).RemoveAll(1, null, ListQueryMode.Ignore));
            EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 2, 3, 0, 1, 2, 3 }, Enumerable.Range(0, 5).Concat(Enumerable.Range(0, 5)).RemoveAll(4, null, ListQueryMode.Ignore));
            EnumerableAssert.AreSequentialEqual(new int[] { 1, 2, 3, 4, 1, 2, 3, 4 }, Enumerable.Range(0, 5).Concat(Enumerable.Range(0, 5)).RemoveAll(0, null, ListQueryMode.Ignore));
        }
 public void NegativeCountIsInvalid()
 {
     Assert.Throws <ArgumentOutOfRangeException>(() => Linq.Repeat("string", -8));
 }
 public void NullValuesAllowed()
 {
     Linq.Repeat <string>(null, 3).Should().BeEquivalentTo(new string[] { null, null, null });
 }
 public void WhenCount0ReturnsEmptyCollection()
 {
     Linq.Repeat("string", 0).Should().BeEmpty();
 }
        public void RepeatsString()
        {
            var repeatSequence = Linq.Repeat("string", 3);

            repeatSequence.Should().BeEquivalentTo("string", "string", "string");
        }
Example #17
0
        public override INotificationResult Notify(IList <INotificationResult> sources)
        {
            var added   = new List <T>();
            var removed = new List <T>();

            foreach (var change in sources)
            {
                if (change.Source == source)
                {
                    var sourceChange = (CollectionChangedNotificationResult <T>)change;
                    if (sourceChange.IsReset)
                    {
                        foreach (var item in lambdaInstances.Values)
                        {
                            item.Successors.Unset(this);
                        }
                        lambdaInstances.Clear();
                        foreach (var item in source)
                        {
                            AttachItem(item);
                        }
                        OnCleared();
                        return(new CollectionChangedNotificationResult <T>(this));
                    }
                    else
                    {
                        NotifySource(sourceChange, added, removed);
                    }
                }
                else if (nullCheck != null && change.Source == nullCheck)
                {
                    if (nullCheck.Value)
                    {
                        added.AddRange(SL.Repeat(default(T), nulls));
                    }
                    else
                    {
                        removed.AddRange(SL.Repeat(default(T), nulls));
                    }
                }
                else
                {
                    var lambdaResult = (TaggedObservableValue <bool, ItemMultiplicity>)change.Source;
                    if (lambdaResult.Value)
                    {
                        added.AddRange(SL.Repeat(lambdaResult.Tag.Item, lambdaResult.Tag.Multiplicity));
                    }
                    else
                    {
                        removed.AddRange(SL.Repeat(lambdaResult.Tag.Item, lambdaResult.Tag.Multiplicity));
                    }
                }
            }

            if (added.Count == 0 && removed.Count == 0)
            {
                return(UnchangedNotificationResult.Instance);
            }

            OnRemoveItems(removed);
            OnAddItems(added);
            return(new CollectionChangedNotificationResult <T>(this, added, removed));
        }
Example #18
0
        /// <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>
        /// int[] numbers = { 1, 2, 3 };
        /// IEnumerable&lt;int&gt; 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)
        {
            source.ThrowIfNull("source");
            return(LinqEnumerable.Concat(LinqEnumerable.Repeat(value, 1), source));
        }