This file contains hidden or 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 curryfun [arity fun] | |
(fn [& args] | |
(let [argcount (count args)] | |
(cond (= arity argcount) | |
(apply fun args) | |
(< argcount arity) | |
(curryfun (- arity argcount) | |
(fn [& args2] (apply fun (concat args args2)))) | |
(> argcount arity) | |
(apply (apply fun (take arity args)) |
This file contains hidden or 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 powerset [list] | |
(if (empty? list) [list] | |
(let [[hd & tl] list ptl (powerset tl)] | |
(lazy-cat (map #(conj % hd) ptl) ptl)))) | |
(defn set-powerset [a-seq] | |
(if (empty? a-seq) #{#{}} | |
(let [hd (first a-seq) | |
tl (rest a-seq) | |
ptl (set-powerset tl)] |
This file contains hidden or 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
exec """ | |
def hello(): | |
print "hello world" | |
""" | |
hello | |
hello() |
This file contains hidden or 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
/* | |
inner class Java/Clojure interop test case | |
Java part: R.java | |
compile this and put it in your $CLASSPATH | |
*/ | |
public class R { | |
class I { final R outerI = R.this ;} | |
public I makeAnI() { return this.new I(); } | |
} | |
This file contains hidden or 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
overtcards = [] | |
currentplayer = 0 | |
score = {0=>0,1=>0} | |
def shuffle(arr) | |
new=[] | |
new2=arr.select do |x|x end | |
arr.size.downto(1) { |n| new.push new2.delete_at(rand(n)) } | |
new | |
end |
This file contains hidden or 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 monoid? | |
([op zero example-vals check] | |
(and (= zero (op zero zero)) | |
(every? (fn [val] (= val (op zero val) (op val zero))) | |
example-vals) | |
(closure-under? op example-vals check) | |
(a-associative? op (conj example-vals zero))))) | |
(monoid? clojure.set/union #{} [#{:d :g} #{2 3}] set?) | |
(monoid? concat nil [[:a :b] [1 2 3][""]'(a b c d e)] seq?) |
This file contains hidden or 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 cartp | |
"cartesian product" | |
([] ()) | |
([coll] (map list coll)) | |
([coll & rest] (for [x coll y (apply cartp rest)] (conj y x)))) | |
(defn closure-under? | |
[op vals check] | |
(every? (fn [[a b]] (check (op a b))) (cartp vals vals))) | |
This file contains hidden or 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
(defmacro deflayer [name & defs] | |
(let [bindings | |
(apply concat | |
(for [[name val] (partition 2 defs)] | |
[name `(let [~'proceed ~name] ~val)]))] | |
(print bindings) | |
`(defmacro ~name [binding-form# & body#] | |
(apply list binding-form# '~(vec bindings) body#)))) | |
(defmacro with-layer [layer & body] |
This file contains hidden or 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 before-advice [fun] | |
(fn [original] | |
(fn [& args] (apply original (apply fun args))))) | |
(defn after-advice [fun] | |
(partial comp fun)) | |
(defn around-advice [fun] | |
(fn [original] | |
(fun original))) |
This file contains hidden or 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 y [a b] (* 3 a b)) | |
;weave an aspect to function in current Namespace | |
(weave #(= 'y (:name (meta %))) [*ns*] | |
(before-advice (fn [& args] (print args) args))) | |
(def io-aspect [(around-advice (fn [proceed] | |
(fn [& args] (io! (apply proceed args))))) | |
(after-advice (fn [result] (flush) result))]) | |
OlderNewer