TwitterSuggest

I couldn’t sleep last night, so I wrote a twitter recommendation engine in javascript: twittersuggest.com

I couldn’t sleep last night, so I wrote a twitter recommendation engine in javascript: twittersuggest.com
For those of you who been playing Fling! on your iphone and are stuck on one level or another, I’ve written a solver for any puzzle. Just punch in the location of the flingables, and hit solve.
I recently had to write a few math functions for some Filtered Averaging stuff. I thought somebody else out there might find them useful
def std_dev(list)
# Calculate neccesary values
list_squared = list.map {|item| item*item }
n = list.size
#Calculate the std deviation
right = (Float (sum(list)**2))/n
((Float (sum(list_squared)) - right) / (n-1)) ** 0.5
end
def correlation(x, y)
# Calculate the necessary values
n = x.size
sum_x = sum(x)
sum_y = sum(y)
x_squared = x.map {|item| item*item }
y_squared = y.map {|item| item*item }
sum_x_squared = sum(x_squared)
sum_y_squared = sum(y_squared)
xy = []
x.each_with_index do |value, key|
xy << value * y[key]
end
sum_xy = sum(xy)
# Calculate the correlation value
left = n * sum_xy - sum_x * sum_y
right = ((n * sum_x_squared - sum_x**2) * (n * sum_y_squared - sum_y**2)) ** 0.5
left / right
end
def sum(list)
list.inject( nil ) { |sum,x| sum ? sum+x : x };
end
As I posted a few days ago, I flew out to San Francisco a week or two ago to checkout the dropbox guys. I did some design stuff for them, and since I’ve got back I haven’t been able to get them out of my head. I wanted to keep thing simple, but add some color and some contrast. I’m pretty happy with this very unpolished version I’ve come up with. Anyways, here’s the results of my efforts:
| Original | My Version |
I’ve been writing a yDecoder for swagr the last couple of days (I’ll release the source once I optimize it a bit) and I ran into a bit of a wall. Every article I decoded was 99.9% correct, but each had a couple of extra bytes. I spent several hours working on it and found that it only occurred at the beginning of a couple lines.
After going through a bunch of other yDecoder implementations, I still couldn’t figure out what I was doing wrong. Then I happened across a bright red message on the yEnc site :
Frequent programming problem (28.03.2003)
I was contacted more than a dozen time by developers who are having problems with the decoding or encoding. They all had the same problem: They did not realize that the NNTP-protocol requires to double a dot in the first colum when a line is sent - and to detect a double dot (and remove one of them) when receiving a line.
So if you are directly posting to or reading from a socket (internet conenction socket), then dont forget this, please
The NNTP protocol repeats the . character if it occurs at the beginning of the line because the . is used as a terminating character. So the moral of the story is always read your RFC’s.
UPDATE2: I’ve added a hack to use psuedo instance variables, it works for now but I’ll refactor to a better solution soon.
UPDATE: After some more research and trouble shooting I’ve found that you can’t add instance variables through categories, it looks as though I’d be better off writing a subclass of NSString for this.
I’m currently writing the decoder for yDec and decided I wanted to read files a line at a time. Unfortunately, I wasn’t able to find a simple way of doing this in objective-c so I wrote a category for the NSString class that allows you to read from a NSString a line at a time.
Header:
// // getLine.h // swagr // // Created by Trevor Berg on 8/7/08. // Copyright 2008 Trevor Berg. All rights reserved. // #import <Cocoa/Cocoa.h> @interface NSString (getLine) - (void)setFilePointer:(int)index; - (int)getFilePointer; - (NSString*) getLine; - (void)resetLine; @end
Implementation:
//
// getLine.m
// swagr
//
// Created by Trevor Berg on 8/7/08.
// Copyright 2008 Trevor Berg. All rights reserved.
//
#import "getLine.h"
@implementation NSString (getLine)
static NSMutableDictionary *ivarHolder = nil;
- (NSString*) getLine{
// chop off the first part of the string so that
// you actually get the next line instead of the first line
NSString* mySelf = [self substringFromIndex:[self getFilePointer]];
// find the index of the end of the line
int first = [mySelf rangeOfString:@"\n"].location;
// make sure it found a line
if (first != NSNotFound) {
NSString* line = [mySelf substringToIndex:(first-1)];
// move the index to the start of the next line
[self setFilePointer:(first + 1 + [self getFilePointer])];
// return the line
return line;
}
else {
// Didn’t find another line so return nil
return nil;
}
}
- (void) resetLine {
// reset the index to the beginning of the file
[self setFilePointer:0];
}
- (void)setFilePointer:(int)index {
if ( ivarHolder == nil )
ivarHolder = [[NSMutableDictionary alloc] init];
[ivarHolder setObject:[NSNumber numberWithInt:index] forKey:self];
}
- (int)getFilePointer {
if(ivarHolder == nil) {
return 0;
}
else {
NSNumber* index = [ivarHolder objectForKey:self];
return [index intValue];
}
}
@end
Obviously, this isn’t the most polished solution so feel free to email me any changes to trevorb(at)gmail.com .

I just got back from my first trip to San Francisco, and I have to say I really liked the place. I flew down to check out Dropbox. If you don’t know about Dropbox, they provide data synchronization, backup, version control, and web sharing free of charge ( up to 2 gb). I have a few invites if anyone needs some. It’s clear they have a very good thing going.
I’ve started working on a new project I’m calling swagr. swagr will be an open source Cocoa usenet newsreader. The project is in the very early stages, but I have created a git repository for the code:
https://github.com/dlfnation/swagr/tree
I’ve also purchased Aaron Hillegass’ Cocoa Programming for Mac OS X (Third Edition) to help guide me through the process.
I always forget I have a blog where I’m supposed to announce everything that I do.
I spent a few hours the last couple of days working on a fun little small project called BinaryURL. It does pretty much the opposite of tinyurl, converts a url to a binary string.
Geeknews.net has posted a great list of Computer Science resources in case there’s something you need to brush up on.
I was looking up a computer science term on Wikipedia the other week and as is wont to happen, one thing led to another and I was about 5 or 6 articles deep on a trail of discovery and research to build up my knowledge.
As I realized I was randomizing myself and getting way off the original track, I decided to start assembling the links in one location for my own reference if not some other geek without the funds for their own degree.
Yes, there’s much more that I don’t have below, I wound up throttling back quite a bit after a while to leave it as is…for now.
These are a few of the projects I've been working on.