Archive for the ‘Work’ Category

7 Days, 7 Nights, 7 Minutes

Friday, October 19th, 2007

One of the projects I’ve been helping get off the ground is I Live Inspired, an inspirational text-messaging service. The site is good, and the concept is great. One can never have too much inspiration.

The founders, Rob and Chris, are on a mission. They are seeking an audience with the Dalai Lama in Indiana and are walking 7 days in hopes of getting a 7 minute audience. They are also keeping a blog of their adventures. These guys are not much older than me and are trying to get a positive service off the ground. Whether they have audience with the Dalai Lama or not, the experience of the walk, the people they’ve met thus far, and the people they’ve yet to meet, will change their lives. And we get to share in that through their writing.

So take a minute, read up on what the future of America is up to, and if you want some extra inspiration delivered to your phone daily, consider signing up for one of the many great communities at I Live Inspired.

* Disclaimer: While I have helped create the site, I do not receive any compensation for spreading the word. I think its a great service and deserves notice.

Remove nested arrays in javascript using the prototype library

Tuesday, May 22nd, 2007

I have been playing with some drawing code in javascript, storing coordinates and using them later on in the application. My list of coordinates is of the form [ [id1, x1, y1, width1, height1], [id2, x2, y2, width2, height2],…]. A requirement of the application is that a user can delete a set of coordinates from the list. Using prototype.js, I created a simple function to remove the nested array based on the id.

// remove an array from the list based on the id
function remove(id, list) {
    return $A(list).map(
        function(arr) {
            if ( $A(arr).first() == id ) { return ; }
            else { return arr; }
        }).compact();
}

In your favorite editor, this function can be a one-liner, but spacing helps here for clarity and formatting on the page. Onward!

So what’s happening? The first thing we do is wrap list with the $A() call to ensure we have access to the extensions prototype gives us for arrays (I’m calling the parameter a list because I’m on an Erlang kick and it has infiltrated my core!). Once extended, we call the map function to iterate through the list and apply the supplied function to each element in the list (in this case it is a list of arrays, so each element passed to the supplied function will be an array as well).

Within the supplied function, we are dealing with a single array of the form [id, x, y, width, height], so $A(arr).first() returns the id of the array. This value is compared to the value of the id parameter and if it matches, returns nothing, or ‘undefined’ in Javascript. If the ids don’t match, it returns the array unaltered. As the map function iterates through the list, a new list is created containing the results of the supplied function. So the return value of the map function call is an array. We then call the compact function on the resulting array, which removes any undefined values from the array, essentially leaving only those arrays that did not have the id passed in.

This function is fairly specialized; the requirements for the function are fairly specific. A more general function could be written but that is an exercise left to the reader.

Recursive FTP

Wednesday, May 9th, 2007

So you want to download some files from an ftp server, but they are contained in more than one subdirectory. With a straight ftp client, you would have to recurse through all of the directories and mget each directory’s contents manually. Never fear, though, there is a little utility that can help – wget.

> wget -r ftp://user:pass@ftpsite.com/directory .

If the ftp site allows anonymous logins, you can omit the user:pass portion. This will get everything…it is left as an exercise to the reader to customize the command.

Drop down menus

Thursday, April 26th, 2007

As we all know (actually, many probably don’t know) Internet Explorer has claimed many hours of developer time trying to get a feature working with the quirks of IE. One quirk that I’ve dealt with recently was the :hover pseudo-class and its implementation across various browsers. The most notable quirk is that IE only supports the :hover on anchor tags (<a>). What’s a fella to do when he wants a drop down menu that displays the sub-menu when the mouse is hovering over an li element? Write some Javascript to aid IE in rendering the drop-down effect properly.

The first draft of our menu is here. If you are unfortunate enough to be using IE, you probably won’t see the sub-menu items. So how do we negotiate this? With a little extra class, and some Javascript.

The second draft of our menu can be found here. The differences to note:

  • The li:hover rule is now accompanied by a li.over as well
  • The function fixHover()
  • The function init()

