| William's profileBill PierceBlogLists | Help |
|
6/29/2006 Google CheckoutIf everyone isn't blogging about it yet, they will be. Google launched Google Checkout.
At first glance, here's what I like about it:
Everyone thinks that Microsoft is the big evil empire but I think because they live with that image they work doubly hard to prove that isn't the case. I am actually more leary (sp?) of Google and their access to massive amounts of data, now including my purchases and credit card information. Thoughts from the peanut gallery? 6/21/2006 Switch HittingMy right wrist was giving me grief the last few days. My finger joints are also feeling a bit stiff. The obvious answer is to not spend 10-12 hours a day on the computer. My job and hobbies make that difficult. So, I used the mouse with my left hand all day yesterday. It wasn't as bad as I thought. The most difficult task was dragging windows around on my dual monitor setup. I also reversed the mouse buttons and increased the mouse speed to help compensate. I don't think I'll be playing Counter Strike with my left hand anytime soon, but its good to know I can ruin both hands with carpel tunnel. 6/14/2006 Javascript Keystroke HandlingHere is a script I hacked together to capture keystrokes. Should be cross browser. Can also intercept browser buttons (like PageDown/PageUp).
function addListener(a,b,c,d){if(a.addEventListener){a.addEventListener(b,c,d);return true;}else if(a.attachEvent){var e=a.attachEvent("on"+b,c);return e;}else{alert("Handler could not be attached");}}
function bind(a,b,c,d){return window.addListener(a,b,function(){d.apply(c,arguments)});} function handleKeystroke(evt)
{ // Grab the cross browser event if( !evt ) evt = window.event; // Character code of key pressed
var asc = !evt.keyCode ? (!evt.which ? evt.charCode : evt.which) : evt.keyCode; // ASCII character of above code
var chr = String.fromCharCode(asc).toLowerCase(); for (var i in this)
{ if (asc == i) { this[i](evt); break; } } } function cancelEvent(evt) { evt.cancelBubble = true; evt.returnValue = false; if (evt.preventDefault) evt.preventDefault(); if (evt.stopPropagation) evt.stopPropagation(); return false; } // // KEY COMMANDS
var keyMap = new Array(); var PAGE_DOWN = 34; var PAGE_UP = 33 var ARROW_DOWN = 40 var ARROW_UP = 38 keyMap[PAGE_DOWN] = pageDown;
keyMap[ARROW_DOWN] = pageDown; keyMap[PAGE_UP] = pageUp; keyMap[ARROW_UP] = pageUp; // function pageDown(evt)
{ alert("You clicked DOWN and we are going down"); } function pageUp(evt)
{ alert("You clicked UP but I am canceling that request"); cancelEvent(evt); } // Add the keydown listner to the document object for global capture bind(document, 'keydown', keyMap, handleKeystroke); 6/12/2006 IE-FF String ManipulationI was porting Stephen Toub's excellent Levenshtein Edit Distance algorithim to Javascript. The original implementation is shown below (I apologize for my short-hand hacker shortcuts):
function LevenshteinDistance(x, y)
{ var n = (x.length || 0); var m = (y.length || 0); if( n == 0 ) return m; if( m == 0 ) return n; var curRow = 0, nextRow = 1; var rows = new Array(new Array(m+1), new Array(m+1)); for( var j=0; j<=m; rows[curRow][j]=j++); for( var i=1; i<=n; i++ ) { rows[nextRow][0] = i; for( var j=1; j<=m; j++ ) { var d1 = rows[curRow][j] + 1; var d2 = rows[nextRow][j-1] + 1; var d3 = rows[curRow][j-1] + ((x[i-1] == y[j-1]) ? 0 : 1); rows[nextRow][j] = Math.min(d1, Math.min(d2, d3)); } if( curRow == 0 ) { curRow = 1; nextRow = 0; } else { curRow = 0; nextRow = 1; } } return rows[curRow][m]; } Running the code appeard to work fine with my test cases in FireFox. So, I ran tests in IE and was getting different results. There were no errors, no exceptions, just a different distance value returned. I banged my head for 20 minutes trying to figure it out.
Finally a coworker was walking through the code, alerting some debug info, and we came accross the problem. It appears that IE does not support array syntax when accessing characters in a String:
var d3 = rows[curRow][j-1] + ((x[i-1] == y[j-1]) ? 0 : 1);
The output of x[i-1] and y[j-1] was always 'undefined' in IE. The final version below uses charAt():
function LevenshteinDistance(x, y)
{ var n = (x.length || 0); var m = (y.length || 0); if( n == 0 ) return m; if( m == 0 ) return n; var curRow = 0, nextRow = 1; var rows = new Array(new Array(m+1), new Array(m+1)); for( var j=0; j<=m; rows[curRow][j]=j++); for( var i=1; i<=n; i++ ) { rows[nextRow][0] = i; for( var j=1; j<=m; j++ ) { var d1 = rows[curRow][j] + 1; var d2 = rows[nextRow][j-1] + 1; var d3 = rows[curRow][j-1] + ((x.charAt(i-1) == y.charAt(j-1)) ? 0 : 1); rows[nextRow][j] = Math.min(d1, Math.min(d2, d3)); } if( curRow == 0 ) { curRow = 1; nextRow = 0; } else { curRow = 0; nextRow = 1; } } return rows[curRow][m]; } File this one under GOTCHAS 6/5/2006 QuoteablesOren Eini (aka Ayende) maintains an excellent blog on .Net programming focusing on IoC, DI, nHibernate, TDD, and good design in general. Reading his blog has helped me become a better programmer. If you aren't a regular listener, I highly recommend his site.
For other great blogs, checkout my Blogroll.
Anyway, Oren provided an excellent quote on his blog today that made me and my co-workers chuckle:
“The most configurable system is java.exe, and it is configured via javac.exe”
Hope you enjoyed it as much as I did.
-Bill |
|
|