public virtual void TestFilter() { IList <string> values = Arrays.AsList("a", "HI", "tHere", "YO"); IEnumerator <string> iterator = Iterables.Filter(values, null).GetEnumerator(); NUnit.Framework.Assert.IsTrue(iterator.MoveNext()); NUnit.Framework.Assert.AreEqual(iterator.Current, "HI"); NUnit.Framework.Assert.AreEqual(iterator.Current, "YO"); NUnit.Framework.Assert.IsFalse(iterator.MoveNext()); }
/// <summary>Sample k items uniformly from an Iterable of size n (without replacement).</summary> /// <param name="items">The items from which to sample.</param> /// <param name="n">The total number of items in the Iterable.</param> /// <param name="k">The number of items to sample.</param> /// <param name="random">The random number generator.</param> /// <returns>An Iterable of k items, chosen randomly from the original n items.</returns> public static IEnumerable <T> Sample <T>(IEnumerable <T> items, int n, int k, Random random) { // assemble a list of all indexes IList <int> indexes = new List <int>(); for (int i = 0; i < n; ++i) { indexes.Add(i); } // shuffle the indexes and select the first k Java.Util.Collections.Shuffle(indexes, random); ICollection <int> indexSet = Generics.NewHashSet(indexes.SubList(0, k)); // filter down to only the items at the selected indexes return(Iterables.Filter(items, new _IPredicate_614(indexSet))); }