Tuesday, January 29, 2008

Programming Languages are Languages too

One of the reasons that people keep inventing new programming languages is that humans are good at using language and computers are not. So was we improve computers and add more complexity, programmers endeavor to make using these new features simpler and less painful. As a result, computer languages are moving in a general direction towards more natural human language. There will likely always be differences, but programming languages and human languages are strikingly similar if you understand some of the widely used syntax.

Here's an example. When you see something like this
z = x * y;
print(z);
it means that you want the computer to "store the value of x times y in the varibale z, then display z on the screen." As you can see, some programming syntax is borrowed from math. This example includes arithmetic and a function. Functions can also be though of as verbs, with variables as the nouns. In object oriented programming, variables can be nouns which are capable of performing actions. If you had a digital carrier pigeon, and you wanted to tell it to carry a letter to your grandmother's house, you might say something like:
myPidgeon.payload = myLetter;
myPideon.flyTo(grandma.house);
In human language, there are always multiple ways to say the same thing, and the same applies in programming. The programmer might just as easily design the program to give grandma the letter like this:
myLetter.setRecipient(grandma);
myPideon.deliver(myLetter);
Now for some fun. What do the following code snippets mean?
  1. if (jack.getWorkPercent() == 100.0 &&
    jack.getPlayPercent() == 0.0) {
    jack.dullBoyFlag = true;
    }
  2. Pie aPie = new Pie();
    Song aSong = new Song(sixpence);
    aSong.sing();
    fill(pocket, rye);
    aPie.add(new Blackbird()[24]);
  3. Mouse mice[3];
    for (i in range(3)) {
    mice[i] = new BlindMouse();
    }
    observe(run(mice));
  4. party = new Party(jack, jill);
    party.setTarget(water);
    party.equip(pail);
    party.ascend(hill);

Wednesday, January 23, 2008

A spoiled programmer

I've been writing quite a bit of Python code recently and I've become a bit spoiled. It's easy in Python to define new classes on the fly, create new functions, pass them here and there, and return arbitrary collections from a method. C will always have a special place in my heart (I think everyone's first language does), but I often think of ways I could make it a bit easier to do certain things like have functions that return functions or have a function return multiple values.

To explain by way of example, it would be fun to do something like this:
/* A function that returns multiple values */
int, char, int myFunction(int a, int b, int c, char d) {...}

...
/* Invoke the function and store the results */
int x, y;
char c;
{x, y, c} = myFunction(5, 6, 7, 'Z');

The above is a fairly Pythonic way of doing things, and it seems like it should be possible in C. The first way I thought of is using structs. I like to think of a struct as the precursor to a class. It allows the arbitrary grouping of variables into a single collection where they can be referred to by name. (In a couple of earlier posts, I showed how you could use structs to simulate classes in C.)

If I define a struct for each one of my multi-variable-returning functions, I can create functions which effectively return multiple values instead of just one. Yes, technically I am returning one value, the struct, but you know what I meant :-) Namely, if you look at the program's stack, there is probably no perceptible difference between returning a struct and returning multiple variables.
/* Create a 2 member struct to hold the return value */
struct myFuncReturn {
int first;
char second;
};

/* A function that returns an int and a char */
struct myFuncReturn myFunc(int a, int b, int c, char d) {
struct myFuncReturn to_return;
to_return.first = (a+b)*c;
to_return.second = d;
return to_return;
}

int main() {
struct myFuncReturn pattern;
pattern = myFunc(2, 3, 4, 'Z');
printf("Pattern: %i, %c\n", pattern.first, pattern.second);
}
This works ok, but I would like to avoid having to create a new struct for each one of my functions. It might be easier if I didn't have to worry about types at all, so the natural choice is to have the function return a type-less void pointer (void*). The calling code would then be responsible for interpreting the function's return struct correctly. If I want to return a new anonymous struct from a function, it might look something like this:
void* myFunc(int a, int b, int c, char d);
If I use the above, I'll need to allocate memory for the struct and return it's address. This is a bit of a bother as well, because now I need to worry about cleaning up that memory later. Instead of having the function allocate a new structure to return, why not pass in a structure and have the function modify it? The code I would need to write would be more aesthetically pleasing (in my opinion) for both the function definition and the calling code which invokes it, and it might even be more efficient.

