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 mandel | |
(:require [clojure.contrib.math :as math]) | |
(:gen-class)) | |
(defn complex [r i] | |
[(double r) (double i)]) | |
(defn add [[r1 i1] [r2 i2]] | |
[(+ r1 r2) (+ i1 i2)]) |
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 take-randnth [num coll] | |
(take num | |
(rest | |
(map first | |
(iterate (fn [[ret items]] | |
(let [idx (rand-int (count items))] | |
[(items idx) | |
(subvec (assoc items idx (items 0)) | |
1)])) | |
[nil |
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 unfold | |
"Next and done? are functions that operate on a seed. next should | |
return a pair, [value new-seed]; the value half of the pair is | |
inserted into the resulting list, while the new-seed is used to | |
continue unfolding. Notably, the value is never passed as an | |
argument to either next or done?." | |
[next done? seed] | |
((fn unfold* | |
[seed] | |
(lazy-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
(ns shuffle.core | |
(:use (incanter core charts))) | |
; naive, O(n+m) | |
(defn take-rand1 [n coll] (take n (shuffle coll))) | |
; lazy, O(n!@#$%m^&) | |
(defn take-rand2 [n coll] | |
(let [coll (vec coll)] | |
(take n (distinct (repeatedly #(rand-nth coll)))))) |
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
;; amalloy's solution to http://4clojure.com/problem/58 | |
(fn [& fs] | |
(reduce (fn [f g] | |
(fn [& x] (f (apply g x)))) | |
fs)) |
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
(->> [1] | |
(iterate (fn [row] | |
(for [[a b] (partition 2 1 | |
(concat [0] row [0]))] | |
(+ a b)))) | |
(take 10)) | |
([1] (1 1) (1 2 1) (1 3 3 1) (1 4 6 4 1) (1 5 10 10 5 1) (1 6 15 20 15 6 1) (1 7 21 35 35 21 7 1) (1 8 28 56 70 56 28 8 1) (1 9 36 84 126 126 84 36 9 1)) |
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
#################################### | |
# BASIC REQUIREMENTS | |
# http://graphite.wikidot.com/installation | |
# http://geek.michaelgrace.org/2011/09/how-to-install-graphite-on-ubuntu/ | |
# Last tested & updated 10/13/2011 | |
#################################### | |
sudo apt-get update | |
sudo apt-get upgrade |
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 my-memo [f] | |
(let [cache (atom {})] | |
(fn [& args] | |
(force (get (swap! cache update-in [args] | |
#(or % (delay (apply f args)))) | |
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 tricky-logic [x] | |
(cond (or (cheap-test1) | |
(expensive-test)) | |
(...body1...) | |
(and (cheap-test2) | |
(expensive-test)) | |
(...body2...) | |
:else (expensive-test))) | |
;; rewrite to avoid repeating tests *or* code: |
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 shuffle.core | |
(:use (incanter core charts))) | |
; naive, O(n+m) | |
(defn take-rand1 [n coll] (take n (shuffle coll))) | |
; lazy, O(n!@#$%m^&) | |
(defn take-rand2 [n coll] | |
(let [coll (vec coll)] | |
(take n (distinct (repeatedly #(rand-nth coll)))))) |
OlderNewer