Scott Hanselman

The Weekly Source Code 16 - Duct Tape Edition

February 21, '08 Comments [18] Posted in Source Code
Sponsored By

duct-tape-roll A few weeks ago I interviewed Steven Frank (blog), co-owner of Panic and a Mac Developer (who I went to college with). After that interview I stumbled upon the very recently release NSDuctTape project. First, how can you not like a project named after Duct Tape. Second, whenever I hear that some code will bridge to completely incongruent and unbridgeable things, I gotta check it out. What can I say, if there's a freak somewhere that promises to tape two things together, I want to see it! ;) (And I mean freak in the most positive way!)

NSDuctTape is niche, to be clear, but if you want to write .NET code using Mono on the Mac and you want access to the Objective C Cocoa libraries, this is your one-stop shop. (NOTE: If you do download his source, you'll likely have to pull the files out one at a time because there's Mac files in the zip with the same names as folders and Windows doesn't like it.)

And so, Dear Reader, I present to you sixteenth in a infinite number of posts of "The Weekly Source Code." Here's some source I was reading this week.

Dave, the author, hasn't check on Mono's support for Linq, but he uses C# 3.0 features to create his own LINQ-lite helper methods. I found this to be a clever "punt."

	
internal static class Enumerable
{
	public static IEnumerable Select(IEnumerable list, Converter convert)
	{
		foreach (TInput value in list)
			yield return convert(value);
	}

	public static IEnumerable SelectMany(IEnumerable list, Converter> convert)
	{
		foreach (TInput value in list)
			foreach (TOutput converted in convert(value))
				yield return converted;
	}

	public static IEnumerable Where(IEnumerable list, Predicate predicate)
	{
		foreach (T value in list)
			if (predicate(value))
				yield return value;
	}

	public static List ToList(IEnumerable list)
	{
		List result = list as List;
		return result ?? new List(list);
	}

	public static T[] ToArray(IEnumerable list)
	{
		return ToList(list).ToArray();
	}
}

Because he's "thunking" (not the technically accurate word, but I like saying it) down into unmanaged code that needs to have handles allocated and deallocated, he creates an HGlobal wrapper class using my most favorite .NET BCL pattern, IDisposable. Classic stuff, simple and works great.

...snip...

public void Dispose()
{
	if (_hGlobal != IntPtr.Zero)
		Marshal.FreeHGlobal(_hGlobal);
	_hGlobal = IntPtr.Zero;
}

private DisposableHGlobal(IntPtr hGlobal)
{
	_hGlobal = hGlobal;
}

public static DisposableHGlobal StructureToHGlobal(T value)
	where T : struct
{
	DisposableHGlobal result = new DisposableHGlobal(Marshal.SizeOf(value));
	Marshal.StructureToPtr(value, result.ToIntPtr(), false);
	return result;
}
...snip...

Finally, in his application managed "wrapper" he spins through his chosen System.Types and registers each of them with ObjectiveC. This interop is one way, meaning that he's choosing to expose his .NET types as Objective C classes.

public static void Run(string nibFile, IEnumerable exposedTypes)
{
	ObjectiveCClass nsAutoReleasePoolClass = ObjectiveCClass.GetClass("NSAutoreleasePool");
	IntPtr autoReleasePool = nsAutoReleasePoolClass.Instantiate();
	ObjectiveCMethods.SendMessage(autoReleasePool, ObjectiveCMethods.SelectorFromString("init"));
	try
	{
		IntPtr process = IntPtr.Zero;
		GetCurrentProcess(ref process);
		TransformProcessType(ref process, ProcessType.ForegroundApplication);
		SetFrontProcess(ref process);

		Registrar.Initialize();

		foreach (Type type in exposedTypes)
		{
			ObjectiveCNameAttribute attribute = MemberInfoUtility.GetCustomAttribute(type);
			Registrar.RegisterClass(attribute != null ? attribute.Name : type.Name, type);
		}

		ObjectiveCClass nsBundleClass = ObjectiveCClass.GetClass("NSBundle");
		IntPtr name = NativeString.StringToNativeString(nibFile);
		ObjectiveCClass nsDictionaryClass = ObjectiveCClass.GetClass("NSDictionary");
		IntPtr key = NativeString.StringToNativeString("NSOwner");
		ObjectiveCClass nsApplicationClass = ObjectiveCClass.GetClass("NSApplication");
		IntPtr sharedApplication = ObjectiveCMethods.SendMessage(nsApplicationClass.ToIntPtr(), ObjectiveCMethods.SelectorFromString("sharedApplication"));
		IntPtr nsDictionary = ObjectiveCMethods.SendMessage(nsDictionaryClass.ToIntPtr(), ObjectiveCMethods.SelectorFromString("dictionaryWithObject:forKey:"), sharedApplication, key);
		IntPtr zone = ObjectiveCMethods.SendMessage(sharedApplication, ObjectiveCMethods.SelectorFromString("zone"));
		ObjectiveCMethods.SendMessage(nsBundleClass.ToIntPtr(), ObjectiveCMethods.SelectorFromString("loadNibFile:externalNameTable:withZone:"), name, nsDictionary, zone);

		ObjectiveCMethods.SendMessage(sharedApplication, ObjectiveCMethods.SelectorFromString("run"));
	}
	finally
	{
		ObjectiveCMethods.SendMessage(autoReleasePool, ObjectiveCMethods.SelectorFromString("release"));
		autoReleasePool = IntPtr.Zero;
	}
}

It's inside the RegisterClass where he creates Objective C classes for each .NET class, lazily making class definitions, and poking values into them. He's using "reflection" on both sides...reflecting over the .NET types, methods, etc and dynamically creating the same types, methods, etc on the ObjectiveC side.

Freaky and fun!

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Learning Languages Fast - Can you Flush the Toilet in Zulu?

February 20, '08 Comments [80] Posted in Musings
Sponsored By

Tim Ferriss has a penchant for languages. His post How to Learn (But Not Master) Any Language in 1 Hour got me thinking because he gives eight sentences that you can use to get a really good understanding of how a particular language is constructed.

The apple is red.
It is John’s apple.
I give John the apple.
We give him the apple.
He gives it to John.
She gives it to him.
I must give it to him.
I want to give it to her.

Tim uses these sentences because they show how verbs are conjugated between speaker and subject, they show gender, number, direct and indirect objects, negations and tense. Fantastic.

Interestingly, if you ask two speakers of the same language, you might get different answers.

Here's my wife's answers for the first 6 sentences in isiNdebele:

I ephuli libomvu.
Li ephuli likaJoni.
Nginika uJoni i ephuli.
Simnika i ephuli.
Ulinika uJoni.
Ulinika yena.

And here is the same from her sister.

I apple leli libomvu
Ngelika Johane.
Ngipha uJohane i apple.
Siyamupha i apple.
Uyalipha uJohn.
Uyalipha yena.

There's a few things interesting about this, other than two sisters will never agree on anything. Deconstructing isiNdebele (isiZulu), we see:

  • Nouns start with a vowel as in iapple or iephuli or uJohn. (There are many noun classes, not every one starts with i.)
  • The prefix si- indicates we and ya- is present tense, so siyamupha is we (are giving) give
  • Mo uses ukunika to say "to give" and her sister says ukulipha meaning "to hand over" but they really both mean to give. (From http://isizulu.net/) Then her sister goes an uses ukulipha in her 3rd example, so get good examples!
  • There's no gender in the last two, so uyalipha can mean both he gives and she gives.
  • I can see how some verbs are conjugated as in ngipha (I give) and uyalipha (he/she gives)

If there are borrowed words in the language that resonate with me (meaning, I can easily remember them) like imota (car) or ifoni (phone) I can now put together sentences like Imota kaScott (It is Scott's car) or if I learn a few basic infinitive verbs like ukufuna (to want), ukufunda (to learn) or ukucela (to request) I can assemble sentences like:

  • ngicela imali - I want (request) money
  • ngifuna ukufunda isiZulu - I want to learn Zulu

What do you think of Tim's sentences? While you'll not become fluent, it seems to me that this a fun an effective way to learn more than just memorized phrases. You'll certainly build a nice base to build on if your brain works this way.

I know I have a very international readership, and I love travel, so I'd love it if you'd all add in the comments the native translations of these eight sentences in the format:

The apple is red. - PHONETIC PRONOUCIATION - NATIVE ALPHABET
etc...

As an aside, Richard Sprague once said that if you really want to know if someone who says they know a language is really fluent in that language, you should ask them how to say "Flush the toilet." Why that phrase? Because most folks who've cobbled together an understanding of a language from High School or phrase books might be able to assemble an awkward sentence that sounds like "push the button to make the water come" rather than the colloquial or commonly used phrase. For example, in French, "tirer la chasse" translates to the English "pull the chain." Only a person who has lived in a country and gained some fluency knows these kinds of colloquialisms. So the next time your office mate says he knows six languages...find out in how many he can say flush the toilet.

Related Posts

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Knowing when to ask for help - Microsoft SharedView

February 17, '08 Comments [30] Posted in Reviews | Tools
Sponsored By

5231481f-1120-47c9-8279-f005c7c74ee0I've been a mess all weekend. Truly. I've been as stressed as I've been in my life, trying to learn a new technology deeply enough to teach it. It's one thing to learn a tech enough to be capable, but in my new role at Microsoft I'm feeling more of a sense of responsibility than I did when I was just Joe Public Who Talks Loud.

I mean, if you speak with authority, you ought to know something about the topic, right? ;)

Anyway, I finally realized that I wasn't reaching out enough to my network of friends/peers/colleagues. This is probably because I'm still getting used to working from home. It's lonely sometimes, and I can't just drop over to a friend's cubicle. This solitary feeling has made me "buckle down" and try to figure things out more that I used to, since I can't just pop into an adjacent office.

Well, my personal lesson for today is knowing when to ask for help.

I've become REALLY good at remote tools since I've been doing this remote thing (even before). I've used NetMeeting, Remote Assistance, GoToMeeting, VNC, CoPilot.com, LiveMeeting, Hamachi and a dozen things in-between.

Nothing has come close to the ease of Microsoft SharedView. It isn't heavyweight like LiveMeeting or fuzzy like VNC, plus it lets you show your screen to up to 15 other people.

One of the coolest things is that everyone gets their own "personal mouse pointer" with their name hanging off it, so you can see what someone is referring to when they are talking!

Today when I was at life's lowest ebb, I called Rob and Phil and said "guys, I need a code review." I did a Vonage conference call and they logged into the Shared Session using just my email address. Bam. No pausing, no firewall futzing, it's the greatest thing since FolderShare.

I hope it stays free and wonderful, because it saved my bacon this evening. Whew. Now I can sleep.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

This Week on Channel 9: Feb 15

February 17, '08 Comments [11] Posted in ASP.NET | Microsoft | Podcast
Sponsored By

While I was up in Redmond for the week, I stopped by the "This Week on Channel 9" show with Dan Fernandez and Brian Keller.

It's a weekly show and this is the 3rd episode. I had a great time and I'm hoping they ask me back, maybe once a month.

Someone asked me on Twitter "you're becoming a media person, do you write code anymore?"...and I cried. Yes, I write code every day, phooey on you. ;) I'm just having fun visiting my buddies, but I'll try to show you some more code.

I kind of messed up their show because we were screwing around and they kept laughing. You can see some out-takes (bloopers) if you watch until the very end, after the credits at about 25:00.

I hope you enjoy it.

NOTE: If you want a FullScreen WMV you can get it, as well as a downloadable version or view it on the Channel 9 site.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Geek Developer Cribs on10

February 15, '08 Comments [39] Posted in ASP.NET | Channel9 | Microsoft | Personal | Podcast | Programming | Remote Work
Sponsored By

Rory and Chris came over to the new house recently and this is what came of that visit. The video is up on10.

"In this edition of Show Us Your Home, Scott Hanselman - a guy who works out of his home office - shows us his Geek Developer Crib with Rory Blyth on the glass and Chris Sells on the open mic.
Scott takes us on a tour of his OCD wiring, Windows Home Server, Xboxen, and personal effects. Special thanks to Rory for producing."

Related Posts You Might Enjoy

  • Wiring the house for a Home Network
  • Part 2 - Design Q&A
  • Part 3 - ISP Hookup
  • Part 4 - Thank You Cat 6 Gigabit Ethernet
  • Part 5 - Gigabit Throughput and Vista
  • Part 6 - Identifying Performance Factors of Home Gigabit Networks
  • How To: Use Google Earth or Virtual Earth to Visualize a New House Lot - Part 1
  • How To: Use Google Earth and SketchItUp to Visualize a New House Lot - Part 2
  • Podcast - Planning, Placing and Building a House with Google Earth and SketchUp
  • About Scott

    Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

    facebook twitter subscribe
    About   Newsletter
    Sponsored By
    Hosting By
    Dedicated Windows Server Hosting by SherWeb

    Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.