Posts Tagged ‘closures’

Extension Methods or a Closure: Why Not Both?

August 11, 2009

Lambdas can be useful for creating little utilities that can help you create more readable code. An example of creating an abstraction.

        [Test]

        public void should_use_classes_instead_of_better_stuff()

        {

            Looper.Loop(5, new Printer());

        }

 

        public class Looper

        {

            public static void Loop(int count, Printer print)

            {

                const int max_count = 5;

                for (int i = 0; i < max_count; i++)

                    print.Output();

            }

        }

 

        public class Printer

        {

            public void Output()

            {

                Console.WriteLine("Executing");

            }

        }

 
Output

Executing

Executing

Executing

Executing

Executing

 
As you can see a lot of work just to print out “Executing” string, and you may run into having to make a changes into your classes.
 
Let’s say the string printed to the console needs to change like this.
 

        [Test]

        public void should_use_classes_instead_of_better_stuff()

        {

            Looper.Loop(1, new Printer("Start"));

            Looper.Loop(5, new Printer("Middle"));

            Looper.Loop(1, new Printer("End"));

        }

 

    public class Looper

    {

        public static void Loop(int max_count, Printer print)

        {

            for (int i = 0; i < max_count; i++)

            {

                print.Output();

            }

        }

    }

 

    public class Printer

    {

        private readonly string _prefix;

 

        public Printer(string prefix)

        {

            _prefix = prefix;

        }

 

        public void Output()

        {

            Console.WriteLine("{0} Executing", _prefix);

        }

    }

Output

Start Executing

Middle Executing

Middle Executing

Middle Executing

Middle Executing

Middle Executing

End Executing

 
Now let’s get into using closures. 
 

        [Test]

        public void should_use_closures()

        {

            Action<string> print = (prefix => Console.WriteLine("{0} Executing", prefix));

 

            Action<int, Action> loop = ((count, fn) =>

            {

                for (int i = 0; i < count; i++)

                {

                    fn();

                }

            });

 

            loop(1, () => print("Start"));

            loop(5, () => print("Middle"));

            loop(1, () => print("End"));

        }

 
Output is the same as above.
 
It still is a little noisy in the parameter for print. So let’s clean this up.
 

        [Test]

        public void should_use_binding_lambda()

        {

            Action<string> print = (prefix => Console.WriteLine("{0} Executing", prefix));

 

            Action<int, Action> loop = ((count, fn) =>

            {

                for (int i = 0; i < count; i++)

                {

                    fn();

                }

            });

 

            Func<string, Action> use_printer = (prefix => () => print(prefix));

 

            loop(1, use_printer("Start"));

            loop(5, use_printer("Middle"));

            loop(1, use_printer("End"));

        }

 
This is nice abstraction. It hides the for loop from the programmer.

It still is noisy. How can we clean this up. TADA! Extension to the rescue.

    public static void Times(this int count, Action function)

    {

        for (int i = 0; i < count; i++)

        {

            function();

        }

    }

And the actual usage

[Test]

public void should_use_binding_lambda_and_extension()

{

    Action<string> print = (prefix => Console.WriteLine("{0} Executing", prefix));

 

    Action<int, Action> loop = ((count, fn) =>

    {

        for (int i = 0; i < count; i++)

        {

            fn();

        }

    });

 

    Func<string, Action> use_printer = (prefix => () => print(prefix));

 

    1.Times(use_printer("Start"));

    5.Times(use_printer("Middle"));

    1.Times(use_printer("End"));

 

}

 
Usage for all 4 iterations
 

// class objects

Looper.Loop(5, new Printer("Middle"));

//closure

loop(5, () => print("Middle"));

//binding on lambda

loop(5, use_printer("Middle"));

//extension

5.Times(use_printer("Middle"));

 
Now which one would you use?
Advertisements

Explorations in declarative programming

July 29, 2009

I want to explore the revolutionary idea of building software in a GPL (C#) that is declarative in style. This means using Event Driven Architecture, Internal DSLs, and Fluent Interfaces.

If you check this out, you will see that I’m very passionate about what is happening in the software world. There is a revolution happening right now!

I don’t care what language you’re using today (including VB6). By following this blog maybe you and I can chart some of the revolution together.

Some things that were the impetuous for this blog post was these two blog posts.

[1] Braindump on the Event Aggregator Pattern

[2] DDD, TDD, BDD, and CQS

[1] is a pretty concise description of the properties of an Event Aggregator (EA). The thing about EA is that it is more of a framework than a pattern. Something similar to an EA is Active Object. The key property for this is the decoupling of publisher from subscriber.

[2] Describes using a declarative language for interpreting systems with inputs, processors, and outputs. This interpretation uses a syntax of { When, Given, Then }. An example would be:

When some_input_event

Given this_process

Then some_output_event

This a declarative language.

convention over configuration‘ succinctly differentiates the declarative and imperative IMHO.

This exploration will utilize command patterns, events, message buses, computational systems, SRP, closures, decoupling, and you name it we’ll cover it.

Be sure to comment!