Skip to content

Instantly share code, notes, and snippets.

@jarpiain
jarpiain / gist:732529
Created December 7, 2010 22:14
Subsampled vector
(deftype Subvec [v n]
clojure.lang.IPersistentVector
(length [_] (quot (count v) n))
(nth [_ i] (v (* i n)))
(assocN [_ i val] (Subvec. (assoc v (* i n) val) n))
(seq [_] (take-nth n (seq v))))
(Subvec. [1 2 3 4 5] 2)
; --> [1 3 5]
@jarpiain
jarpiain / gist:654521
Created October 29, 2010 21:58
Primality testing
(defn prime? [n]
(let [n (int n)
q (int (Math/sqrt (double n)))]
(loop [i 2]
(if (> i q) true
(if (zero? (unchecked-remainder n i)) false
(recur (inc i)))))))
(defmacro currying-fn [args & body]
`(fn
(~args ~@body)
~@(for [i (next (range (count args)))]
(let [[head tail] (split-at i args)]
`(~(vec head) (currying-fn ~(vec tail) ~@body))))))
(def apply-if (currying-fn [p f x] (if (p x) (f x) x)))
(map (apply-if odd? inc) (range 10))