So we added a rule that says any ul with a parent li with a class of over will also get the styling that a ul with a parent with li:hover gets; in this case – display the underlying ul. Next, we added a function (fixHover) that took an element, and retrieved all of it’s immediate children nodes. We then iterate through the list of children, basically adding two events, “mouseover” and “mouseout”, to for each element to observe. For “mouseover” events, append the classname “over” to the element; on “mouseout” events, remove the “over” classname. The essence here is that :hover is the CSS equivalent of observing the “mouseover” and “mouseout” events. The draw back to our solution is that if Javascript is turned off, the sub-menus remain hidden from the user.

NOTE: I am not a designer, so the purpose of this article is to merely illustrate the ability to apply hover-type functionality to any element on the page in IE and not showcase my ability to make things look nice.

Another feature to mention is the init function and the Event.observe() call, which calls the init function after the window has finished loading the page. This is a must because we cannot apply the “mouseover” and “mouseout” event observations until the nodes have been created in the DOM. Best to leave this until the window has loaded. Both of the functions rely on the prototype.js library to retrieve the child nodes, iterate through the nodes, and attach events to the nodes. It is possible to do this without prototype or with another library, but I leave it up to the reader to translate this code to their library of choice.

Collabofit is live (kinda)

Thursday, September 14th, 2006

We pushed collabofit.com live the other day, restricting access to personal invites for the time being. Interested in the experience? Want to be an early adopter? Shoot me a mail and I’ll set you up. Check out Katanaa and the collabofit blog for more information.

Linux-Fu For File Deletion

Monday, August 14th, 2006

Having setup a backup server for work and home, I was looking into how to remove archives that were a week or older. Initially, I wrote a simple script to search a path for files that matched a naming convention and whose creation dates exceeded the week limit. This was fine and dandy but not the most elegant and didn’t really increase my linux-fu. So I decided to delve deeper and find a good one-liner that embraces the linux way of life. My result:

rm `find /path/to/backups/ -mtime +7 -name ‘*.bz2′`

