Method I Wish Was There: RegexIsMatch

I don’t like the verbosity of a couple days ago use of Regex(). That is not to say that I don’t still find the Regex extension method useful, but if I’m just matching I should say so.

var re = @“\d+-\d+-\d+-\d+”.Regex();
var match = re.Match(“things 10-12-5-1”);
//do things with match
var othermatch = re.Match(“stuff 1-6-8-9”);

Regex extension method is great. But for a Boolean statement I should be able to just say “RegexIsMatch?”

if ( someText.RegexIsMatch(@”\d+\.\d+”) ) …

Method I Wish Was There: ContainsDigit

Sometimes when dealing with lots of strings or parsing text, I don’t always want to turn directly to regular expressions. I might want to just say “if this thing has a digit”.

public static bool ContainsDigit(this string source) {
  return @"\d".Regex().IsMatch(source);
}

This uses yesterday’s Regex() extension method on string and simply returns true or false, exactly as it reads.

string field1 = GetFieldFromSomeParserOrSomething();
if (field1.ContainsDigit()) …

Method I Wish Was There: Regex

I don’t want to have to pull in System.Text.RegularExpression every time I want a new Regex. Perl, boo, awk and javascript just say // for regex. In C#, I can just say “abc\d{3}”.Regex().

public static System.Text.RegularExpressions.Regex Regex(this string source)
{
  return new System.Text.RegularExpressions.Regex(source);
}

Now I can write:

if (“there are (\d+) lights”.Regex().Match().Groups[1].Value > 0) …

Method I Wish Was There: string.Contains

Sure, string has a member method of Contains, but its an exact comparison. What I really want is something that takes a StringComparison argument as well.

public static bool Contains(this string source, string search, StringComparison comparison)
{
  return source.IndexOf(source, 0, comparison) > -1;
}

Not much to this extension method, but it gives me the semantics that I want.

if (foo.Contains("bar", StringComparison.CurrentCultureIgnoreCase)) …

FOSDEM Videos Online 2

I’ve been watching more FOSDEM videos. They are very cool. Watching in 720p is awesome.

Alan McGovern on The Evolution of MonoTorrent – By far the best bittorrent library for .net. Alan talks about the challenges of writing a library that will handle things like hundreds (if not thousands) of simultaneous socket connections, why using threads to do so is wrong, and how good design and the async pattern made it easy and fast.

Stephane Delcroix on Image Processing – I was really looking forward to this one – Mono.SIMD is awesome – but the whole thing is code and the non-bold small font on a white background did not get picked up by the projector. Needless to say that Mono.SIMD is still awesome, but the talk isn’t really worth watching. I’ll be it was cool live though. If you don’t know Mono.SIMD is yet another Mono innovation that is Mono-only. It provides an object oriented api for the underlying SIMD instructions in modern cpus. These are the SSE instructions that you see listed in the spec about your Core2 CPU.

Jeremie Laval on Parallel Fx – This is NOT just another talk on Parallel Fx aka Task Parallel Library+PLINQ. Jeremie actually wrote Mono’s implementation of TPL. While its great to see Stephen Toub show use cases of TPL, the geek in me wants to see what is under the hood. Jeremie gets into some of the challenges in implementing the scheduler and library. He also mentioned that .NET4’s thread pool actually uses the TPL scheduler underneath. I didn’t know that. He also shows a demo of Future<> which I had never considered. Chaining Future<>’s in a tree like manner in order to create parallel delayed evaluation.

Andreia Gaita on Moonlight and You – Why moonlight is important to mono? WOW! They are working on XAML designers for MonoDevelop. I can’t imagine how awesome moonlight development will be on Linux in the future. Moonlight is super close to working in Chrome on Linux. mxap –desktop lets you create moonlight desktop apps launchable with the mopen command. Very cool. Desktop apps in moonlight. Very cool pixel shaders on video in moonlight 3.

Jim Purbrick on Building The Virtual Babel – Mono in Second Life – The virtual worlds in Second Life and how it has progressed for 7 years. Very cool to hear how an existing language LSL and existing virtual machine was moved to a new virtual machine, Mono. I’ve never heard of JavaGoX or Brakes so listening to the description of script migration was rather mind blowing.(very cool)

Joe Shields on OSCTool – learning C# and Mono by Doing – While probably not of interest to a lot of developers, this one hit close to home for me because I was in a position pretty close to Joe’s about 8 years ago. Of course I didn’t know .NET at the time and Mono was still very young, but I do recall playing with some ASMX web services to do things similar to what he was doing. Joe makes a point that python, perl, C and even Java have cross platform difficulties (think HPUX, Itanium, Tru64 etc) of which Mono seems to mitigate much. The audio goes out for a few minutes in the middle, just skip to about 17:00 and watch the demo.

Mirco Bauer on Smuxi – IRC in a modern environment – Just an irc client with some special features. I’ll definitely be looking into this because I’m an IRC junkie, and it also has the exact twitter client that I’ve been looking for since I started using twitter.

FOSDEM Videos online

As a .NET programmer in my day job targeting Windows desktop applications (winforms and wpf), I don’t get to stay on top of much ASP.NET or Mono. The ASP.NET stuff I feel like I have a good enough handle on via channels I use to stay on top of .NET in general (user groups, blogs, etc). Mono gets much less coverage there.

