Skip to content

Instantly share code, notes, and snippets.

View mjg123's full-sized avatar
😅

Matthew Gilliard mjg123

😅
View GitHub Profile
@mjg123
mjg123 / gist:956051
Created May 4, 2011 21:14
my 13 LOC chat server for node.js
var http = require('http');
var url = require('url');
var messages = ["welcome to nodeChat"];
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
var query = url.parse(req.url, true).query;
if ( query.message ){
messages.push( query.message );
res.end("thanks");
} else {
@mjg123
mjg123 / SortedRespectDue.java
Created July 10, 2011 19:43
Testing the behaviour of mergesort in Java's core implementation
import java.util.Arrays;
import java.util.Random;
/**
* Created by Matthew Gilliard
* Date: 10/07/11
* Time: 18:22
*/
public class SortedRespectDue {
@mjg123
mjg123 / SortedRespectDue.java
Created July 10, 2011 19:45
Testing the behaviour of mergesort in Java's core implementation
import java.util.Arrays;
import java.util.Random;
/**
* Created by Matthew Gilliard
* Date: 10/07/11
* Time: 18:22
*/
public class SortedRespectDue {
@mjg123
mjg123 / MergeSort.java
Created July 10, 2011 20:10
Source of mergesort from Java 6
/**
* Src is the source array that starts at index 0
* Dest is the (possibly larger) array destination with a possible offset
* low is the index in dest to start sorting
* high is the end index in dest to end sorting
* off is the offset to generate corresponding low, high in src
*/
private static void mergeSort(Object[] src,
Object[] dest,
@mjg123
mjg123 / hello.cljs
Created July 21, 2011 19:43
Hello World in ClojureScript
(ns hello)
(defn ^:export greet [n]
(str "Hello " n))
@mjg123
mjg123 / hello.js
Created July 21, 2011 19:50
hello.cljs compiled
function b(c){throw c;}var f=null;function aa(){return function(c){return c}}function i(c){return function(){return this[c]}}function j(c){return function(){return c}}var k;
function n(c){var a=typeof c;if(a=="object")if(c){if(c instanceof Array)return"array";else if(c instanceof Object)return a;var d=Object.prototype.toString.call(c);if(d=="[object Window]")return"object";if(d=="[object Array]"||typeof c.length=="number"&&typeof c.splice!="undefined"&&typeof c.propertyIsEnumerable!="undefined"&&!c.propertyIsEnumerable("splice"))return"array";if(d=="[object Function]"||typeof c.call!="undefined"&&typeof c.propertyIsEnumerable!="undefined"&&!c.propertyIsEnumerable("call"))return"function"}else return"null";
else if(a=="function"&&typeof c.call=="undefined")return"object";return a}function ba(c){return typeof c=="string"}Math.floor(Math.random()*2147483648).toString(36);var ca={"\000":"\\0","\u0008":"\\b","\u000c":"\\f","\n":"\\n","\r":"\\r","\t":"\\t","\u000b":"\\x0B",'"':'\\"',"\\":"\\\\"},da={"'":"\\'"};
fun
@mjg123
mjg123 / map2map.clj
Created July 21, 2011 22:17
function to turn a clojure map into a js map in ClojureScript
(defn make-js-map
"makes a javascript map from a clojure one"
[cljmap]
(reduce #(aset %1 (name (first %2)) (second %2)) (js-obj) cljmap))
; (make-js-map {:a "Hello"}) returns "Hello", not the js-map :(
@mjg123
mjg123 / ajax.cljs
Created July 21, 2011 22:43
How to make a json(p) request from ClojureScript using jQuery
(def jquery (js* "$"))
(defn show [msg]
(let [data-as-json ((js* "JSON.stringify") msg nil 4)]
((js* "alert") data-as-json)))
(defn make-js-map
"makes a javascript map from a clojure one"
[cljmap]
(let [out (js-obj)]
@mjg123
mjg123 / helloclosure.cljs
Created July 21, 2011 23:48
Same jsonp call as before but with gclosure instead of jQuery
(ns helloclosure
(:require [goog.net.Jsonp :as jsonp]))
(defn show [msg]
(let [data-as-json ((js* "JSON.stringify") msg nil 4)]
((js* "alert") data-as-json)))
(defn doajax []
(.send (goog.net.Jsonp. (goog.Uri. "http://api.stackoverflow.com/1.1/users/268619") "jsonp")
nil
@mjg123
mjg123 / calculate.clj
Created July 25, 2011 19:24
Why can't I recur here?
(defn calculate
"A list of coin-values from coins, which sum to target"
[coins target]
(let [coin (max-coin coins target)]
(cond
(not coin) (js-alert "No solution")
(= coin target) (list coin)
(< coin target) (cons coin (recur coins (- target coin))))))