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 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))) |
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
;;; | |
;;; 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]] |
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
(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 |
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
(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) |
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
;; 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] |
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
;; 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) |
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
;; 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))) |
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
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 |
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
;; 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 |
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
(setq swank-clojure-init-files (list "/Users/tin/src/clj/user.clj")) |