Skip to content

Instantly share code, notes, and snippets.

@bdesham
bdesham / flatten-sets.clj
Created June 3, 2011 03:46
A Clojure function like flatten, except that it pulls elements out of sets instead of sequences.
(defn flatten-sets
"Like flatten, but pulls elements out of sets instead of sequences."
[v]
(filter (complement set?)
(rest (tree-seq set? seq (set v)))))
@bdesham
bdesham / lines.cc
Created June 13, 2011 20:35
Read lines from a file in C++
// <iostream> and <fstream> have been included
void lines() {
ifstream in_file;
in_file.open("input.txt");
string line;
while (in_file.getline(line, 256)) {
cout << "line: \"" << line << "\"\n";
}
@bdesham
bdesham / gist:1023802
Created June 13, 2011 21:47
Calling a function on the values in a map
(defn modify-vals
[f m]
(zipmap
(keys m)
(for [item (vals m)] (f item))))
@bdesham
bdesham / transpose.clj
Created July 6, 2011 16:50
Transpose a matrix in Clojure
(defn square-matrix?
[mat]
(apply =
(count mat)
(for [r mat] (count r))))
(defn transpose
[mat]
{:pre [(square-matrix? mat)]}
(loop [res [],
@bdesham
bdesham / gist:1074160
Created July 10, 2011 02:03
Clojure implementation of Project Euler problem 12
;; Utility functions that will be used later
(def naturals (iterate inc 1))
(defn all-factors
[n]
(filter #(= (mod n %) 0)
(take-while #(<= % (sqrt n))
(rest naturals))))
@bdesham
bdesham / slideshow.css
Created September 2, 2011 20:37
Animate through a series of images with jQuery
ul.slideshow {
list-style: none;
width: 294px;
height: 286px;
float: right;
overflow: hidden;
position: relative;
margin: 0;
padding: 0;
padding-left: 0.5em;
@bdesham
bdesham / gist:1293792
Created October 17, 2011 21:00
Get the Mac OS X version from a Dashcode (JavaScript) app
// return the version of OS X in an array, e.g. a[0] = 10, a[1] = 2, a[2] = 2
function get_macosx_version()
{
var re = new RegExp(".+Mac OS X (\\d+)_(\\d+)_(\\d+)\\).+");
var match = re.exec(navigator.appVersion);
return [parseInt(match[1]), parseInt(match[2]), parseInt(match[3])];
}
@bdesham
bdesham / obfuscate.clj
Created August 11, 2012 20:26
Obfuscate text for use in web pages
; Replaces each character with an HTML escape code like "&#x20;".
; This is useful as some lightweight protection against e-mail harvesting;
; it probably won't perform correctly with non-ASCII input.
;
; example:
; (obfuscate "example.com")
; => "&#x65;&#x78;&#x61;&#x6d;&#x70;&#x6c;&#x65;&#x2e;&#x63;&#x6f;&#x6d;"
(defn obfuscate
[text]
@bdesham
bdesham / gist:4983084
Created February 19, 2013 04:22
Asks the user to input students’ names and grades, storing them for later use
grades = {}
for i in range(number_of_students):
# using input() can be unsafe; use raw_input() instead
name = raw_input("Name: ")
first_grade = int(raw_input("First grade: "))
second_grade = int(raw_input("Second grade: "))
grades[name] = (first_grade, second_grade)
# after running, grades will be something like
(defn matrix
[vectors]
(for [i (range (count vectors))
j (range (count vectors))]
(distance-between (nth vectors i) (nth vectors j))))
; originally written in Python as
; def generate_matrix(vectors):
; size = len(vectors)
; return [[distance_between(vectors[i], vectors[j]) for i in range(size)] for j in range(size)]