The breakdown:

  1. rm – remove files, any n00b knows that…
  2. the ` mark surrounding the argument passed to rm denotes that the shell should use the result of executing the command(s) inside the ticks as the arguments to rm. This tick can be found to the left of the number one.
  3. find does that, finds files and lists them to stdout, generally the screen.
  4. /path/to/backups is the path find begins its search from…
  5. -mtime +7 says find files who’s modification time is more than 7*24 hours ago, effectively a week or more in the past.
  6. -name ‘*.bz2′ matches any file ending in with the .bz2 extension.

Slap that sucker into a cron job and you have yourself an automated way to maintain a certain number of files that do not exceed a certain time frame. find is enormously useful and has so many wonderful options to assist you in your file searching. So there you have it, a simple one-liner to remove files based on their timestamp.

New Webserver To Play With

Saturday, July 22nd, 2006

So what’s a guy to do on Friday night / Saturday? Probably look for something really nerdy to do, right??!?? That’s probably the response of most guys right? Okay, maybe not; but when your significant other is still playing in Florida, and the movie theatre is running on partial power and not showing movies, it is easy to become absorbed by something new and shiny. My new toy: YAWS, (Yet Another WebServer for those not in the know).

I am a web developer and as such spend a healthy chunk of time leveraging the Apache webserver to serve up the web applications I develop. So when another web server makes performance claims like this (thoughput ( load(x-axis) vs KBytes/second(y-axis)):

Apache vs YAWS performance

Our figure shows the performance of a server when subject to parallel load. This kind of load is often generated in a so-called “Distributed denial of service attack”.

Apache dies at about 4,000 parallel sessions. Yaws is still functioning at over 80,000 parallel connections.

Intruiged, I upgraded my version of erlang, got myself YAWS up and running, and am now delving into the world of erlang web development. I think I’ll write a simple application in both erlang/YAWS and PHP/Apache and run some benchmarks to test these claims myself. To see my YAWS development, tune your browser to my compy. Currently it only has the default site, as my erlang fu is in development.

The graph is non-trivial and if it proves to be true, even partially true, that YAWS can handle 20x the number of parallel requests Apache can, that may be just the advantage we need over our competition.

More to come…

Ah Ha!

Wednesday, July 19th, 2006

Continuations…frequently referred to by Paul Graham as, to paraphrase, awesome. With all the reading I’ve done regarding continuations, I’ve never really seen the big deal about them.  Turns out its because I didn’t get it. Imagine that… Fortunately for me, I have continued to read about them and finally had the “ah ha” moment. Reading this page describing fuctional programming, the author explains continuations in way that finally made it click:

A “continuation” is a parameter we may choose to pass to our function that specifies where the function should return. The description may be more complicated than it sounds. Take a look at the following code:

int i = add(5, 10);
int j = square(i);

The function add returns 15 to be assigned to i, the place where add was originally called. After that the value of i is used to call square. Note that a lazy compiler can’t rearrange these lines of code because the second line depends on successful evaluation of the first. We can rewrite this code block using Continuation Passing Style or CPS, where the function add doesn’t return to the original caller but instead returns its result to square.

int j = add(5, 10, square);

In this case add gets another parameter – a function that add must call with its result upon completion. In this case square is a continuation of add. In both cases j will equal 225.

So if you redefine your functions to take an optional last parameter that points to where the function should return to, suddenly you have some serious flexibility regarding what happens at the end of functions. The brilliance behind this method is shown a little further in the article:

Once we convert a program to CPS it becomes clear that every instruction has some continuation, a function it will call with the result, which in a regular program would be a place it must return to. Let’s pick any instruction from above code, say add(5, 10). In a program written in CPS style it’s clear what add‘s continuation is – it’s a function that add calls once it’s done. But what is it in a non-CPS program? We could, of course, convert the program to CPS, but do we have to?

It turns out that we don’t. Look carefully at our CPS conversion. If you try to write a compiler for it and think about it long enough you’ll realize that the CPS version needs no stack! No function ever “returns” in the traditional sense, it just calls another function with the result instead. We don’t need to push function arguments on the stack with every call and then pop them back, we can simply store them in some block of memory and use a jump instruction instead. We’ll never need the original arguments – they’ll never be used again since no function ever returns!

So, programs written in CPS style have no stack but have an extra argument with a function to call. Programs not written in CPS style have no argument with a function to call, but have the stack instead. What does the stack contain? Simply the arguments, and a pointer to memory where the function should return. Do you see a light bulb? The stack simply contains continuation information! The pointer to the return instruction in the stack is essentially the same thing as the function to call in CPS programs! If you wanted to find out what continuation for add(5, 10) is, you’d simply have to examine the stack at the point of its execution!

Continuations are particularly well-suited to web programming because of the stateless nature of the web. A request for a page could come at anytime, and what happened before  that page request will not matter. However, this is not always desirable. With continuations, we can simulate state, even in a stateless environment. For more on that subject, start with this article. An interesting alternative to MVC.
This is a whole new paradigm from the one I was taught in school and I think rightfully so. It is a tough concept to wrap your mind around at first. Now that I’ve had the “ah ha” moment, I look forward to implementing these ideas in my future work.

Tooltips

Sunday, June 25th, 2006

Ever find a link or button on a website that looked interesting but was disabled? Did you feel frustrated when you didn’t know why that certain something was disabled? This is a prime example of where a little hint as to why something is the way it is would be oh so useful. These hints are commonly referred to as tooltips, and a nice little javascript library named Tooltip.js handles this nifty little feature. Looking at their demos, it seems like a nice tool in the toolbelt. We’ll see how nicely it plays with Caseman.

My Public Email Key

Thursday, June 15th, 2006

For those that wish to send me email, I have set up my work email (james@serafinistudios.com) with a key pair. Download my public key here. Use this key with your favorite email client (I like Thunderbird) and send me email that no one else can read. Well, most any one…