Why I changed my mind about Node.js

Intro

I first heard of Node.js about 4 years ago. I thought it was a ridiculous idea: using JavaScript as your server-side language! JavaScript always felt to me like an inferior language. Why would you use it unless you absolutely had to!? Now of course, you have to do JavaScript if you are writing code which runs in the browser, but on the server, you can use any language you choose. So why choose JavaScript?

I did choose to use JavaScript/Node.js on my current project. I have now been using it for the last several months, and I am quite impressed! Let me tell you what made me change my mind about Node.js.

First, Node.js has been adopted ...

Sessions in Node.js and Express.js

In the process of trying to explain to a friend how sessions work, I decided to write a barebones example to demonstrate how to do sessions in Node.js with Express.js.

Here's a live demo: http://expressjs-session-example.herokuapp.com/

And Here's what it looks like:

And here is the code:

var express = require('express');
var bodyParser = require('body-parser');

///////////////////////////////////////////////////////////////////////////////////////// MIDDLEWARE

var app = express();

// Needed to handle JSON posts
app.use(bodyParser.json());

// Cookie parsing needed for sessions

// Session framework
app.use(session({secret: 'notsosecretkey123'}));

// Consider all URLs under /public/ as static files, and return them raw.
app.use(express.static(__dirname + '/public'));

/////////////////////////////////////////////////////////////////////////////////////////// HANDLERS

function ...

On Abstract Art

Too much detail in art is profanity - a vulgar attempt to sum up that which is beyond finite expression.

Example:

How to SSH Tunnel

When to use an SSH Tunnel?

An SSH Tunnel is perfect for the following scenarios:

• You want to get to a website that a corporate network is blocking.
• You want to encrypt the network traffic for a non-encrypted protocol.

What is an SSH Tunnel?

An SSH Tunnel is where you tell your local SSH Client to forward your local computer's traffic to a specified SSH Server (over the encrypted SSH protocol). The SSH Server will then act as a proxy for all requests you make on your local computer.

How to set up an SSH Tunnel

There are 2 steps to set up an SSH Tunnel, and they both happen on your local computer (no configuration is needed on the SSH server which will be acting as ...

Art is a laser

The best art is more like a laser beam than a flood light - it doesn't hit everyone, but those it hits, it burns.

Fuel efficiency difference cost

I recently was car shopping, and I had the question about gas mileage: "How much does, say, the difference between 25 mpg and 30 mpg cost?"

To answer this question, I did a bit of analysis using IPython Notebook...

Goal

• To find how much different fuel efficiencies cost in terms of dollars per year.
• I want to estimate this value for the next 10 or so years.

Vim run current file

I just added a nice command to my .vimrc file to allow me to run the current file I am working on by typing :R:

command R !./%


On the VIM note, here is a link to my current vim config (.vimrc file and .vim directory) on github: Vim config.

Barcamp Milwaukee 2012

Barcamp Milwaukee 2012 is being held October 6th-7th. I had a good time last year, so I can definitely recommend it :)

Secret to not having road rage

I have found the secret to not getting road rage: audiobooks. Try it. Right now, I'm listening to Dune by Frank Herbert.

Facial Detection with OpenCV and Python

I was able to get some basic facial detection working in OpenCV with Python. Here's what it looks like:

And here is the 34-line python script to draw boxes around all detected faces in live video:

import cv

CAMERA_INDEX = 0

def detect_faces(image):
faces = []
detected = cv.HaarDetectObjects(image, cascade, storage, 1.2, 2, cv.CV_HAAR_DO_CANNY_PRUNING, (100,100))
if detected:
for (x,y,w,h),n in detected:
faces.append((x,y,w,h))
return faces

if __name__ == "__main__":
cv.NamedWindow("Video", cv.CV_WINDOW_AUTOSIZE)

capture = cv.CaptureFromCAM(CAMERA_INDEX)
storage = cv.CreateMemStorage()
faces = []

i = 0
while True:
image = cv.QueryFrame(capture)

# Only run the Detection algorithm every 5 frames to improve performance
if i%5==0:
faces ...

Intro to OpenCV with Python

I have started my journey into the world of OpenCV using Python on my Mac. These are my first steps with it.

Installing OpenCV for Python on the Mac

# Install numpy as a prerequisite
sudo port install numpy

# Install OpenCV with the Python API
sudo port install opencv +python27


(The port command is part of Macports)

