Skip to content

Instantly share code, notes, and snippets.

(defn pmap-batched [n f xs]
(let [k (int (/ (count xs) n))]
(apply concat (pmap #(doall (map f %)) (partition-all k xs)))))
(defn compute [x] (Thread/sleep 1) x)
(defn compute-all
[batches n]
(pmap-batched batches compute (range n)))
;;;
;;; Blog post at http://wp.me/p12FcK-3
;;;
;;; Loom: http://github.com/jkk/loom
;;; GraphViz: http://graphviz.org
;;; Ubigraph: http://ubietylab.net/ubigraph/
;;;
(ns user
(:use [clojure.string :only [lower-case split-lines join]]
@jkk
jkk / opener.clj
Created September 10, 2010 01:32
(ns user
(:use [clojure.java [io :only [file]] [shell :only [sh]]]))
(defn- os []
"Returns :win, :mac, :unix, or nil"
(condp
#(<= 0 (.indexOf %2 %1))
(.toLowerCase (System/getProperty "os.name"))
"win" :win
"mac" :mac
(ns ^{:doc "Defines protocols for graphs, digraphs, and weighted graphs.
Also provides record implementations and constructors for simple graphs --
weighted, unweighted, directed, and undirected. The implementations are based
on adjacency lists."
:author "Justin Kramer"}
loom.graph)
(set! *warn-on-reflection* true)
;; http://codekata.pragprog.com/2007/01/kata_six_anagra.html
(ns user
(:use [clojure.string :only [join split-lines lower-case]]))
;; map of normalized (with sorted letters) word to set of anagrams
(def anagram-map
(let [words (split-lines (slurp "/usr/share/dict/words"))]
(reduce
(fn [m word]
;; Re: http://twoguysarguing.wordpress.com/2010/08/09/sum-to-a-million/
;; Note: (n*(n+1))/2 is obviously faster
(defn triangle-num [s e]
(let [n (long e)]
(loop [sum (long 0)
i (long s)]
(if (> i n)
sum
(recur (unchecked-add sum i)
;; inspired by http://github.com/Seajure/reducate
(defmacro accumulate
[[acc-sym acc-expr & [bind-sym bind-expr & bind-more]] body]
`(reduce
(fn ~[acc-sym bind-sym]
~(if (empty? bind-more)
body
`(accumulate
~(into [acc-sym acc-sym] bind-more)
~body)))
user=> (use 'clj-wiki.repl)
nil
user=> (examples contains?)
-------------------------
clojure.core/contains?
`contains?` is intended for use on key-based collections such as sets and maps:
(contains? {:foo 1 :bar 2} :foo)
=> true
;; From "Transform columnar data into a tree?" Clojure Google Groups thread:
;; http://groups.google.com/group/clojure/browse_thread/thread/91142e34913e6e28
(use 'clojure.contrib.pprint)
(defn add-path [root path]
(let [add-child (fn [parent child-path]
(if (get-in parent child-path)
parent
(assoc-in parent child-path
(setq swank-clojure-init-files (list "/Users/tin/src/clj/user.clj"))