Now coding...http://nowcoding.net
Pablo Fernandez DuranSun, 02 Mar 2014 18:30:42 +0000en-UShourly1https://wordpress.org/?v=4.2.22[F#] Common Intersection of a sequence of sequenceshttp://feedproxy.google.com/~r/PabloFernandezDuran/~3/0-IwSYn_veY/
http://nowcoding.net/fsharp-common-intersection-of-a-sequence-of-sequences/#commentsThu, 20 Feb 2014 18:21:19 +0000http://nowcoding.net/?p=138The idea is to find the common intersection of a sequence of sequences — or lists or arrays (in red in the image below).

Intersection of 3 sets

Let’s say we have a bunch of CSV files and we want to find all the common columns between these files.
We are goint to use the following data for testing:

In F# we don’t have a built-in function to compute the intersection of two lists or sequences — as we do in C# with Enumerable.Intersect — but we do have an intersection function in the Set module to work with sets, so we will use it.

First we need to transform our sequence of lists in a sequence of sets (working directly with our data):

[Update]
There is built-in function in the Set module to calculate directly the common intersection of a sequence of sets: Set.intersectMany, so we only need to transform our sequence of lists to a sequence of sets.

]]>http://nowcoding.net/fsharp-common-intersection-of-a-sequence-of-sequences/feed/0http://nowcoding.net/fsharp-common-intersection-of-a-sequence-of-sequences/[SQL Server] Display hierarchical data from a treehttp://feedproxy.google.com/~r/PabloFernandezDuran/~3/pFXs6LDmSTk/
http://nowcoding.net/sql-server-display-hierarchical-data-from-a-tree/#commentsFri, 13 Dec 2013 00:35:15 +0000http://nowcoding.net/?p=123It is common to have a hierarchical or tree structure table in a SQL model data.

We would like to display the data as follow:

Hierarchical display data

But when it comes to display the raw data it is hard to identify the hierarchical form.

]]>http://nowcoding.net/sql-server-display-hierarchical-data-from-a-tree/feed/4http://nowcoding.net/sql-server-display-hierarchical-data-from-a-tree/Windows Forms, how to avoid a Dialog Box to closehttp://feedproxy.google.com/~r/PabloFernandezDuran/~3/-WaeoQJLlvg/
http://nowcoding.net/windows-forms-how-to-avoid-dialog-box-to-close/#commentsSat, 09 Nov 2013 02:38:27 +0000http://nowcoding.net/?p=94When displaying a Form as a Dialog/Modal Box sometimes you may need to avoid the Dialog form to close.

Let’s say we have a main form that displays another form as a Modal dialog to finally display some information from that form.

Let’s say now that I want to make some validation before the Modal Dialog form closes, for example allow only “platypus” as favorite pet. How can I make the modal dialog to continue running after clicking a button?

if (!radioButtonPlatypus.Checked)
{
// avoid the modal dialog to close
}

This code has no comments and it has been in production since years, at least the method’s name gives a hint of its intention, this method is meant to format a string (like a name) replacing the first character after a given separator character (' ', '-', '_', '.', '\'') by its upper case form.

It seems to work. Wait! I don’t really like how this application displays my last name (FernanDez). Leaving aside some algorithmic issues, let’s find the source of the problem. The incriminated line of code is:

Let’s say name.Substring(index, 1) = "d".
We have: name = name.Replace("d", "d".ToUpper()). Not only the character “d” at the index position is replaced but all the occurrences of “d” in the string are replaced.

Using the TextInfo.ToTitleCase method

That’s better, but I’m not still happy about this code, a lot of noise, difficult to maintain, the main intention of the algorithm it’s hidden in its implementation and so on. Before trying to rewrite the method let’s see if there is something in the .NET framework that can be useful. We have the TextInfo.ToTitleCase Method.

Great! \b[a-zA-Z] will find all characters between a to z and A to Z just after a word boundary (\b). More information about Regex.Replacehere.

One case is not covered with this regex: characters following a ‘_’ (underscore). That is because the word boundary \b by definition considers as word characters the “character class”\w, and \w is the short hand for [A-Za-z0-9_].

Let’s tune our regex to the handle the _ character as a separator: (?<=\b|_)[a-zA-Z].

Bonus

To handle names like mcfry or macdonald’s we can use:(?<=\b(?:mc|mac)?|_)[a-zA-Z](?<!'s\b)

Finally

We need to do some extra work on the regex if we want to match an extended range of characters like diacritics (accents), take a look at the Character classes in regex reference on the smdn or here. You can use this set to match also accents:[a-zA-ZÀ-ÿ].

Warning

Formatting names is a delicate question, it may depend on cultures and languages. The name is the identity of a person and people may no like that an application tells them how to write their name. So maybe the best way to format a name is to leave it as typed by the user.