Wednesday, February 9, 2011

Web server in CoffeeScript and Node

So, here's a first stab at a web server using CoffeeScript and Node:

# Imports.
{createServer} = require 'http'
{parse: parseUrl} = require 'url'
{readFile} = require 'fs'
{resolve} = require 'path'

# Setup.
base = "#{resolve('.')}/"
log = console.log

# Server definition.
server = createServer (request, response) ->
reqPath = parseUrl(request.url).pathname
filePath = ".#{reqPath}"
status = 500
respond = (content) ->
response.writeHead status,
'Content-Length': content.length
'Content-Type': 'text/html'
response.end content
log "#{request.method} #{reqPath} #{status}"
# Verify the path is local to the base!!!
# TODO Better startsWith
if resolve(filePath).indexOf base
# TODO status = forbidden?
respond("")
return
# Handle the request.
if request.method is 'GET'
readFile filePath, (err, data) ->
if err
if err.errno is 2
status = 404
data = ""
else
status = 200
respond(data)
else
respond("")

# Start the server.
port = 8080
server.listen port, ->
log "Running on #{port} from #{base} ..."

Not so bad. Probably could be cleaned up. I get tangled in CoffeeScript sometimes, but usually it's straightforward.

I don't drink coffee, but this is nice little scripting all right.

Wednesday, February 2, 2011

Python import angst

I've said it before, and I'll say it again, Python importing stinks. Either you corrupt your module namespace, or you put imports in all your individual functions. Both are horrible options.

Monday, November 29, 2010

New algorithms are painful in any language.

I think this set of examples from langref.org helps to show the pain of unexpected algorithms. It's a simple case, but note that none of the examples is remarkably clearer or shorter than most of the others. Library and language niceties often help for common use cases (hashtables, sorting, matrix multiplication, ...). With time, the set of what's common grows. Most programmers don't need to write new algorithms. But if you do, you can't always expect your language or library to save you.

Monday, November 8, 2010

Trying to exit Java

Just before the Oracle vs. Google lawsuit, I started a Java project for my PhD research. It was my first Java project in a while. I do more C++, MATLAB, and Python these days. I chose Java because I know it well and it simplifies cross-platform for me. I considered JavaScript, but this project included (2D) physics. I wasn't convinced by the JavaScript physics engines out there compared to the state of JBox2D.

Well, I'm one of the folks that has been convinced that Java isn't really open source nor independently implementable. If you get in the way of Oracle revenue, they will sue you. That's not open. Sorry.

So, where do I go?

I have a language idea of my own with primary output targets being JS (for client) and C++ (for speedy batch programming or native apps), but I don't really have the time to implement that. I could use haXe. It's the most credible JS/C++ targeting language out there, in my experience, but I'm not convinced I want to go that route either.

My current plan is to improve my C++ skills. Really. Anti-C++ rhetoric is strong in the Java community, but I'm being convinced that it can be used for good instead of evil. I also plan to stay in touch with JS land. And maybe someday I'll make or use a language that targets both (such as haXe).

I just wished I'd used C++ for that 2D physics project.

Sunday, November 7, 2010

Mormon Articles of Faith: Number 1

I've recently felt like sharing some of my beliefs about more important things than tech. I belong to The Church of Jesus Christ of Latter-day Saints. In other words, I'm a Mormon. We get that nickname because of the Book of Mormon, which we believe is a record of ancient Christians living in the Americas. That, among other things, makes us different from many other folks.

We are also called LDS (Latter-day Saints, from the name of the church).

To share our beliefs, I feel like working from our Articles of Faith. (See here for what "articles of faith" are in general and here for the full list of the LDS articles.)

I'll start with our first article:

1. We believe in God, the Eternal Father, and in His Son, Jesus Christ, and in the Holy Ghost.

Here's a great discussion of this subject from Gordon B. Hinckley, who under Christ's direction was the leader of the church at the time of his passing in 2008. He had authority to speak officially for the church, and I don't.

Still, I may continue this thread and speak from my own understanding. If I do that, I may emphasize that it's what I personally believe, not what we believe. Again, I don't have authority to speak for the church, and I definitely don't know everything. But I still try to stay in harmony with the teachings of the church and with the will of God as I understand it. I do my best (sometimes). Note that even President Hinckley (the term we use for the leader of the church) used the phrase "I believe" in his discussion. Despite his authority, I think he also liked to emphasize a personal perspective sometimes. I once listened to President Hinckley speak at a soccer stadium in Guatemala City. (Here's a reference from him to that occasion in Guate.) He said (to the extent I remember his wording), "If you remember one thing from today, remember you heard Gordon Hinckley say that he knows God lives." I actually do remember very little else from his message that day.

Anyway, to summarize my take-aways for the first article of faith: I believe in a personal God, not just universal forces. Even more specifically, there are three distinct persons to which I refer as God. All three can be called "one God" because of their great unity. While I seek to follow the promptings of the Holy Spirit in my life, I understand Him less as an individual. That's just where I'm at. The Father and the Son are clearer to me, even if I haven't seen them myself during my time here on earth. The Son, Jesus Christ, came to earth (as discussed in the New Testament in the Holy Bible) as a manifestation of God on earth. He also came to be like us. He knows first hand what we go through. I find that comforting. Further, knowing the Son helps us to know the Father.

If I continue this subject, I'll have more to say about Jesus. Mormons really are Christians in the sense that Jesus is at the center of our belief and worship.

Finally, again, in case anyone skims too much (as I do): Everything I say is my personal perspective. I don't speak officially for the church by any means.

Friday, October 29, 2010

Mac OS X Lion goes full screen

As a former Amiga user, I've long been a fan of full screen apps. This is also an area where Windows and Linux have outshined Mac with its not-really-even-a-maximize-button user interface. So this new plan full screen apps plan for Lion is really interesting to me.

Sadly, I fear the Mac App Store is just the first step toward completely eliminating traditional application distribution on the Mac. We'll see where they are in 10 years.

On a side note, I do still need to pardon Apple since they removed the draconian anti-Flash clause for iPhone app development. Can't ignore them, though. They'll likely be back at things along those lines (or other troubles of similar magnitude) before too long.

Wednesday, September 15, 2010

Wishing for TextPad bookmarks in Eclipse ...

There's one feature of TextPad that's just wonderfully convenient and helpful. It's called bookmarked lines.

You can mark lines by search (meaning by regexes), you can cut or copy marked lines, go forward/backward to next marked line, see them visually of course, and do other similar things.

What's the point? It means you don't need to drop down to a shell for things like grep or such like. They're really nice, actually. I used them just constantly when I was regularly on TextPad. I can't say enough nice things about them, really.

Please, please, please, please, please could someone put them in Eclipse? (Sorry for being a beggar rather than a helper.)

If Eclipse weren't already too complicated, I'd recommend an easy swap technique between different bookmark categories. Sometimes I would want to remember certain markings when temporarily moving to others. One option is to open the same contents multiple times (such as by copy and paste to a new document), but that was a pain really. But I'm afraid that anyone in Eclipse land would make a horror out of trying to manage multiple marker categories. So just one would be great for now.

And as another point, current Eclipse bookmarks are _nowhere_ near as good as TextPad's, despite a superficial similarity. But they could perhaps be improved to provide real service.

Thanks in advance!