If I pass in a pointer to the result struct as the first parameter to the function, my program could look like this.
/* Function definition, the out parameter is the return value */
void myFunc(void* out, int a, int b, int c, char d) {
((struct{int first; char second;}*)out)->first = (a+b)*c;
((struct{int first; char second;}*)out)->second = d;
}

int main() {
struct{int first; char second;} pattern;
myFunc(&pattern, 2, 3, 4, 'Z');
printf("Pattern: %i, %c\n", pattern.first, pattern.second);
}
Look ma, no type declarations! Now you might say that writing out the entire struct definition each time is a bit unpleasant, but you could always define a struct and use it instead. I wanted to show that you don't really need to declare a type for each function, which could create a bit of a mess if you start using multi-return functions everywhere. With the above pattern, you could also start to play some interesting games by having functions that actually return different structs in different situations (provided the out pointer's reserved space is large enough for the data you want to send back). If I've lost you by now, I do apologize.

For added effect, note that the anonymous structs don't need to match, you just need to make sure that the shape of the structure is the same so that you don't overwrite data. I could have written myFunc like this:
void myFunc(void* out, int a, int b, int c, char d) {
((struct{int first;}*)out)->first = (a+b)*c;
((struct{int x; char second;}*)out)->second = d;
}
Or if you want to go even further, like this:
void myFunc(void* out, int a, int b, int c, char d) {
*((int*)out) = (a+b)*c;
((struct{int x; char second;}*)out)->second = d;
}
Ah, the joys of programming. It's little games like this that make programming lots of fun. It's like working on a big wide open puzzle that you get to build yourself. No wonder I'm spoiled.

Monday, January 14, 2008

Twitter

If you've never tried Twitter, it might just be worth your while. I've been using it for a few months now and I'm quite pleased. As much as I try to make frequent blog updates, writing an entire entry sometimes feel like a large task. In contrast, twitter imposes a strict 140 character limit. This limit is both challenging and freeing in some ways: How much can you pack into a sentence or two? In addition to providing a place to share small updates, Twitter introduces a social aspect as well. By using @username notation, you can let everyone know your post was directed to a specific person so that they have a window into the conversation.

If you are familiar with Facebook, using Twitter is a bit like having a website made of just your profile status and your wall. These are my two favorite features of Facebook, so perhaps this is why I enjoy Twitter. The main difference is that you never post on someone else's wall, your posts show up on their wall if they choose to "follow" you (subscribe to your updates). I've never really used my phone to twitter (yes twitter is also a verb) or receive updates, but supporting posts from multiple platforms is a big selling point for some users.

My Twitter page is here and I also include a feed viewer gadget here on my blog to display my last few Twitter posts. If you have an account post it below!

Sunday, January 06, 2008

Trying out the XO Laptop Operating System

In a recent post I mentioned that I had ordered an XO Laptop from the One Laptop Per Child program. It is still on order but being the slightly impatient person that I am I decided to see if I could test drive the software on the laptop before it arrives. It is based on Linux after all which usually means the software is freely available somewhere on the Internet.

