2007-07-06

slightly enhanced Higher-Level Abstractions on Enumerations

Andrei has some nice code to work with Enumerations and while showing it to the team today, I tried to chain calls like this:

      


        [Test]
        public void Can_Chain_Select_Collect_and_More() {
            bool satisfying = Enumerating.On(_array)
                .Select(IsEven)
                .Collect<string>(delegate(string input) { return input + "-" + input; })
                .AreAllSatisfying(Satisfies);
            Assert.IsTrue(satisfying);
        }
 
 
        private bool Satisfies(string obj) {
            return obj == "2-2" || obj == "4-4" || obj == "6-6";
        }
 
But this did not work :-( (To much C# 3.0 exposure I guess) because methods like Select and Collect return ICollection<T>. 
So I changed it. 
You can grab it here: Enumerating.Zip. Of course all praise to Andrei, all blame to me.
 
How did I do it?
Well I Just changed 
        ICollection<R> Collect<R>(Converter<T, R> convert);

        ICollection<R> Map<R>(Converter<T, R> convert);
        ICollection<T> Select(Predicate<T> satisfies);
        ICollection<T> FindAll(Predicate<T> satisfies);
        ICollection<T> Filter(Predicate<T> satisfies);
        ICollection<T> Reject(Predicate<T> satisfies);
        ICollection<R> SelectThenCollect<R>(Predicate<T> satisfies, Converter<T, R> converter);
        ICollection<T> Sort(Comparison<T> comparison);
to 
        IEnumerating<R> Collect<R>(Converter<T, R> convert);

        IEnumerating<R> Map<R>(Converter<T, R> convert);
        IEnumerating<T> Select(Predicate<T> satisfies);
        IEnumerating<T> FindAll(Predicate<T> satisfies);
        IEnumerating<T> Filter(Predicate<T> satisfies);
        IEnumerating<T> Reject(Predicate<T> satisfies);
        IEnumerating<R> SelectThenCollect<R>(Predicate<T> satisfies, Converter<T, R> converter);
        IEnumerating<T> Sort(Comparison<T> comparison);
 
So its possible to chain commands that return more than one thing. 
And I added 
        ICollection<T> ToCollection();

to make the tests work.
 
 

Whos.amung.us?

whos online

Ralf"s shared items