Luckily there are some awesome (720p quality) videos from FOSDEM 2010 of some Mono centric presentations.

Lluis Sanchez Gual on MonoDevelop – I knew MonoDevelop 2.2 had some awesome in it, but I didn’t know about some of the code generation things in it. Think R# mixed with CodeRush. It is very sweet.  Lluis’s Blog is always a good read too.

Ivan Porto Carrero on IronRuby – Poor Ivan had some demo troubles, but overall the presentation was excellent. It is VERY cool to see a Banshee add-in written in Ruby. I don’t think I was reading Ivan’s Blog before, but I am now.

Miguel on Mono – I think this is kind of Miguel’s “State of the Monkey” of the day. Its a status overview with a few deep dives into things. I especially thought it was cool that he went deep (well, deeper than most) into Expression<>.

The videos seem to be coming out slowly. I’m posting these brief summaries of the videos as I watch them, so expect me to link to more as I watch them.

Windows 7 mp3 tag editor

I just accidentally found Windows 7’s built in mp3 (and presumably other metadata, exif perhaps) tag editor.

I looked for this thing for what felt like hours over the past year. Eventually I sucked it up and downloaded mp3tag, but its still nice to know that this is there for the next time.

Normally when browsing my mp3 files I see a window that looks like this:

Stromkern1

See that summary pane at the bottom? Select a file with editable metadata, like an mp3 and resize that pane. Then click one of the metadata values.

Stromkern2

Wow, I wish that had been more discoverable. –1 point for Windows 7 for making that far from intuitive, but 1 point for Windows 7 for having the feature.

LINQ Abuse with the C# 4 dynamic type

With C# 4 adding some support for dynamic typing one of the first thing that I wanted to do is use it with LINQ.

I want to do this:

dynamic x;
var h = from y in x where y == 1 select y.something;

But I get error messages on both where and select that says

Query expressions over source type ‘dynamic’ or with a join sequence of ‘dynamic’ are not allowed

Major bummer.

But surely there is something I can do. 🙂

*the title of this post starts with LINQ abuse… please don’t comment about how stupid and evil this is. I know it. Instead, consider this an exercise in getting to know C# a little better.

The dynamic type is just sugar for the object type and some attributes to which the compiler pays attention.

Lets use object…

object things = new[] { 0,1,2,3,4,5,6,7, };
var whatIwant = from thing in things
                            where thing % 2 == 0
                            select thing;
// or if you like longhand:
var wiw = things.Where(thing => thing%2 == 0).Select(thing => thing);

How does this compile? Well, by making Where and Select resolve to extension methods on object instead of extension methods on IEnumerable<T> (which is what people USUALLY think of when they think LINQ).

public static IEnumerable<dynamic> Select(this object source, Func<dynamic, dynamic> map)
{
    foreach (dynamic item in source as dynamic)
    {
        yield return map(item);
    }
}
public static IEnumerable<dynamic> Where(this object source, Func<dynamic, dynamic> predicate)
{
    foreach (dynamic item in source as dynamic)
    {
        if (predicate(item))
            yield return item;
    }
}

Extension methods on object, then cast to dynamic (extension methods aren’t allowed on dynamic).

It should be short work to fill out whatever LINQ methods are necessary to make whatever LINQ expressions you wish work against dynamic (object) and now you can use LINQ with a source that is typed dynamic.

Version Control Tools Is More Than Just The Tools

Martin Fowler has an excellent post on Version Control, and he almost got all of the way there, but for more than just a tiny development shop I think he missed a few important pieces.

http://martinfowler.com/bliki/VersionControlTools.html

Martin does say that Mercurial and git get most of the attention and that the choice between the two come down to “…the shadow of github.”

He is dead right, but I’d argue that this is where the recommendability starts. github is awesome. Ask anyone who has used it and I’ve rarely heard anything negative said. But for anything but the tiniest organization outsourcing your VCS to github is not a practical option. There could be IP requirements or legal requirements about keeping source private and just the over cautious nature of people is to want to run VCS in house. github is not open source. gitorious is.

http://gitorious.org/about

You can go checkout the source code to gitorious and run your own in your own internal organization. AWESOME!

I’d never heard of the Mercurial hosting project http://bitbucket.org/ until i read Martin’s post. That is great to know that it exists. But…

The most mature solution is something which is also open source, has the best project management implementation, has the best bug tracking implementation and integration and is just all around awesome. Its called Launchpad.

http://launchpad.net/

Yes, the same software that drives launchpad is open source and you can install and run it for your own team, company, organization or however you are structured.

Launchpad is written by open source volunteers as well as employees of Canonical. You can buy support for Launchpad from Canonical. Launchpad uses the lesser known bazaar DVCS. Martin kindly left out the DVCS which is used by the most prominent Linux distribution, ubuntu, and the most prominent open source database software, MySQL.

Just like GIT is not going away, because linux uses it, and Mercurial isn’t going away because python and google use it, bazaar is not going away, because ubuntu and mysql use it.

My point is that the web hub to your code and the tools surrounding how you get to your code is just as important as the base DVCS tool itself. SVN has always been good, but ViewVC makes it great. GIT is good. github makes it great. Launchpad makes bazaar great. It makes bazaar at least as good of a choice as either git or mercurial.