Skip to content

Instantly share code, notes, and snippets.

View kitofr's full-sized avatar

Kristoffer Roupé kitofr

  • Stockholm, Sweden
View GitHub Profile
@kitofr
kitofr / merge 2 arrays to hash
Created February 27, 2011 22:42
Merge 2 arrays into a single hash
([:a, :b, :c].zip([1,2,3])).inject({}){|res, ele| res[ele.first] = ele.last; res} #=> {:c=>3, :a=>1, :b=>2}
;http://4clojure.com/problem/22
#(loop [coll % cnt 0]
(if (empty? coll)
cnt
(recur (next coll) (inc cnt))))
;http://4clojure.com/problem/23
#(loop [coll % stk []]
(if (empty? coll)
stk
(recur (next coll) (cons (first coll) stk))))
;http://4clojure.com/problem/26
#(loop [n % seq []]
(letfn [(fib [x]
(if (<= x 2)
1
(+ (fib (- x 2)) (fib(- x 1)))))]
(if (= n 0)
seq
(recur (dec n) (cons (fib n) seq)))))
;http://4clojure.com/problem/32
#(loop [col % c ()]
(if (empty? col)
(reverse c)
(recur (rest col)
(cons (first col) (cons (first col) c)))))
;http://4clojure.com/problem/34
#(loop [from %1 to %2 c ()]
(if (= from to)
(reverse c)
(recur (inc from) to (conj c from))))
#(loop [col % c ()]
(cond (empty? col) (reverse c)
(= (first col) (first c)) (recur (rest col) c)
:else (recur (rest col) (cons (first col) c))))
;http://4clojure.com/problem/42
#(loop [n % f 1]
(if (= n 1)
f
(recur (dec n) (* f n))))
#(loop [coll %1 n %2 c ()]
(if (empty? coll)
(reverse c)
(recur (rest coll) n (into c (repeat n (first coll))))))
#(loop [f %1 s %2 c ()]
(if (or
(empty? f)
(empty? s))
(reverse c)
(recur (rest f) (rest s) (conj c (first f) (first s)))))