Skip to content

Instantly share code, notes, and snippets.

anonymous
anonymous / gist:621663
Created October 12, 2010 04:35
(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)])
(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
(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
(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))))))
;; amalloy's solution to http://4clojure.com/problem/58
(fn [& fs]
(reduce (fn [f g]
(fn [& x] (f (apply g x))))
fs))
(->> [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))
####################################
# 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
(defn my-memo [f]
(let [cache (atom {})]
(fn [& args]
(force (get (swap! cache update-in [args]
#(or % (delay (apply f args))))
args)))))
(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:
(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))))))