Taking the XO for a spin turned out to be pretty easy and I have been able to add all of the applications I've wanted so far. I mentioned in my last post about the XO that I wanted to use it for programming and web browsing so I had a few requirements in mind. The laptop comes preinstalled with a custom web browser and Python but I often like to program in C and C++ so I wanted to install gcc. The web browser has a few wrinkles too, in the development build I tested I wasn't able to get some flash plugins to load (could be the version of flash or the fact that the XO version I downloaded wasn't a production release). The browser is tabless and it is a bit difficult (though not impossible) to figure out where downloaded files are stored. In short it wasn't quite was I've grown accustomed to, so I thought I would try installing Firefox. He's my step by step instructions for trying out the XO Laptop virtually and customizing it.

I began with the XO's wiki and found out that there are instructions for emulating the XO and VMWare virtual machine images for recent builds of the system. VMWare is a program which creates a simulated computer that runs within your current operating system. For the past few years I've tested all of the Linux distributions I've considered on VMWare Server (free to download and use at home). Once I started VMWare, I opened the ship.2-OLPC-655.vmx file and started it up.

After the initial configuration, I wanted to add gcc, a collection of open source compilers. I thought it was going to involve downloading several RPM files, but I found out about a tool called yum which manages RPM packages. This is similar to Ubuntu's apt-get. As root I was able to run
yum install gcc
and it downloaded and installed all prerequisites.

Next I wanted to install Firefox, and to download it I thought I would try Lynx (a text based web browser). Installing Lynx was just as easy, as root I ran:
yum install lynx
I ran lynx www.google.com and searched for firefox and downloaded the tar.gz Linux version. After downloading I unpacked the archive using
tar zxvf firefox-2.0.0.11.tar.gz
I tried running firefox, but got an error about a missing shared object library. Yum was able to find this as well. One final time as root, I ran
yum install libstdc++.so.5
After installing the C++ library, firefox ran just fine. The menus and graphics in Firefox matched the XO's theme, which I thought was pretty nifty. I was also able to install Flash. Well there you have it, why not take it for a test drive yourself.

Tuesday, January 01, 2008

New Year's Resolutions

Well folks, it's that time of year again. Time to set goals with the best of intentions and aim high to better myself. I'm borrowing quite a few of my new years resolutions from last year, there were a couple that didn't go as well as I had hoped. Daily time for devotions, reading, and mediation tops the list. More frequently journaling and blogging also made the cut.

I noticed a handy goal tracking website on Lifehacker which I'm using at the moment. However, I often find that the online nature of tools used to track things like to-do lists and finances can create a small inconvenience which leads to an eventual breakdown. A plain old pen and paper still holds an important place.

In any case, here's to a new year and to best-laid plans.

Friday, December 21, 2007

An open source JavaScript library: q12

I've written some JavaScript utility functions as part of my ongoing wiki/note taking application. It is growing into a full fledged Ajax application with a web server and now a minimal Ajax library which I've decided to release as a separate open source project.

From the project's front page:

I found myself needing a few common utilities as I was writing an Ajax application. Rather than use a heavyweight or verbose library, I wanted something compact that minimized the amount of typing I needed to do. This is where the name q12 comes from, just three little keys up there in the upper left corner of the keyboard.

This library provides functions for the following:

  • Basic DOM manipulation
  • Asynchronous HTTP requests with callbacks
  • Class methods and inheritance
  • Base64 encoding and other forms of data escaping
  • AES encryption
Writing your own Ajax library is also a great way to learn JavaScript (IMHO). I'll be making little tweaks as I work further on my project, it's getting quite close. I think I've probably said that before but rewriting from scratch tends to set one back a bit. Third iteration's the charm?

Wednesday, December 05, 2007

One Laptop Per Child, Give One Get One

If you are looking for a cheap, power efficient, portable laptop for wireless web browsing and programming, like I was, consider the XO Laptop from the One Laptop Per Child association. I ordered one to keep and one as a donation through the Give One Get One program. It looks like it will be a lot of fun, I'm especially interested in the wireless mesh networking and social aspects to using the laptop. Some of the the music software looks like fun as well.

I'm ordering one for mostly selfish reasons, but this is probably my first computer purchase which will benefit someone else. Who knows, perhaps a child in a developing nation will discover a new world of seemingly limitless possibilities through programming just like I did as a child. This is my personal take on the vision behind this unique program. The opportunity to order one is slipping away fast, the offer to buy one and give one away ends December 31st. Let me know if you've ordered one, perhaps we can organize a laptop party (Arne I'm looking at you ;-)

Thursday, November 29, 2007

Something like Lisp

Ever since compilers class in college, I've toyed with the idea of creating a new programming language. Every year or so a spend a few days thinking about one and get off to a short start. The problem is I often try to bite off more than I can chew and start dealing with the most difficult parts first. So I recently decided to get a minimal language up and running. Something with simple syntax, no concern for efficiency, and an easy to build base.

Lisp has some of the simplest syntax of any of the languages I've worked with (perhaps with the exception of BF). I was looking for something simple which I could quickly implement, so I used it as a model and created an interpreter in Python. The whole project took just a couple of hours, but I was able to write an interpreter that would run the following program:
set[x 5]
set[x add[1 2 3 4 5]]
set[y add[1 2]]
println[get[x]]
println[get[y]]

As you might have guessed, this program displays the following in the terminal:
15
3
I implemented a simple parser using Python's regular expression module re (The group method can come in quite handy), and I defined each of the above functions in a dictionary which maps function calls in my custom language to functions in a Python script which I import. You might have noticed that each of the methods is designed to take a variable number of arguments (thought only they may not all be used), and all function calls are made recursively, with nested functions being evaluated first. The syntax looks slightly like s-expressions but it's a bit more accessible for someone who has worked with C-like languages (except there's no need to reach for the shift key for those tricky parenthesis). I haven't added a mechanism for lambda expressions yet, but defining new functions might look something like this:
function[my_fun group[x y] group[multiply[x add[x y]]]]
The above would define a function named my_fun which takes two arguments, and multiplies the first by the sum of the two arguments.

I just made up this micro language, and haven't given this a great deal of thought. The idea was to create something quick and dirty to create a working interpreter, rather than the usual of intense planning without a working prototype. This language will probably never see any further development. It was a mental exercise, but I found it quite enjoyable. If you'd like to see the source code for the interpreter, please let me know (I get the feeling Matt might be somewhat interested).

Monday, November 12, 2007

Fluxbuntu 7.10

Long time readers may remember the saga of my old laptop (a Compaq Presario 1700T circa 2000). I had ditched openSUSE several months ago in favor of Fluxbuntu, a variant of Ubuntu which used the light weight Fluxbox windowing system in place of the Gnome desktop. My old computer has only 128 megabytes of RAM, so memory is at a very high premium. With the release of Gutsy Gibbon, Fluxbuntu picked up a few new features, so I upgraded and gave it a try. I have been extremely pleased. All the good stuff is there that I enjoyed before (installing new free software using Synaptic or Aptitude, Firefox, XMMS, etc.) but there were a couple of great new additions. For one, automounting of USB drives. I have a small pen drive that I carry around to hold many of my files: music, programming projects, etc. Mounting had always been a bit of a pain with my laptop's OS. Now I just plug it in, it mounts, and an icon for the drive appears on my desktop.

The discovery came at a perfect time. My in-laws decided they wanted to resurrect an old PC so they could browse the web side by side. All they really needed was Firefox, Open Office, and Picasa and their computer had the same amount of RAM as my old laptop (probably from about the same time frame). Fluxbuntu to the rescue ;-) Can you believe it, my in-laws are running Linux.

Thursday, November 08, 2007

IE, XMLHttpRequest, and caching

I recently noticed an oft cited irritation with using XMLHttpRequest in Internet Explorer. Turns out, the browser intercepts HTTP GET requests made from within JavaScript and gives your Ajax application a cached response instead of fresh data.

I was running some tests on scorpion_server, using the JavaScript client in the example that I've packaged with the server, and I noticed that I could change the data stored in a resource when using IE6, but I couldn't get the value I had just set. This means my client-server pair is pretty much useless in IE6 (probably 7 too). But I thought of a solution. IE is just performing a test for exact match when checking the cache, so if any part of the URL is different, IE will actually perform the query like I want it to. So I added a timestamp URL parameter to all GET requests made by the client if the browser is IE, and I configured the server to ignore all URL parameters on GET requests. I wasn't using them anyway, perhaps someday I will, but I don't foresee a need because all I want is a super simple remote data store. (Who knows, YAGNI might just be my new mantra.)

Note that IE was the only browser (of those I tested) that exhibited this annoying behavior. Mozilla based browsers (Firefox, SeaMonkey, Flock), Opera, and Safari all behaved correctly and don't require the timestamp URL parameter hack.

Sunday, November 04, 2007

JavaScript client for my Scorpion Server

I've added a JavaScript client to the simple web server project: scorpion server. The client is able to set the username and password and make authenticated GET and POST requests to retrieve and modify resources on the server. Now that this portion is complete, I'm ready to rebuild my Ajax Wiki application to run on top of this simple web server.

Saturday, October 27, 2007

Hey look, a simple web server

I think I'm done writing my web server. I have gotten it to do what I want, namely this. This server will:
  1. Run just about anywhere. I sometimes run it off of a USB pen drive.
  2. Send all traffic over an HTTPS connection.
  3. Handle user authentication and permissions. You can only read and write where you have permissions.
  4. Allow you to store data in a remote location. Just POST to a URL to store something at that location, GET to retrieve it.
There really wasn't much to it. I was able to write this quickly and there wasn't that much code. The main thing it is lacking (in my opinion) is speed. This could be fixed by making it multithreaded and adding some caching since right now it always reads from the disk. Without further ado, here's the code. I also wrote a Python client and a JavaScript client to go with it will be coming soon. If you found this to be useful, please let me know.

I've also been looking at CherryPy as a framework to create the same type of portable, simple, and secure web server. As usual, stay tuned for details.

Wednesday, October 24, 2007

Varieties in Code Design

One of the interesting things about writing software is the myriad of ways to express what it is that you would like the computer to do. When designing a class, function, or something else, you can make the syntax looks just about any way you like. The real interesting bit, is when multiple people need to work on a large project together. Everyone needs to be able to understand the code, so it's probably a good idea if you agree on code conventions beforehand. All of the below would do equally well, but which is most clear?

x = a + 5;
x = a.plus(5);
x = 5.plus(a); (You could do this in Ruby/)
x = plus(a, 5);
plus(a, 5, x);
a.plus(5, x);
set(x, plus(5, x)); (This looks a bit like Lisp.)

The list goes on and on.

Tune in next time for simple Python web-server fun.

Thursday, October 18, 2007

Design for the simple secure storage server

In my last post, I mentioned my motivation for writing this server and pointed to the foundation I'm building on. Now it's time for more detail.

This server may not be supper fast (single thread execution) and it may not be super secure (user data stored in plaintext on the server) but it will be super easy to set up.

Allow me to clarify the security point in the above summary. In the initial version of this server, all traffic will be sent over an SSL connection (HTTPS) and users will authenticate with the server using Basic Auth. In Basic Auth, the users password is sent to the server in plaintext. There are better authentication schemes out there, but for this version of the server, I'm going for quick and simple. Basic Auth is just barely acceptable for this project because the connection is secure, but the server will likely store these passwords in plaintext as well (for now) so server disk security may be the weak link. With that said, the idea for this server is to provide a simple and portable back-end for my AJAX applications.

Thursday, October 11, 2007

A simple HTTPS server

Recently, I've been working on an AJAX application in my spare time and there's something I could really use: a simple network data store.

A JavaScript application isn't very useful without some persistent data. However this usually requires running a web server. My original idea was to distribute the application as a file which is loaded from the local disk. At this point you may be saying, "Wait a minute JavaScript running in a browser can't access the local disk." But scripts can read and write to the local disk if they are loaded from disk instead of the Internet. See TiddlyWiki for a great example of a useful application that uses this design. The problem though, is what happens when you want to sync the data from the AJAX application across multiple computers. Well, once again, it looks like I need a web server after all.

So I set out to build a simple server. All it really needs to do is allow applications to store and retrieve data. To make sure that the data remains a secret, the traffic will be sent over an HTTPS connection. Access to certain directories and files on the server will be granted only to select users, so usernames and passwords are required too. Since I've been working with Python recently, I tried to see if it was possible to create a simple HTTPS server which could handle GET and POST requests and perform dynamic behavior. I found a great example on activestate.com which uses an open SSL .pem file. The instructions in the article made setting up this server a breeze. I've been working on a customized version of the above example, but it isn't quite ready. As usual, stay tuned :)

Wednesday, July 18, 2007

New Domain and A New Project

A quick update on the domain name, I've registered jeffscudder.com and set up an email account, so you may be getting some mail from a new address the near future. I'm still looking for an idea on a domain to register for hosting projects. I'd like to get a name where my friends and I can all hook our email monikers (I don't imagine anyone wants to be bob@jeffscudder.com).

I've been working on a new project. I'm a huge fan of tiddlywiki, I've been using it almost a year now for keeping my notes organized. As I've continued to use it, I've found three things I would like to change:
  1. Contents are saved in plaintext. I carry my tiddlywiki around on a pen drive and some of the notes contain personal information that I'd like to keep safe. Plus, I like playing with cryptography.
  2. Notes are saved to a local file. This is both a strength and a weakness, the problem is I sometimes use multiple computers and I want to easily move between them. I'd like to have the option of saving to a local file or a server.
  3. It is sometimes slow. There is a lot of eye candy and it is very customizable, but I just want something lighweight, small, and simple.
I've gotten pretty far on my own wiki web app. but it's not quite ready to share. Keep an eye out for a release sometime in the near future.

I leave you with a song that has been stuck in my head all of last week: Deathbed by Relient K.

Friday, June 01, 2007

Domain Name Ideas

I've been thinking about getting my own domain for some time now, and I've finally decided to make the leap. The only problem now is that pesky task of choosing. I have a few ideas, but I'd like to hear what you guys come up with. Think of it as a distributed brainstorming session, no idea is too strange. (I was inspired by a discussion on Yed's blog where we helped him choose custom license plates.) I'm keeping the post short and sweet, stay tuned for details.

