Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mrspeaker
mrspeaker / pseudo-code.java
Created March 5, 2012 13:56
Why does Notch check X and Y separately in the collision detection routine?
/*
After watching a few of Notch's (and Mojan's) "write a game in a weekend" livecasts and
pulling apart some of the code (http://www.mrspeaker.net/2011/12/30/colorising-sprites-1/)
I've noticed that the collision detection routines are broken into two methods - usually
called "move" and "move2".
The move method gets the amount the entity will move on the X axis and calls
move2 with move2(x, 0). Then the same thing is done with the Y: move2(0,y).
Why would this be necessary? I've broken the code down into pseudo code to see if I
@alandipert
alandipert / ded.clj
Created August 24, 2011 03:18
Command-line structural data editing
(ns ded
"Structural Data EDitor for Clojure with zippers. Inspired by Interlisp: http://larry.masinter.net/interlisp-ieee.pdf"
(:require [clojure.zip :as z])
(:use [clojure.pprint :only (with-pprint-dispatch code-dispatch pprint)]
[clojure.repl :only (source-fn)]))
(defn print-hr
"Prints 30 dashes and a newline."
[c]
(println (apply str (repeat 30 c))))
@nathanmarz
nathanmarz / gist:1165885
Created August 23, 2011 17:11
Benchmark between HashMap with lock vs. persistent map with atom
(defn map-incr-unsafe []
(let [m (java.util.HashMap.)]
(.put m "a" 0)
(doseq [i (range 1000000)]
(let [a (.get m "a")]
(.put m "a" (inc a))
))))
(defn map-incr-safe []
(let [o (Object.)
@ithayer
ithayer / tf-idf.clj
Created June 28, 2011 06:32
Simple tf-idf in 30 lines of clojure. Inspired by a nice simple scala implementation: https://github.com/felipehummel/TinySearchEngine/blob/master/scala/tinySearch.scala and matches as closely as possible the computation.
(ns ignacio.tfidf (:require [clojure.contrib.string :as string])) ;; Simple tfidf in clojure, for fun.
(def stopwords (set (string/split #"\n" (slurp "./stopwords.txt"))))
(defn tokenize [raw-text] ;; Lowercases and splits on non-letters, non-numbers.
(remove stopwords (string/split #"[^a-z0-9äöüáéíóúãâêîôûàèìòùçñ]+" (string/lower-case raw-text))))
(defn idf2 [n-docs match] (Math/pow (Math/log (/ n-docs (count (keys match)))) 2))
(defn index-one [fname] ;; Index for one file. Given an fname, returns a map of token -> map of (fname, count)
(ns example.flash
(:use compojure.core)
(:use ring.util.response)
(:use ring.middleware.session)
(:use ring.middleware.flash)
(:require [compojure.route :as route]))
(defn show-new [flash]
(str "Flash: " flash
"<br/>
;annotation syntax
(import [java.lang.annotation Retention RetentionPolicy Target ElementType]
[javax.xml.ws WebServiceRef WebServiceRefs])
(definterface Foo (foo []))
;annotation on type
(deftype ^{Deprecated true
Retention RetentionPolicy/RUNTIME
javax.annotation.processing.SupportedOptions ["foo" "bar" "baz"]
@dwf
dwf / hinton.py
Created February 1, 2010 20:51
A function for drawing Hinton diagrams with matplotlib.
#!/usr/bin/env python
"""
Draws Hinton diagrams using matplotlib ( http://matplotlib.sf.net/ ).
Hinton diagrams are a handy way of visualizing weight matrices, using
colour to denote sign and area to denote magnitude.
By David Warde-Farley -- user AT cs dot toronto dot edu (user = dwf)
with thanks to Geoffrey Hinton for providing the MATLAB code off of
which this is modeled.
overtcards = []
currentplayer = 0
score = {0=>0,1=>0}
def shuffle(arr)
new=[]
new2=arr.select do |x|x end
arr.size.downto(1) { |n| new.push new2.delete_at(rand(n)) }
new
end