Skip to content

Instantly share code, notes, and snippets.

@nathanic
nathanic / gist:1214725
Created September 13, 2011 19:01
jasmine BDD example
describe("Player", function() {
var player;
// hook before each test
beforeEach(function(){
player = new Player();
spyOn(loading,'show'); // intercept and record calls to loading.show
player.play();
});
// actual specifications/tests
@nathanic
nathanic / knight.clj
Created May 10, 2012 14:26
knight's random walk
;; http://www.johndcook.com/blog/2012/05/08/a-knights-random-walk/
;;
;; Start a knight at a corner square of an otherwise-empty chessboard. Move the
;; knight at random by choosing uniformly from the legal knight-moves at each
;; step. What is the mean number of moves until the knight returns to the
;; starting square?
(def MAXPOS 7)
; (pos, move) -> pos
@nathanic
nathanic / ants.py
Created August 2, 2012 16:51
ants sorting woodchips in space
#!/usr/bin/env python
""" This is a simulation of ants moving woodchips around
using the pyscape classes. Hopefully it demonstrates the
construction of central clusters using only decentralized
algorithms followed by autonomous agents.
We begin with a scattering of woodchips across the grid.
Ants randomly walk around, following a simple rule:
if you see a woodchip:
@nathanic
nathanic / reverseStdin.hs
Created August 31, 2012 01:05
Explanatory version of the line-reversing Haskell one-liner
-- a more verbose and explanatory version of this one-liner
-- main = interact $ unlines . map reverse . lines
-- we're going to use some library functions:
-- lines :: String -> [String]
-- unlines :: [String] -> String
-- reverse :: [a] -> [a]
-- reverse works on arbitrary lists, and Strings are [Char].
-- let ... in allows you to bind names to values,
@nathanic
nathanic / quine.hs
Created September 4, 2012 20:34
triple quine in three languages!
nathan@hugin:~/p/haskell/tidbits$ cat quine.hs
q a b c=putStrLn $ b ++ [toEnum 10,'q','('] ++ show b ++ [','] ++ show c ++ [','] ++ show a ++ [')']
main=q "q a b c=putStrLn $ b ++ [toEnum 10,'q','('] ++ show b ++ [','] ++ show c ++ [','] ++ show a ++ [')']" "def q(a,b,c):print b+chr(10)+'q('+repr(b)+','+repr(c)+','+repr(a)+')'" "def e(x) return 34.chr+x+34.chr end;def q(a,b,c) print b+10.chr+'main=q '+e(b)+' '+e(c)+' '+e(a)+' '+10.chr end"
nathan@hugin:~/p/haskell/tidbits$ runghc quine.hs
def q(a,b,c):print b+chr(10)+'q('+repr(b)+','+repr(c)+','+repr(a)+')'
q("def q(a,b,c):print b+chr(10)+'q('+repr(b)+','+repr(c)+','+repr(a)+')'","def e(x) return 34.chr+x+34.chr end;def q(a,b,c) print b+10.chr+'main=q '+e(b)+' '+e(c)+' '+e(a)+' '+10.chr end","q a b c=putStrLn $ b ++ [toEnum 10,'q','('] ++ show b ++ [','] ++ show c ++ [','] ++ show a ++ [')']")
@nathanic
nathanic / tsalesp.py
Created October 2, 2012 14:10
Simulated Annealing in Python in the early aughties
#!/usr/bin/env python
import random
from math import *
# This is an attempt at solving the travelling salesperson problem
# with simulated annealing. I tried this once in matlab and it sucked.
# a map is a list of tuples, one tuple for each city.
# each tuple represents the (x,y) location of a city on the map.
@nathanic
nathanic / greetings.clj
Created October 12, 2012 16:28
simple lazybot plugin, detects and replies to greetings
(ns lazybot.plugins.greetings
(:use lazybot.registry)
(:use [lazybot.utilities :only [prefix]])
(:require [clojure.string :as s]))
(def morning-patterns
[#".*good.morning.*"
#".*top.*o.*morning.*"
])
@nathanic
nathanic / big_ben.clj
Created November 14, 2012 16:01
clojure implementation of josh's big ben bot
(ns big-ben.core
(:use [clj-time.core :exclude [extend]]
[clj-time.local])
(:require [irclj.core :as ircb]
[overtone.at-at :as at-at]
[clojure.string :as string]))
(def HOUR-IN-MS (* 60 60 1000))
(def config
@nathanic
nathanic / apples_to_apples.clj
Created November 14, 2012 16:53
closer match to the style of silly_big_ben_irc_bot
(ns simplebot.core
(:use [clj-time.core :exclude [extend]]
[clj-time.local])
(:require [irclj.core :as ircb]
[overtone.at-at :as at-at]
[clojure.string :as string]))
(def CHANNEL "#silly-bot-test")
(defn -main [& args]
@nathanic
nathanic / feelings.clj
Created November 29, 2012 16:11
bitsbot feelings plugin
(ns lazybot.plugins.feelings
(:require [clojure.string :as string])
(:use [lazybot registry]
[somnium.congomongo :only [fetch fetch-one insert! destroy!]]))
; feelings.clj: a lazybot plugin to remember and report how people feel about various stuff
; TODO: parameterize prefix instead of literal '@', or don't bother?
(def love-responses