Wednesday, May 02, 2007

China Trip - Part 5

I will close my retelling of our China trip with our days in Hangzhou and Shanghai. (The Wikipedia articles have great pictures of many of the sites I took pictures of, including the tea plantations and Shanghai's waterfront.) In Hangzhou, we began with a boat ride on the West Lake and took a walk through the gardens nearby. We also visited a tea house and sampled traditional green tea. The next day, we traveled to Shanghai. There, Vanessa and I saw the new developments and skyscrapers which have sprung up over the past few years. It is a city which is growing extremely rapidly, yet all of the land is still owned by the government. They have started a lease program, in which an entity can get a lease from the government to use the land for 70 years. When property changes hands, the lease isn't renewed, you only have the remaining time on the original lease. Some of the real estate prices were outrageous (even by California standards) if you consider that you may only have your property for 50 years before the government can take it back. I imagine this system will seem extremely odd to most Americans. While we were in the waterfront we saw the construction of what will be the tallest building in the world, when it is completed before the 2008 Olympics.

In closing, I offer some reflections on the trip. We saw amazing sights, experienced a different culture, and met new and interesting people. Several thoughts occurred to me as I think over my journey, I noticed that their culture had common themes and ideas. Similar stories, values, and ideals, it seems that in some ways, the core of most societies is very much alike. I was also struck by the pace of growth, it seems that China is an economic force to be reckoned with, and they appear to be rapidly expanding. Perhaps I saw only the "good parts"; my view may be inaccurate. Perhaps my view was colored by the pessimism which people say is characteristic of my generation when it comes to our nation's future. Perhaps I was just tired of the Chinese food ;)

Tuesday, April 17, 2007

China Trip - Part 4

We begin this installment of our ongoing series on our China trip in my favorite location. The Lingering Garden is a masterpiece of architecture and oriental style gardens complete with bonsai trees (However, it was devoid of bonsai kittens). I'm not surprised that Chinese poets came to this garden to find inspiration in beautiful Suzhou. I could have easily spent a few more hours walking around the pond and I can't believe that the entire complex of gardens and buildings once belonged to one family.

The third picture in this post is from the leaning Huqiu Tower in Suzhou. The brick work on the structure was amazing and it is older than the Leaning Tower of Pisa. Vanessa took this one from a plaza to the side of the tower. Next time, I'll write about the very end of our trip in Hangzhou and Shanghai.

Tuesday, March 20, 2007

China Trip - Part 3

There were only a few moments where we caught a glimpse of what everyday life is like for the people of China and a couple of them came on this day of traveling. We continued our journey with a rickshaw ride through some of the older areas of Beijing and then we took a trip to the Temple of Heaven. As we walked the long corridor to the temple, we saw a large group of people playing cards, dancing, and making music. It was a glimpse of life that I would like to have taken part in, but I felt like an outsider. I'm still not sure what card games they were playing. The Temple of Heaven was a breathtaking structure and from the top of the hill you could see the entire city.