Skip to content

Instantly share code, notes, and snippets.

@amalloy
amalloy / gist:848084
Created February 28, 2011 21:36 — forked from ray1729/gist:848069
;; not sure whether this is an improvement but I don't really like letfn
(defn keep-only
[n bucket-fn s]
((fn my-filter
[s seen]
(lazy-seq
(when (seq s)
(let [e (first s)
b (bucket-fn e)
c (inc (get seen b 0))]
(defmacro defcomp [name doc args & fs]
(let [fnmeta {:doc doc :arglists `'(~args)}]
`(def ~(with-meta name fnmeta) (comp ~@fs))))
(use 'name.choi.joshua.fnparse)
(def tokenize (partial re-seq #"[\[\](){}]|[^\[\](){}]+"))
(defn initial-state
[input] {:remainder (tokenize input)
:brackets (list)})
user>
(let [[_ e-class e-name & e-ret :as catch-clause] '(catch Exception _ x)]
`(catch ~e-class ~e-name (do (println ~e-name) ~@e-ret)))
(catch Exception _ (do (clojure.core/println _) x))
(defmacro add-field [document field]
(let [fstr (str field)]
`(.add document (Field. ~fstr ~field
Field$Store/YES Field$Index/ANALYZED))))
(defn create-document
[json]
(let [age (NumericField. "age" Field$Store/YES true)
vintage (NumericField. "vintage" Field$Store/YES true)
document (Document.)
cascalog.playground> (?<- (stdout) [?person ?age] (age ?person ?age) (< ?age 30))
INFO - using default application jar, may cause class not found exceptions on the cluster
INFO - using application jar: /home/akm/src/clojure/hadoop/lib/cascading-core-1.2-wip-63.jar
INFO - [] starting
INFO - [] source: cascalog.MemorySourceTap@3e0
INFO - [] sink: StdoutTap["SequenceFile[[UNKNOWN]->[ALL]]"]["/tmp/temp69430827573878467652505554465178270"]"]
INFO - [] parallel execution is enabled: false
INFO - [] starting jobs: 1
INFO - [] allocating threads: 1
INFO - [] starting step: (1/1) ...eFile[[UNKNOWN]->[ALL]]"]["/tmp/temp69430827573878467652505554465178270"]"]
(defn break
[pred coll]
(lazy-seq
(if-let [[x & rest :as all] (not-empty coll)]
(if-not (pred x)
[() all]
(let [[a b] (break pred rest)]
[(cons x a) b]))
[() ()])))
Which do you like better? Will the second have issues if the user has an unusual ns declaration, in which clojure.core/cond is not mapped?
(defmacro cond [& clauses]
(if-not (even? (count clauses))
(throw (IllegalArgumentException. "cond requires an even number of forms"))
(reduce (fn [else [test then]]
`(if ~test ~then ~else))
nil
(reverse (partition 2 clauses)))))
@amalloy
amalloy / core.clj
Created March 23, 2011 18:21 — forked from Raynes/core.clj
(import java.awt.event.ActionListener)
(defn add-action [cmp fn]
(doto cmp
(.addActionListener (doto (proxy [ActionListener] [])
(update-proxy {"actionPerformed" fn})))))
(defn add-action [cmp fn]
(doto cmp
(.addActionListener (reify ActionListener
<!--
Seriously guys, CSS has been around a long time. Don't send me these
disgusting nested <table>s; I will never buy a product after seeing this.
-->
</td>
</tr>
</table>
</td>
</tr>