Scott Hanselman has been posting weekly snippets of code in various languages … today he did Fibonacci sequences … by the way, the point of his posts is to get you to read code in other languages, not necessarily showing the best way to do things in any of the languages represented — you only solve fibonacci sequences recursively in demos, not in real life. Well, except perhaps in tightly optimized tail-recursion languages, maybe in Haskell …
That’s just beautiful
... incidentally, by default, PowerShell will only recurse up to 100 levels in the stack, so it kind-of sucks at recursive calls. You could also write that as a traditional function in powershell, but the way powershell passes arguments to functions makes it really awkward. I’ll put it on multiple lines so you can read it easier.
As a side note, that function actually runs measurably slower than the pipeline method … but speeds up noticeably if you stick a few [int] type specifiers in the right places so PowerShell doesn’t have to infer types — something to think about.
30 Jan
Microsoft has released a new website this week that I can only describe as the other half of GotDotNet. If you used to use GotDotNet, you know there were two reasons to go there: the third party projects that were hosted there, and the Microsoft Samples.
GotDotNet was replaced by CodePlex, and a lot of Microsoft samples are, in fact, showing up there. But the thing about sites like CodePlex and SourceForge is that they expect you to use all the services — source control, download mirroring, forums, wiki, etc. In fact, both CodePlex and SourceForge have “activity” metrics which include measurements of the amount of developer activity that happens in their source control.
Microsoft Code Gallery is for those “samples” projects and other projects that don’t need source control — If you were using GotDotNet or CodePlex or SourceForge just for the download mirroring, then you could consider using the CodeGallery. They provide download mirroring with your choice of software licenses, and even a small wiki and forum … it’s basically exactly the same as CodePlex, but without source control.
I’m not sure why the world needed such a thing separate from CodePlex. Wouldn’t they be better off just creating a special “we don’t need source control” option for CodePlex projects? I mean, maybe they were seeing a lot of projects on CodePlex that weren’t using the source-control and such — just using it as a dumping ground for downloads. (I know that happens a lot on SourceForge — they have TONS of projects which are there just for download mirroring). But I don’t see the need for a separate project, nor do I understand the desire to have it be on the MSDN domain.
I started this post a few weeks ago, but never quite got around to posting it, and then I saw today that Dmitry wrote about the memory problems with using measure-object and I thought this would be a great time to post this. Essentially, this is a continuation of my exploration of development in the PowerShell pipeline, and we’ll see some of the problems with the way that the pipeline works and what happens if you overlook the numbers of items that could be in the pipeline.
Dmitry was trying to count the number of users in Active Directory, and ran into ridiculous memory use when using Measure-Object because it appears to collect all the items in the pipeline into memory before it counts, resulting in huge amounts of memory use based on the number of objects you’re counting. Of course, for the purpose of counting, it’s a pretty obvious fix (as Dmitry explains), but for other purposes it may not be as obvious.
When I originally started this post, I was trying to upgrade my original Select-Random script (which was published in the PowerShell Community Extensions) to use reservoir sampling in an attempt to solve the same problem. Select-Random is a script which selects a random element from a collection or from the pipeline. In the past, if the elements were passed in as an argument, it simply collected them into an array (in a really inefficient way, but never mind) and then picks a random number and selects the item by index.
I started by improving the collection method to use an ArrayList and reduced the memory usage to about 1/5th of the original method, and then rewrote the whole script to use reservoir sampling instead. Greg wrote a good explanation of reservoir sampling but basically it’s an algorithm for choosing random items from a collection of unknown size without having to traverse the collection twice (to count it the first time and then to select) — in my case, the need to count the collection was requiring me to store the pipeline input in an array, so the improvement was more about memory use than about needing to enumerate the list twice. However, there is a downside: it’s slower. (more…)
Someone asked on the PowerShell Community Forum about a way to use google talk or jabber to send messages from PowerShell, and I got carried away …
The result is yet another CodePlex project: PoshXmpp. Basically I just took the event-based GPL library provided by AG Software (agsXMPP) and wrote a polling-based wrapper for it.
You can do pretty much anything instant-messaging related that you want to do with Jabber, from building bridges between different jabber transports, to notifying yourself of events on servers, etc. As an example, here’s the script for a Jabber bot that joins a chat (#PowerShell on irc.FreeNode.net by default) and then notifies us of new posts in an ATOM feed (by default, it monitors the PowerShell usenet newsgroup by using the Google groups ATOM feed).
Now, most of that script was spent parsing xml from the atom feed, so lets try another example. This script will join mirror a groupt chat to instant message. By default it joins the PowerShell IRC channel, and then instant messages you so that you can participate in the IRC chat via your instant messenger. You can register your Jabber account with the AIM, ICQ, MSN, or other transport on it’s Jabber server and then use this to mirror IRC to your MSN account or to AIM on your phone or whatever
. I’ve been using im.flosoft.biz which allows you to not only register new accounts on the web, but also register them with the transports through a web form.
There’s another example on the PowerShell Xmpp for Jabber project page, showing how you can use this to read a chat room out loud (sometimes I run that when I’m just monitoring a chat room while I do other work). You could also use the Xmpp library for notifications — set up your Windows 2008 server to instant message you when IIS fails, or when you receive an email, whatever. I’ve even been playing with Invoke-Expression … you can actually write a script to do powershell remoting over Jabber (yikes).
I’m going to try using Pibb for comments for a while. It’s a live web-chat which uses OpenID for authentication, which most people will have already, after all, you can use your AIM account, Wordpress.com account, LiveJournal … Or you could use your Yahoo! login via idProxy or your Jabber (or Google) account via XMPP South Africa or sign up for a free OpenID from http://myopenid.com/ or https://pip.verisignlabs.com/ or http://ClaimID.com … I could go on, but this is really just a test post to see if Pibb commenting is working.
The fact is that it’s just not up to snuff right now. The wordpress plugin itself required quite a bit of wrangling (read “rewriting”) to get it to work at all, and then once I got it working, it was rather disappointing. I had to manually rewrite the “comments” portion of my template to use Pibb instead of my current system, and when I did I still wasn’t satisfied.
So instead, I’m just using OpenID for the regular comments. I think I may choose to have comments without an OpenID emailed to me instead of being displayed, but for now I’ll wait and see.