This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn detect [pred coll] | |
(if (empty? coll) nil | |
(if (pred (first coll)) | |
(first coll) | |
(recur pred (rest coll))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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>") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; HTTP query string parser, Clojure | |
(defn parse-query-string | |
"Parses a querystring into a hash" | |
[qs] | |
(into {} (map #(vec (.split % "=")) (.split qs "&")))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Whatever | |
def foo | |
@@foo = 42 | |
end | |
end | |
class Bar < Whatever | |
def bar | |
@@foo = 41 | |
end | |
end |