Monday, July 12, 2010

Lambda Expressions

=> is pronounced goes to, that means that x => x * 2 should be explained as x goes to x * 2

A simple example multiplying two numbers

delegate int mul(int a, int b);
static void Main(string[] args)
{
    mul m = (x,y) => x * y;
    Console.WriteLine(m(5,5)); // 25
}

Simple example just comparing contents

delegate bool equ(int a, int b);
static void Main(string[] args)
{
    equ e = (x,y) => x == y;
    Console.WriteLine(m(5,5)); // True
    Console.WriteLine(m(5,4)); // False
}

Alternatively as Func<T, TResult> (notice not declaring the delegate)

static void Main(string[] args)
{
    Func<int, int, int> mul = (x,y) => x * y;
    Func<int, int, bool> equ = (x,y) => x == y;
    Console.WriteLine(m(5,5)); // 25
    Console.WriteLine(m(5,5)); // True
    Console.WriteLine(m(5,4)); // False
}

Retrieving a Recursive List of Files

This is much simpler now:
Directory.EnumerateFiles(@"c:\", "*.cs", SearchOption.AllDirectories);

Added Google Analytics to Page

Published web site and included it to be searchable. It is going to be interesting to see if anyone will read this blog.

<script type='text/javascript'> 
 
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-17412355-1']);
  _gaq.push(['_trackPageview']);
 
  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
 
</script> 

Syntax Highlighting

The syntax highlighter used on this blog is creaetd by Alex Gorbatchev and is currently configured on this blog with the following parameters:

After <head> I insert the following code in my blogger html template
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css' rel='stylesheet' type='text/css'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shAutoloader.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCSharp.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJScript.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js' type='text/javascript'/>

And before </body> I insert the following javascript code
<script type='text/javascript'>
    SyntaxHighlighter.defaults['bloggerMode'] = true;
    SyntaxHighlighter.defaults['toolbar'] = false;
    SyntaxHighlighter.defaults['gutter'] = false;
    SyntaxHighlighter.all();
</script>

This gives me the opportunity to use the following syntax highlighting/brushes:
- C#
- JavaScript
- XML, XHTML

Simple Events Example

This is a short example for how one could use events.

using System;

namespace EventsExample
{
    public class MyEventArgs : EventArgs
    {
        public readonly int Complete;
        public MyEventArgs(int index, int length)
        {
            Complete = (index + 1) * 100 / length;
        }
    }

    public delegate void StartDelegate(object sender, EventArgs e);
    public delegate void UpdateDelegate(object sender, MyEventArgs e);

    public class MyClass
    {
        public event StartDelegate StartEvent;
        public event UpdateDelegate UpdateEvent;

        protected virtual void Start(EventArgs e)
        {
            if (StartEvent != null)
                StartEvent(this, e);
        }

        protected virtual void Update(MyEventArgs e)
        {
            if (UpdateEvent != null)
                UpdateEvent(this, e);
        }

        public void CountTo(int j)
        {
            Start(EventArgs.Empty);
            for (int i = 0; i < j; i++)
            {
                // do stuff here
                Update(new MyEventArgs(i, j));
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            MyClass myClass = new MyClass();
            myClass.StartEvent += new StartDelegate(myClass_Start);
            myClass.UpdateEvent += new UpdateDelegate(myClass_Update);

            myClass.CountTo(100);
        }

        private static void myClass_Start(object sender, EventArgs e)
        {
            Console.WriteLine("Started!");
        }

        private static void myClass_Update(object sender, MyEventArgs e)
        {
            MyEventArgs f = (MyEventArgs)e;
            Console.WriteLine("{0}% complete", f.Complete);
        }
    }
}