Skip to content

Instantly share code, notes, and snippets.

View rcampbell's full-sized avatar

Robert Campbell rcampbell

View GitHub Profile
@rcampbell
rcampbell / euler.clj
Created December 23, 2009 11:00
Project Euler solutions in Clojure
;;; euler.clj -- Project Euler solutions in Clojure
;; by Robert Campbell, http://www.robert-campbell.com/
;; Most of my implemented solutions are crude, utilizing only brute force.
;; I'm more interested in exploring functional programming techniques in
;; Clojure than in mathematical optimizations. I do, however, keep to the
;; standard one minute rule, running all programs on a 2.66GHz Core2 Quad.
;; Problems requiring a sequence of primes were originally solved using
@rcampbell
rcampbell / dynamic-form.clj
Created December 29, 2009 14:59
Dynamic form population for Enlive
;; Dynamic form population based on Christophe Grand's suggestions found here:
;; http://bit.ly/72NkNS
(ns dynamic-form
(:refer-clojure :exclude [empty complement])
(:use net.cgrand.enlive-html)
(:import [java.io File]))
(defn populate-select [options]
(fn [node]
@rcampbell
rcampbell / euler.hs
Created December 30, 2009 22:11
Project Euler solutions in Haskell
{- -}
-- Problem 1
foldr (+) 0 (filter (\n -> mod n 3 == 0 || mod n 5 == 0) [1..1000])
@rcampbell
rcampbell / ComparableTests.java
Created January 15, 2010 16:50
Java DSL for testing proper conformance to general contracts for common Object methods
/**
* Any class which implements the <b>Comparable</b> interface must implement <b>compareTo</b>. Run
* these tests on all classes which implement <b>compareTo</b> to confirm they conform to the
* general contract.
*
* @author Robert Campbell
*/
@SuppressWarnings("unchecked")
public class ComparableTests {
@rcampbell
rcampbell / rdf.clj
Created February 18, 2010 07:53
Querying OpenCalais RDF models and MapReducing the results
(ns calais.rdf
(:use [clojure.http.client]
[clojure.contrib.str-utils :only [re-gsub]]
[clojure.contrib.seq-utils :only [frequencies flatten partition-all]])
(:require [clojure.contrib.str-utils2 :as str-utils])
(:import [java.io File FileInputStream]
[com.hp.hpl.jena.rdf.model Model ModelFactory]
[com.hp.hpl.jena.query QueryExecutionFactory QueryFactory]))
(defn load-model [file]
@rcampbell
rcampbell / render.clj
Created February 21, 2010 10:51
Autowiring a model and taxonomy to a view using Enlive
(ns render
(:refer-clojure :exclude [empty complement])
(:use [clojure.set :only [difference]]
[net.cgrand.enlive-html]))
(def this-ns *ns*)
(defmulti populate
"Populate the given tag with content v and
taxonomy options if supported by the tag"
@rcampbell
rcampbell / gist:314600
Created February 25, 2010 15:01
Rendering Calais tag aggregates
(ns calais.web
(:use compojure
[compojure.http response]
[clojure.contrib.def :only [defn-memo]]
[clojure.contrib.seq-utils :only [rand-elt]]
[clojure.contrib.str-utils :only [re-gsub]])
(:require [calais.rdf :as rdf])
(:import [java.io File]))
(declare percent)
(ns match)
(defn nest [m]
"Converts a flat map with keys using underscore
nesting to proper nested maps"
(reduce (fn [m [k v]]
(let [ks (map keyword
(.split (if (keyword? k) (name k) k) "__"))]
(assoc-in m ks v))) {} m))
(ns coordinates
(:import [clojure.lang IPersistentVector IPersistentMap]))
(declare flatten)
(defmulti append
"Appends a given data structure to a flat map"
#(class %3))
(defmethod append IPersistentVector [m k v]
@rcampbell
rcampbell / gist:324297
Created March 7, 2010 10:21
Code Golf - 99 Bottles Of Beer
print "\n\n".join(["%d %s of beer on the wall, %d %s of beer.\n%s"%(i,i>1 and"bottles"or"bottle",i,i>1 and"bottles"or"bottle",i-1==0 and"Go to the store and buy some more, 99 bottles of beer on the wall."or"Take one down and pass it around, %d %s of beer on the wall."%(i-1,i-1>1 and"bottles"or"bottle"))for i in reversed(range(1,100))])