Friday, December 12, 2008

Toys for boys


I can't wait for grab something like this toy for programmers
Some of the specs:

* 80MHz
* 512K flash storage
* 32K RAM
* 3 pushbuttons
* 5 LEDs
* 5V voltage regulator
* Terminal emulator command interface
* USB bootloader
* SMD resistor pads for every I/O pin

Wednesday, December 10, 2008

Lambda expressions closure fix

Is quite a time from my last post. My wife and I were blessed with a baby boy, and the time frozen for me, between the child needs and the job.

Today i had read an article very interesting about closure fix in Enumerator context.
http://geekswithblogs.net/jolson/archive/2008/06/13/lambdas---know-your-closures.aspx
below is a copy paste of it in case that page disappear:

Lambdas - Know Your Closures


In this post I want to discuss with you the importance of realizing how lambdas work (and why you should care). Let's dive right in with some code.

Given the following code snippet, what would you expect the output to be (no cheating :P)?

var actions = new List();

for (int i = 0; i < 10; i++)
{
actions.Add(() => Console.WriteLine(i));
}

foreach (var action in actions)
{
action();
}

Would you believe me if I told you this is the output you get?

10
10
10
10
10
10
10
10
10
10

At first glance, one might expect this output:

0
1
2
3
4
5
6
7
8
9

But all tens are output instead. Why does this happen? Let's crank open Reflector and find out why...



The first thing you'll notice is that the compiler has created a helper class to enable the closure we have. This helper class created by the compiler contains a local variable that we use to iterate over and a method that our delegate is contained within. This helper class is especially interesting because it is where the magic happens.



[CompilerGenerated]
private sealed class <>c__DisplayClass2
{
// Fields
public int i;

// Methods
public void
b__0()
{
Console.WriteLine(this.i);
}
}

Rather than diving into MSIL, let's look at some pseudo-code of what the compiler _actually_ executes with the original code above (based on the generated MSIL):



var actions = new List();

<>c__DisplayClass2 localFrame = new <>c__DisplayClass2();
for (localFrame.i = 0; localFrame.i < 10; localFrame.i++)
{
actions.Add(localFrame.
b__0);
}

foreach (var action in actions)
{
action();
}



Perhaps now you can see where the problem is. The "problem" in our original code exists because of the scope that our closures are defined. The local index from our for loop is now stored in our helper class <>c__DisplayClass2. And the code that is executed by the action is contained within the compiler generated
b__0() method now. So the Console.WriteLine() method now uses the local variable from <>c__DisplayClass2 when it is executed.



So while we are looping through building all our actions, we are also incrementing the property i in localFrame (an instance of <>c__DisplayClass2). Then at the end when we are actually executing the actions, the
b__0() is called and uses the local i property (which by this time, has already been incremented to 10 from our loop). And that's why every action we execute prints "10" instead of the 0 through 9 like we expected.



So, why do you need to know how these work? Take the following code that outputs items from an array of strings:



var actions = new List();
string[] urls =
{
"http://www.url.com",
"http://www.someurl.com",
"http://www.someotherurl.com",
"http://www.yetanotherurl.com"
};

for (int i = 0; i < urls.Length; i++)
{
actions.Add(() => Console.WriteLine(urls[i]));
}

foreach (var action in actions)
{
action();
}



This code looks pretty innocuous. Our bounds are protected, and we just index into our array to output a string. But, is that what we're really doing? Remember how closures work from above. The actual thing that happens when I run this code is this:



Confusing



Interesting. Even though our index variable should only even be less than the length of our url array, an exception is thrown because the index variable is actually equal to the length of our url array (and hence outside of the bounds thanks to 0-based indices).



Well, that wasn't what we were probably expecting. But now that we are having this "problem", what is the easiest way to resolve it? Remember that the problem is happening because of the scope of the variable within our closure. So to fix this, we can essentially declare a temporary variable that is unique in scope to this specific iteration through our array:



for (int i = 0; i < urls.Length; i++)
{
string localUrl = urls[i];
actions.Add(() => Console.WriteLine(localUrl));
}



And now the code is fixed.



Understanding how lambdas work is especially important when you start developing with a library that leverages lambdas heavily like LINQ does, or Parallel Extensions to the .NET Framework. And don't worry, even those people that know how lambdas work occasionally get bitten by this behavior.

Tuesday, July 22, 2008

A good laugh speech recognition in Vista

I know how hard is to make such a tool, but I can't stop to lol about outcome


Thursday, May 22, 2008

MRSD how to change default port for host projects

[MRSD(Microsoft Robotics Developer Studio) installation folder]\tools\Templates\CSharp\ contains a zip file named CS_Hosting_2.0.730.3.zip that contains a template for a c# host project. Change the program.cs in that archive to use the new ports instead of defaults 50000 and 50001.

A critic over your shoulder


Pex could be the critic you need for your code. If you don't have a friend to criticize when you make a mistake or to laugh his ass off, then you should find one. Pex is a testing engine, that make code analysis and generate unit tests accordingly. The final scope is to increase the quality of your unit tests by raising your test code coverage.

Wednesday, May 21, 2008

Smat tags in runtime Windows.Forms

A very interesting topic aboutSmart tags in runtime on Windows.Forms. I just made a copy:

