Monday 28 March 2011

C# 2.0 - Functional composition

Yes! It's possible, just! Get your thinking gear around this!

        public static filter_fn<A> not<A>(filter_fn<A> f)
        {
            return delegate(A a) { return !f(a); };
        }

        public static bool forAll<A>(filter_fn<A> f, IEnumerable<A> input)
        {
            return !exists(not(f), input);
        }


and an example:

        public void CompositionTest1()
        {
            int[] i = { 1, 2, 3, 45, 56, 6 };

            bool allOdd = Functional.forAll(Functional.dIsOdd, i);
            bool notAllOdd = Functional.exists(Functional.not(Functional.dIsOdd), i);

            Assert.IsFalse(allOdd);
            Assert.IsTrue(notAllOdd);
        }

No comments:

Post a Comment