Skip to content

Instantly share code, notes, and snippets.

@rapha
rapha / TweetReader.js
Created March 11, 2009 12:26
Twitter reader in Rhino Javascript
// Dependencies: rhino, growl, growlnotify, a twitter account (put your login info into the config object)
importClass(java.net.Authenticator)
importClass(java.net.PasswordAuthentication)
importClass(java.lang.Thread)
load('http://json.org/json2.js')
var config = {
email: 'my_username',
@rapha
rapha / maxima.js
Created March 12, 2009 00:45
Interpret single maxima expressions from Rhino Javascript
var expression = arguments[0]
var options = {input:expression, output:''}
runCommand('maxima', '--very-quiet', options)
var result = options.output.replace(/\s+(.*?)\s+/, '$1')
print(result)
@rapha
rapha / x
Created April 7, 2009 02:31
Ubiquity timezone command for Firefox
var noun_type_city = {
_name: "city",
_cities: undefined,
_fetchCityOffsets: function(callback, prefix) {
if (this._cities) {
this._suggestCities(callback, prefix)
} else {
var self = this
jQuery.get( 'http://www.raphscallion.com/clock/timezones.php', {}, function( response ) {
@rapha
rapha / curry.js
Created April 28, 2009 05:42
Currying in Javascript
function curry(func) {
function curried(args) {
return function(nextVal) {
var newArgs = args.concat([nextVal])
if (newArgs.length >= func.arity)
return func.apply(null, newArgs)
else
return curried(newArgs)
}
}
@rapha
rapha / visualise
Created April 30, 2009 07:55
Visualise the class hierarchy in a Java project with this script
#!/bin/bash
DIR=$1
FORMAT=${2:-'pdf'}
FILENAME="hierarchy.$FORMAT"
cat \
<(echo 'digraph types {') \
<(ack '(class|interface) \w+ (extends|implements) \w+' --java -h -o $DIR | \
awk '{print "\"" $4 "\"", "->", "\"" $2 "\""}') \
<(echo '}') \
@rapha
rapha / build-jsjunit.xml
Created May 10, 2009 01:23
JUnit tests in Javascript, using this ant include file which provides the `compile-jsjunit-tests` task
<project name='jsjunit'>
<taskdef resource="net/sf/antcontrib/antcontrib.properties"/>
<property name='slash' value='${file.separator}'/>
<path id='classpath'>
<pathelement path="${junit.jar}"/>
<pathelement path="${js.jar}"/>
<pathelement path="${basedir}"/>
</path>
@rapha
rapha / sums.js
Created May 28, 2009 06:53
find sets of positive integers which sum to another integer
var sums = function(num) {
var sets = [[num]];
for (var i = 1; i <= num/2; i++) {
var left = sums(i);
var right = sums(num-i);
left.forEach(function(lhs) {
right.forEach(function(rhs) {
sets.push(lhs.concat(rhs).sort());
})
})
@rapha
rapha / trampoline.ml
Created May 29, 2009 04:45
Trampolining in OCaml
(* translated from http://blog.richdougherty.com/2009/04/tail-calls-tailrec-and-trampolines.html *)
(* implement trampolining *)
type 'a bounce = Done of 'a | Call of (unit -> 'a bounce)
let rec trampoline = function
| Call thunk -> trampoline (thunk())
| Done x -> x
(* define some functions which use them *)
@rapha
rapha / mixin.js
Created August 10, 2009 09:52
Javascript mixin implementation
function mixable() {
var parents = [];
return {
mixin: function(parent) { parents.push(parent); },
__noSuchMethod__: function(id, args) {
for each (var parent in parents) {
if (typeof parent[id] === 'function') {
return parent[id].apply(this, args);
}
}
@rapha
rapha / chop.scala
Created October 14, 2009 12:55
Karate Chop Kata in Scala
def chop(needle : Int, haystack : Array[Int]) : Int = {
if (haystack.length == 0) return -1
val midPoint = haystack.length / 2
val midValue = haystack(midPoint)
if (midValue == needle)
midPoint
else if (needle < midValue)
chop(needle, haystack.slice(0, midPoint))
else