You have to create a DesignerOptions with UseSmartTags property set to true, add the DesignerOptions to DesignerOptionService, after which add this service to the DesignerSurface, I write the following sample which host a TextBox and a DataGridView control on the designer surface.

Code Snippet

namespace Sample25

{

public partial class Form2 : Form

{

public Form2()

{

InitializeComponent();

}



private void Form2_Load(object sender, EventArgs e)

{

myDesignSurface surface = new myDesignSurface();

IDesignerHost host = surface.GetService(typeof(IDesignerHost)) as IDesignerHost;



UserControl root = host.CreateComponent(typeof(UserControl)) as UserControl;

root.Width = this.panel1.Width - 30;

root.Height = this.panel1.Height - 30;



TextBox textbox = host.CreateComponent(typeof(TextBox)) as TextBox;

textbox.Location = new Point(100, 90);

textbox.Text = "textbox";

root.Controls.Add(textbox);



DataGridView dgv = host.CreateComponent(typeof(DataGridView)) as DataGridView;

dgv.Location = new Point(100, 130);

dgv.Text = "DataGridView";

root.Controls.Add(dgv);



Control c = surface.View as Control;

c.Dock = DockStyle.Fill;

c.BackColor = Color.White;

c.Parent = this.panel1;

}

}



class myDesignSurface : DesignSurface

{

public myDesignSurface()

{

MyDesignerOptionService optionService = new MyDesignerOptionService();

this.ServiceContainer.AddService(typeof(DesignerOptionService), optionService);

}

}



class MyDesignerOptionService : DesignerOptionService

{

protected override void PopulateOptionCollection(DesignerOptionCollection options)

{

if (options.Parent == null)

{

DesignerOptionCollection doc =
this.CreateOptionCollection(options, "WindowsFormsDesigner", null);

DesignerOptions doptions = new DesignerOptions();

doptions.UseSmartTags = true;

this.CreateOptionCollection(doc, "General", doptions);

}

}

}

}

Changing learning curve ... is developer's learning effort

How easy is to change the learning curve for the users of your programs? The answer lays on your response to What is ergonomics?

Example of not to do: to name Arbiter.Choise<>() (is suggesting a decision to make by using a predicate ) instead of Arbiter.TakeFirstArrivingFrom<>() (more clear for the reader) or Arbiter.FirstReceivedFrom() or even Arbiter.Any<>() sounds better.

I polluted my mind with Implementing a Simple 2D Object Tracker. I'm sorry right now, because i want to make my own system of recognition, or at least to take the time to think the best approach. After that to see other programmer's solutions.

Tuesday, May 20, 2008

Nature is fighting back - the suicide ants

NASA moves to save computers from swarming ants.
I'm remember Ants a great animation movie and the Ants and The Day of the ants by Webber great sci-fi books all in category must see and read

Falling in the pit of success

First service that control a robot. Is very simple in his nature, but is the first little big step.

Prepare yourself for staring at amazing movie - great work Boston Dynamics


At this time I'm a PHSC

SpamAssasin with the Traffic control tool a good news.

Some steganography (let the user to hide data in plain view) tools Hide in Picture, PDF's, HTML files or bitmaps, mp3stego
To hide a folder, just rename it to Alt+255, change the icon representation to a blank icon.

Mouse gesture in C#, to think of some usage in the future solutions.

Sunday, May 18, 2008

Starting is hard sometimes

I just watched the architecture getting started video for Microsoft Robotics Studio (MSRS) and decided to play with Visual Programming Language for MSRS. Is very intuitive and I made a hello world application in 30 seconds. But when tried to run I got:
A valid transport port was not specified.
This setback is easy to resolve by using
netstat -a -o
and see if the ports are used already by your system and make a decision if you want to terminate the program that is using the ports or to use other ports. To change what ports to use is fairly simple. From the Run menu choose Port Settings and enter the desired unused port numbers (let say 55004 and 55504).
Running again I got other error:
DsspForwarder:CreateRequestFrowarder. System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. Parameter name: Transport not found in local ServiceEnvironment instance, for scheme:http
ouch. Error that is hiding other error in other service:
Cannot create a TCP Listener.
Error was raised because my operating system was Vista, and UAC (User Account Control) is on. The right solution for this is to use httpreserve, not to disable the UAC. I run DSS Command Prompt from the menu group Microsoft Robotics Developer Studio as administrator and executed the command httpreserve /port:55004 /user:me ( 55004 is the http port and me is current vista user logged in). After this application run smoothly and a hello appear.

Friday, May 16, 2008

Social integration

Google http://translate.google.com/translate_t now allow me to translate text from English in Romanian. Is not perfect, but what is?

A very funny toy http://phun.cs.umu.se/wiki/Download to learn physics.

http://www.FlazX.com is a very nice site, it makes me to want to live forever.

Thursday, May 15, 2008

Future is now

Microsoft TouchWall is very impressive and is more practical then Surface table.

Sharping the axe before cutting the tree

LinqPad from http://www.linqpad.net is a nice learning tool. It comes with a challenge. I will subscribe to it.

It seems that saving(inserting) bulk in SQL Server is much more efficient. So our framework will gain a new functionality. There are scenarios when allot of calculated records will get home by this new bus.

I'm disappointed by Entities Framework I hope that will evolve in a real tool in the near future.

It seems that the day is getting smaller for me, even the light period is increasing.