Skip to content

Instantly share code, notes, and snippets.

@cshepp
cshepp / socket.iodisconnect.js
Created August 3, 2012 15:10
Socket.io disconnect
socket.on('disconnect', function(socket){
socket.get('room', function(err, room){
socket.get('nick', function(err, nick){
socket.broadcast.to(room).emit('client-left', nick);
});
});
});
@cshepp
cshepp / gyro.html
Last active December 15, 2015 02:19
Excuse the ugly code - this was just a proof-of-concept. Because my "server" wasn't always at the same IP address, I made my phonegap app load a generic index.html file (from the /assets/www directory within the app) that would allow me to specify a page to load - in this case, I would load my.server.ip.address:port/gyro.html. Gyro.html takes ca…
<!doctype html>
<head>
<title>controller</title>
<!--<link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.css" />-->
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<!--<script src="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.js"></script>-->
<script src="socket.io/socket.io.js"></script>
<script src="cordova-2.0.0.js"></script>
<script>
var transmit = false;
@cshepp
cshepp / static-blog.js
Created June 27, 2014 18:35
Static Blog Generator
/*
Static Site Generator
Author: Cody Shepp 6/27/2014
License: MIT
The only outside dependency is mustache (npm install mustache)
The basic folder structure I used is as follows:
/
(defn find-cycle
"finds a single cycle in the parents
starting at the seed index"
[a b seed]
(loop [coll (mapv vector a b)
cyl #{}
cur (get-in coll [seed 0])]
(let [idx (first (keep-indexed #(if (= (first %2) cur) %1) coll))]
(if (nil? idx)
(vec cyl)
(defn create-population [gen-size genome-size]
"creates a random population of size gen-size,
with each genome of size genome-size"
(map (fn [x] (vec (unique-random-numbers genome-size)))
(range gen-size)))
;; from https://clojuredocs.org/clojure.core/rand-int
(defn unique-random-numbers [n]
"Generates a list of unique random ints between 0 and n"
(let [a-set (set (take n (repeatedly #(rand-int n))))]
;; data from http://www.codeproject.com/Articles/259926/Introduction-to-Genetic-Algorithm-Encoding-Camel
;; licensed under the CPOL: http://www.codeproject.com/info/cpol10.aspx
(def distances [[ 0 28 57 72 81 85 80 113 89 80 ]
[ 28 0 28 45 54 57 63 85 63 63 ]
[ 57 28 0 20 30 28 57 57 40 57 ]
[ 72 45 20 0 10 20 72 45 20 45 ]
[ 81 54 30 10 0 22 81 41 10 41 ]
[ 85 57 28 20 22 0 63 28 28 63 ]
[ 80 63 57 72 81 63 0 80 89 113 ]
[ 113 85 57 45 41 28 80 0 40 80 ]
(defn calculate-fitness [genome]
"Returns the total distance travelled for a given solution"
(let [path (pathify genome)
distances (map get-distance path)]
(reduce + distances)))
(defn pathify [vec]
"turns a vector into a vector of transitions,
including last element back to the first
(def num-participants 4) ;; how many solutions should take part in each tournament
(defn tournament-select [population]
"order the members of the population according
to their success in...battles to the death!"
(mapv (fn [x] (let [idxs (repeatedly num-participants #(rand-int (count population)))
participants (map #(population %) idxs)]
(first (sort-by calculate-fitness participants))))
(range (count population))))
(def fitness-multiplier 100)
(defn roulette-select [population]
"randomly selects solutions -
probability of being selected is
weighted using fitness score"
(let [roulette-wheel (reduce #(conj %1 (+ (* (- 700 %2) fitness-multiplier) (last %1))) [0] (map calculate-fitness population))
total (last roulette-wheel)]
(mapv (fn [x] (nth population (.indexOf roulette-wheel (first (filter #(< (rand-int total) %1) roulette-wheel)))))
(range (count population)))))
(defn find-all-cycles [a b]
"finds all possible cycles in the parents"
(loop [idx 0
cyl (order-preserving-set)]
(let [c (find-cycle a b idx)]
(if (= idx (count a))
(vec (seq cyl))
(recur (inc idx) (into cyl [c]))))))
(defn find-cycle [a b seed]