Skip to content

Instantly share code, notes, and snippets.

View daviddavis's full-sized avatar

David Davis daviddavis

View GitHub Profile
sc () {
if [[ -f ./script/rails ]]
rails c $@
else
./script/console $@
fi
}
sc () {
if [[ -f ./script/rails ]]
then
rails c $@
else
./script/console $@
fi
}
@daviddavis
daviddavis / test.clj
Created January 19, 2011 13:59
This is a test
(defn fib
([] (concat [0 1] (fib 0 1)))
([a b] (lazy-cons (+ a b) (fib b (+ a b)))))
user> (take 20 (fib))
(0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181)
@daviddavis
daviddavis / Exception
Created March 13, 2011 17:09
palindromes.clj
Exception in thread "main" java.lang.Exception: Unable to resolve symbol: re-gsub in this context (palindromes.clj:27)
at clojure.lang.Compiler.analyze(Compiler.java:5205)
at clojure.lang.Compiler.analyze(Compiler.java:5151)
at clojure.lang.Compiler$InvokeExpr.parse(Compiler.java:3036)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:5371)
at clojure.lang.Compiler.analyze(Compiler.java:5190)
at clojure.lang.Compiler.analyze(Compiler.java:5151)
at clojure.lang.Compiler$BodyExpr$Parser.parse(Compiler.java:4670)
at clojure.lang.Compiler$FnMethod.parse(Compiler.java:4328)
(ns testing
(:require [clojure.string :as str]))
(defn sanitize [n]
(str/replace #"[^a-z]" "" (.toLowerCase n))
)
(print (sanitize "abba"))
;; output is "abba[abba^abba-abba]abba"
(ns palindromes
(:require [clojure.string :as str]))
(def text
(str "I'll just type in some example text here and embed a little
palindrome - A man, a plan, a canal, Panama! - I expect that will be
the longest palindrome found in this text.
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Integer volutpat lorem imperdiet ante bibendum ullamcorper. Mauris
@daviddavis
daviddavis / reverse1.clj
Created April 26, 2011 21:47
Implementing my own reverse
;; Exception in thread "main" java.lang.UnsupportedOperationException: Can only recur from tail position (test.clj:6)
(defn rev [data]
(loop [data data, product ()]
(if (empty? data)
product
((conj product (first data))
(recur (rest data) product)))))
(print (rev '(1 2 3 4 5)))
def happy?(x)
seen = []
while !(seen.include?(x))
seen << x
x = x.to_s.chars.inject(0) { |sum, i| sum += (i.to_i * i.to_i) }
#puts "iteration: #{x}\n"
return true if x == 1
end
return false
@daviddavis
daviddavis / gist:990713
Created May 25, 2011 10:10
PragProg Clojure magazine
PragPub, the free monthly electronic magazine for software
developers from The Pragmatic Programmers, will be publishing a
special Clojure issue in July, and we're looking for articles.
I'm committed to sourcing at least one article in that issue
from one of the many active Clojure user groups around the
world. I've been a believer in developer user groups since the
1980s when I was editing Dr. Dobb's Journal, inspired by Dennis
Allison's image of a community of developers standing on each
other's shoulders, and I know there are some great ideas out
@daviddavis
daviddavis / 1.9.2.fast-require.patch
Created July 7, 2011 22:24
Ruby 1.9.2 patch to speed up require
diff --git a/array.c b/array.c
index b1616c5..16326fc 100644
--- a/array.c
+++ b/array.c
@@ -302,7 +302,7 @@ ary_alloc(VALUE klass)
return (VALUE)ary;
}
-static VALUE
+VALUE