Skip to content

Instantly share code, notes, and snippets.

View pjb3's full-sized avatar

Paul Barry pjb3

View GitHub Profile
(defn detect [pred coll]
(if (empty? coll) nil
(if (pred (first coll))
(first coll)
(recur pred (rest coll)))))
<html>
<head>
<title>JavaScript</title>
<script type="text/javascript">
var template = function() {
with(this) {
buf = []
buf.push("Here are the ")
buf.push( name )
buf.push(":\n<ul>")
(defn move-to [shape x y] (merge shape {:x x :y y}))
(defn r-move-to [shape x y] (move-to shape (+ (shape :x) x) (+ (shape :y) y)))
(derive ::rectangle ::shape)
(derive ::circle ::shape)
(defn rectangle [x y w h] (with-meta {:x x :y y :width w :height h} {:type ::rectangle}))
(defn circle [x y r] (with-meta {:x x :y y :radius r} {:type ::circle}))
(defmulti draw (fn [shape] ((meta shape) :type)))
(defmethod draw ::rectangle [rect]
(println (str "Draw a Rectangle at:(" (rect :x) ", " (rect :y)
"), width " (rect :width) ", height " (rect :height))))
R = 3963.1676
def miles_to_lat(x)
x*(1/((Math::PI/180)*R))
end
def miles_to_lon(x, lat)
x*(1/(((Math::PI/180)*R) * Math.cos(lat*(Math::PI/180))))
end
import clojure.lang.Compiler;
import clojure.lang.PersistentList;
import clojure.lang.Symbol;
import clojure.lang.IPersistentCollection;
public class MyProgram {
public static void main(String[] args) throws Exception {
IPersistentCollection myProgram = new PersistentList(41);
myProgram = myProgram.cons(1);
# true if any fields of this object match any of the values
def any_field_matches_any_value?(fields, values)
re = Regexp.new(values.join("|"), Regexp::IGNORECASE)
fields.map{|f| send(f)}.any?{|v| v.to_s.match(re)}
end
# true if any fields of this object match any of the values
def any_field_matches_any_value?(fields, values)
re = Regexp.new(values.join("|"), Regexp::IGNORECASE)
fields.map{|f| send(f)}.any?{|v| v.to_s.match(re)}
end
$ mkdir -p src/ex
$ echo "(ns ex.Foo (:gen-class :extends Exception))" > src/ex/Foo.clj
$ mkdir classes
$ java -cp src:classes:/Users/pbarry/src/clojure/clojure.jar clojure.lang.Repl
Clojure
user=> (compile 'ex.Foo)
ex.Foo
user=> (ex.Foo. "FAIL")
#<Foo ex.Foo FAIL>
user=> (defn f [x] (if (nil? x) (throw (ex.Foo. "FAIL")) x))
; HTTP query string parser, Clojure
(defn parse-query-string
"Parses a querystring into a hash"
[qs]
(into {} (map #(vec (.split % "=")) (.split qs "&"))))
class Whatever
def foo
@@foo = 42
end
end
class Bar < Whatever
def bar
@@foo = 41
end
end