Capturing an images from a webcam

  1 2 3 4 5 6 7 8 9 10 #!/usr/bin/env python2.7 import cv cv.NamedWindow("w1", cv.CV_WINDOW_AUTOSIZE) camera_index = 0 capture = cv.CaptureFromCAM(camera_index) frame = cv.QueryFrame(capture) cv.SaveImage("pic.jpg", frame) 

(I tested this with the iSight camera on my Macbook Pro)

Displaying live video

  1 2 3 4 5 6 7 8 9 10 11 #!/usr/bin/env python2 ...

NSArray+FP

I realized that I utilize the [NSArray head] and [NSArray tail] functions in various code snippets, but haven.t shared the actual code for that category. So here is my NSArray+FPcode.

NSArray+FP.h

#import <Foundation/Foundation.h>
typedef id(^MapBlock)(id);
@interface NSArray (FP)
- (NSArray *)map:(MapBlock)block;
- (NSArray *)tail;
@end


NSArray+FP.m

#import "NSArray+FP.h"
@implementation NSArray (FP)

- (NSArray *)map:(MapBlock)block {
NSMutableArray *resultArray = [[NSMutableArray alloc] init];
for (id object in self) {
}
return resultArray;
}

{
return [self objectAtIndex:0];
}

- (NSArray *)tail
{
NSRange tailRange;
tailRange.location = 1;
tailRange.length = [self count] - 1;
return [self subarrayWithRange:tailRange];
}

@end


Eye movement on web pages

• I've always preferred bulleted lists (and the like) over flat paragraph formats
• I believe they can be visually parsed much faster than paragraphs
• Now I have evidence: F-shaped reading pattern study :)

Here is an image which shows the eye movement of users on web pages (where red means more time was spent looking there):

Functional programming to deal with asynchronicity in Objective-C

In the last few weeks, I've used the [ALAssetsLibrary loadImages:callback:] method to load a list of images. For what I was doing, I wanted to wait till all the images were loaded before proceeding. I found that I could solve this problem very elegantly using a recursive solution:

- (void)loadImages:(NSArray *)imageUrls loadedImages:(NSArray *)loadedImages
callback:(void(^)(NSArray *))callback
{
if (imageUrls == nil || [imageUrls count] == 0) {
}
else {
__unsafe_unretained id unretained_self = self;
ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];
ALAssetRepresentation *assetRepresentation = asset.defaultRepresentation;

UIImage *image = [UIImage imageWithCGImage:assetRepresentation.fullResolutionImage
scale:assetRepresentation.scale
orientation:(UIImageOrientation)assetRepresentation.orientation];

} failureBlock:^(NSError *error) {
}];
}
}


In ...

First look at Pylab/Matplotlib

Since I've been getting into Machine Learning/Artificial Intelligence recently, I've been looking at various computing environments recently. Some of the contenders are:

• MATLAB - The traditional software stack for doing machine learning and statistical analysis
• GNU Octave - An open-source MATLAB clone.
• R - An open source clone of a statistical computing environment called S.
• Julia - A language for doing statistical analysis. The goals are to compete with Matlab and R.
• Matplotlib/Pylab/SciPy/NumPy - see below

Of these, I've tried Octave and Matplotlib. Matplotlib/Pylab is the software stack consisting of:

• IPython - an interactive REPL for Python with things like tab completion
• Matplotlib - a graphical plotting library
• NumPy - a matrix library
• SciPy - a collection of scientific and mathematical algorithms

I've only played with Matplotlib/Pylab ...

How to reference self in a block in Objective-C

When using ARC (automatic reference counting) in Objective-C, you need to be careful to avoid causing retain cycles. One place where a retain cycle can occur is where self is referenced in a block. To avoid a retain cycle, you can use the __unsafe_unretained modifier as such:

__unsafe_unretained id unretained_self = self;
reachability.reachableBlock = ^(Reachability *r) {
dispatch_async(dispatch_get_main_queue(), ^{
SurveyFormController *myself = unretained_self;
self.mapView.reachable = YES;
[myself configureView];
});
};


Probability and Statistics book for Python programmers

This seems like a nice, concise book on probability and statistics for Python programmers: Think Stats.

Premature generalization is the new premature optimization

A co-worker was just showing me a situation where another developer had written a validation framework which was only used on 2 pages. I've seen this pattern again and again, and I've put a phrase to the thought:

Premature generalization is the new premature optimization.


And it, too, is evil.

I'm taking the Stanford Machine Learning class. The first algorithm we covered is Linear Regression using Gradient Descent. I implemented this algorithm is Python. Here's what it looks like:

import subprocess

def create_hypothesis(slope, y0):
return lambda x: slope*x + y0

