Skip to content

Instantly share code, notes, and snippets.

(use 'clojure.walk)
;from psykotic http://gist.github.com/328830
(defmacro >> [& xs]
(reduce (fn [x1 x2]
(let [x1- (gensym)]
`(let [~x1- ~x1]
~(postwalk (fn [x] (if (= x '%) x1- x)) x2))))
(macroexpand-all xs)))
(import [java.awt Toolkit]
[java.awt.datatransfer StringSelection DataFlavor
Transferable UnsupportedFlavorException])
(defn clear-clipboard []
(.. Toolkit
getDefaultToolkit
getSystemClipboard
(setContents
(proxy [Transferable] []
@mecdemort
mecdemort / Clojure JTree.clj
Created March 27, 2010 03:45
JTree from a clojure collection
; Turn a map into a JTree
; keys can be any object using toString to display
; listener is called as (listener seq-of-keys-to-selected-node)
;root
; |-child-1
; | |-Child-1-1
; | |-child-1-2
; |-child-2
; |-child-3
(use '[clojure.contrib.lazy-seqs :only [primes]]
'[clojure.set])
(def prime?
(let [mem (ref #{})
primes (ref primes)]
(fn [n]
(if (< n (first @primes))
(@mem n)
(let [[mems ps] (split-with #(<= % n) @primes)]
;;https://github.com/naleksander/instrumentos/blob/master/src/instrumentos/yield.clj
(defn funnel []
(let [q (java.util.concurrent.SynchronousQueue.)]
[(take-while (partial not= q)
(repeatedly #(.take q)))
(fn
([e] (.put q e))
([] (.put q q)))]))
@mecdemort
mecdemort / project.clj
Created March 20, 2011 04:05
project.clj
(defproject myproject "1.0.0-SNAPSHOT"
:description "FIXME: write"
:dependencies [[org.clojure/clojure "1.3.0-alpha6"]
[org.clojure/clojure-contrib "1.2.0"]]
:dev-dependencies [[swank-clojure "1.3.0-SNAPSHOT"]])
F:\Clojure\Projects\myproject>lein deps
Copying 2 files to F:\Clojure\Projects\myproject\lib
Copying 2 files to F:\Clojure\Projects\myproject\lib\dev
F:\Clojure\Projects\myproject>lein swank
Var *classpath* not marked :dynamic true, setting to :dynamic. You should fix th
is before next release!
Exception in thread "main" java.lang.ClassNotFoundException: swank/clj_contrib/p
print$fn__648$pretty_pr_code_STAR___649, compiling:(swank/clj_contrib/pprint.clj
@mecdemort
mecdemort / life.clj
Created March 24, 2011 19:55
Game of life
(definterface AutomataOps
(^long bound [^long x])
(^long getCell [^long x ^long y])
(^long countNeighbors [^long x ^long y])
(^long alive [^long cell ^long neighbors])
(^long updateCell [^long x ^long y]))
(defprotocol Automata
(update [this]))
(defn map-all
"Sequence of results from applying f to the first of each of colls.
Empty colls are dropped and evaluation proceeds."
[f & colls]
(lazy-seq
(when-let [s (seq colls)]
(cons (apply f (map first colls))
(apply map-all f (for [coll (map rest s)
:when (seq coll)]
coll))))))
(defn partitions [sizes coll]
(lazy-seq
(when-let [n (first sizes)]
(when-let [s (seq coll)]
(cons (take n coll)
(partitions (next sizes) (drop n coll)))))))
(defn partitions [sizes coll]
(second (reduce
(fn [[coll res] n]