Skip to content

Instantly share code, notes, and snippets.

@briprowe
briprowe / minmax.clj
Created May 29, 2011 02:56
Three different funcitons to find the min and max values in a sequence
(defn minmax-1 [seq]
(loop [[a & more] seq
min a
max a]
(if a
(recur more (if (< a min) a min) (if (> a max) a max))
{:min min :max max})))
(defn minmax-2 [[n & seq]]
(into {} (reduce
@briprowe
briprowe / timing.clj
Created May 29, 2011 02:58
Find the execution time of a function of a sequence.
(defn my-time
"Use this output to generate timings for the 3 functions above."
[f n]
(dotimes [i n]
(time (f (range 1000)))))
(defn minmax-1 [seq]
(loop [[a & more] seq
min a
max a]
(if a
(recur more (min a min) (max a max))
{:min min :max max})))
(defn minmax-2 [[n & seq]]
(into {} (reduce
java -version
java version "1.6.0_25"
Java(TM) SE Runtime Environment (build 1.6.0_25-b06)
Java HotSpot(TM) 64-Bit Server VM (build 20.0-b11, mixed mode)
(defn strongest-paths
"Calculates the strength of the strongest path between each pair of
candidates."
[defeats candidates]
(let [p (into {} (doall (for [i candidates,
j candidates :when (not= i j)]
(if (> (defeats [i j]) (defeats [j i]))
{[i j] (defeats [i j])}
{[i j] 0}))))]
(println p)
(defn strongest-paths
"Calculates the strength of the strongest path between each pair of
candidates."
[defeats candidates]
(let [p (into {} (doall (for [i candidates,
j candidates :when (not= i j)]
(if (> (defeats [i j]) (defeats [j i]))
{[i j] (defeats [i j])}
{[i j] 0}))))]
(println p)
(let [f (partial * 2)]
(map #(%1 %2) (cycle [f identity]) [1 2 3 4 5 6]))
; ==> (2 2 6 4 10 6)
user> (hash (byte-array 3 (map byte [1 2 3])))
391901279
user> (hash (byte-array 3 (map byte [1 2 3])))
568055061
user> (Arrays/hashCode (byte-array 3 (map byte [1 2 3])))
30817
user> (Arrays/hashCode (byte-array 3 (map byte [1 2 3])))
30817
(def *sessions*
^{:private true}
(ref {})) ; session-id :-> session
(definline get-sessions
[]
@*sessions*)
(ns enlive-demo.core
(:require [net.cgrand
[enlive-html :as enlive]]))
(defn odd-li
[content]
(assoc {:tag :li.odd} :content content))
(defn even-li
[content]