def linear_regression(data_points, learning_rate=0.01, variance=0.001):
""" Takes a set of data points in the form: [(1,1), (2,2), ...] and outputs (slope, y0). """

slope_guess = 1.
y0_guess = 1.

last_slope = 1000.
last_y0 = 1000.
num_points = len(data_points)

while (abs(slope_guess-last_slope) > variance or abs(y0_guess - last_y0) > variance):
last_slope = slope_guess
last_y0 = y0_guess

hypothesis = create_hypothesis(slope_guess, y0_guess)

y0_guess = y0_guess - learning_rate * (1./num_points) * sum([hypothesis(point[0]) - point[1] for point in data_points])
slope_guess = slope_guess - learning_rate * (1./num_points) * sum([ (hypothesis(point[0]) - point[1]) * point[0] for point ...

Embedding an XSLT in an XML document

Today, I had this XML document which I was wanting to make viewable in a browser. My plan was to run the XML document through an XSLT engine to transform it into an HTML document (which could then be opened in a browser).

Instead, I found that I could just embed the XSLT directly in the XML document. Then, when this file is opened in a browser, the XSLT executes and spits out HTML, which is then rendered into the browser window. This is great because I now have the original XML file (which we wanted to keep further analysis purposes) which can also be rendered nicely in a browser.

Here's an example:

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xml" href="#stylesheet"?>
<!DOCTYPE catelog ...

Resampling Wheel Algorithm

I've been taking the Udacity CS373 (Robotic Car) class, and this week the topic was Particle filters. Particle filters are basically a localization algorithm that accounts for error in measurements and sensors.

Anyway, part of the Particle Filter algorithm requires the generation of a new set of these things called "particles" based on the particles' weights. So to accomplish this task, the Resample Wheel algorithm was presented in class. It is a particularly elegant method of generating a new set of particles by randomly drawing from an old set of particles (with replacement). The particle weight determines the likelihood of it being picked.

Here is the algorithm (with print statements - I used these to help me understand how the algorithm works):

import random

def generate_new_particles(old_particles, weights ...

JSONP for cross-origin AJAX

I was recently wanting to enable a webpage to utilize some server-side functionality which was on a different server than the one serving the original page. Traditionally, this has been problematic due to browsers' Same origin policy. JSONP is a solution to this problem. Here is how to do it with jQuery...

This webpage can be saved locally to any machine, opened in a browser, and can then load data from my web server:

<html><head>
<script type="text/javascript">
$(document).ready(function(){$("button").click(function(){
$.ajax({ url:"http://test.calebmadrigal.com/javascript/jsonp_test.json", dataType:'jsonp', jsonpCallback: "jsonp_load", success:function(result){$("#outputdiv").html(result);
}
});
});
});
</script>
<button ...

Tail call optimization in Python

Since I've been getting into functional programming more recently, the fact that Python doesn't do tail-call optimization has been bothering me. So I did a bit of searching, and found this gem: Tail Call Optimization Decorator.

Here is a snippet from it:

import sys

class TailRecurseException:
def __init__(self, args, kwargs):
self.args = args
self.kwargs = kwargs

def tail_call_optimized(g):
"""
This function decorates a function with tail call
optimization. It does this by throwing an exception
if it is it's own grandparent, and catching such
exceptions to fake the tail call optimization.

This function fails if the decorated
function recurses in a non-tail context.
"""
def func(*args, **kwargs):
f = sys._getframe()
if f.f_back and f.f_back.f_back \
and f.f_back.f_back.f_code == f.f_code ...

Standard Deviation in Python

I just wanted to go through the process of calculating standard deviation today, and this is how I did it in Python. Python makes such a nice calculator :)

>>> s = [2,4,4,4,5,5,7,9]
>>> def average(s): return sum(s) * 1.0 / len(s)
...
>>> avg = average(s)
>>> avg
5.0
>>> variance = map(lambda x: (x - avg)**2, s)
>>> variance
[9.0, 1.0, 1.0, 1.0, 0.0, 0.0, 4.0, 16.0]
>>> average(variance)
4.0
>>> import math
>>> standard_deviation = math.sqrt(average(variance))
>>> standard_deviation
2.0
>>>


Django on Hostmonster

I just finished going through the process of installing and configuring Django (with FastCGI) on Hostmonster's Shared hosting. It was more painful than I expected, so I decided write a post about how I got it working...

First, I wanted to create a subdomain which would host my django stuff. In order to do this, I created a subdomain using the cPanel (the item is called "Subdomains"). This created the directory and a basic .htaccess file (along with a few error pages).

I then went through the basic processes described on these pages:

When I finished these guides, I kept getting a 500 error. I eventually found that I can access the error log ...

String to Base64 String in Objective-C

Today, I needed to convert an ASCII-encoded NSString to a base64 NSString. I found a number of methods that convert from an NSString to a Base64 NSData, or from NSData to a Base64 string, but couldn't find one that did exactly what I needed. So here is a method that simply converts an NSString to a Base64 NSString:

+ (NSString *)base64String:(NSString *)str
{
NSData *theData = [str dataUsingEncoding: NSASCIIStringEncoding];
const uint8_t* input = (const uint8_t*)[theData bytes];
NSInteger length = [theData length];

static char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

NSMutableData* data = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
uint8_t* output = (uint8_t*)data.mutableBytes;

NSInteger i;
for (i=0; i < length; i += 3) {
NSInteger value = 0;
NSInteger j;
for (j = i; j < (i + 3); j++) {
value <<= 8;

if (j < length) {
value |= (0xFF & input[j]);
}
}

NSInteger theIndex ...

Function application and function composition in Haskell

In the process of learning haskell, I've been trying to become comfortable with the function applicator ($) and function composition (.). Here is some code to show how they relate (and how they relate to using parenthesis to enforce order of operations): let dict = [(1,'a'),(2,'b'),(3,'c')] -- The following 3 lines of code are equivalent... snd . head . filter (\item@(k,v) -> k == 2)$ dict -- returns 'b'
snd $head$ filter (\item@(k,v) -> k == 2) dict -- returns 'b'
snd (head (filter (\item@(k,v) -> k == 2) dict)) -- return 'b'


Map in Objective-C

Today, I was faced with an issue several times where I had an array of items of one type, and had to convert this into an array of items of a different type. This made me really want a map() function. So I added one to NSArray like so:

NSArray+FP.h

#import <Foundation/Foundation.h>
typedef id(^MapBlock)(id);
@interface NSArray (FP)
- (NSArray *)map:(MapBlock)block;
@end


NSArray+FP.m

#import "NSArray+FP.h"
@implementation NSArray (FP)
- (NSArray *)map:(MapBlock)block {
NSMutableArray *resultArray = [[NSMutableArray alloc] init];
for (id object in self) {
}
return resultArray;
}
@end


So now, I can do this!

NSArray *strArray = [NSArray arrayWithObjects:@"1",@"1",@"2",@"3",@"5",@"8",nil];
NSArray *numArray = [strArray map:^id(id object) {
NSString *str = object;
return [NSNumber ...

How to place custom image in UITextField in iOS

Here is how to put an image in the right portion of a UITextField:

UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(55, 10, 660, 30)];
textField.rightViewMode = UITextFieldViewModeWhileEditing;

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 15, 15)];
imageView.image = [UIImage imageNamed:@"invalidFieldCircle"];
textField.rightView = imageView;


This also assumes that an image called "invalidFieldCircle.png" is in your project's "Resources" directory, which should be located just below the main project directory (you may have to create it).

Force weak reference in Objective-C

Today, I was trying to add an object to an NSDictionary and force it to be a weak reference. This can be done with the [NSValue valueWithNonretainedObject] call:

NSValue *weakRef = [NSValue valueWithNonretainedObject:loadDelegate];
NSDictionary *userInfo = [[NSDictionary alloc] initWithObjectsAndKeys:weakRef, @"loadDelegate", nil];


Just started looking into the Haskell programming language by going through the first few chapters of the awesome book: Learn You a Haskell which is free to read online. Here are some things I really like about what I have seen of Haskell so far:

• Type inference allows you to have static typing but not all the verbosity of most other statically-typed languages.
• Lazy evaluation is huge in Haskell, and it lets you do things like this: let factorial n = product (take n [1..]) So here, we have an infinite list ([1..] creates a infinite list from 1 to infinity!) which from which the first n items are taken and passed to the product function. Awesomeness...
• Nice REPL

Consuming web service in Python with SUDS

import datetime
from suds.client import Client
url = "http://localhost:9080/dataplanws/DataPlanWebService/WEB-INF/wsdl/DataPlanWebService.wsdl"
client = Client(url)
print client.service.hello("Caleb")


Periods in Powershell

For running most commands, powershell and CMD work the same. One big difference I've found is that, when you use periods (.) in a command, they must be surrounded by quotation marks. For instance, this won't work:

mvn clean install -U -Docns.web.dynamic.content.version=2.6.0


But this will work:

mvn clean install -U "-Docns.web.dynamic.content.version=2.6.0"