Skip to content

Instantly share code, notes, and snippets.

@prajwalit
prajwalit / PlaceholderCheck,js
Created January 6, 2011 10:21
For checking support for placeholder
// To detect native support for the HTML5 placeholder attribute
var fakeInput = document.createElement("input"),
placeHolderSupport = ("placeholder" in fakeInput),
clearValue = function () {
if (searchField.val() === originalText) {
searchField.val("");
}
};
@prajwalit
prajwalit / gist:1042064
Created June 23, 2011 07:04
save-layout before killing emacs
;;; save-layout before killing emacs. (needs revive.el) https://github.com/nunb/revive-mode-config
(defun save-state-kill-emacs (&optional arg)
"Save state, buffers and kill emacs"
(interactive "p")
(emacs-save-layout)
(save-buffers-kill-emacs))
(global-set-key (kbd "C-x C-c") 'save-state-kill-emacs)
@prajwalit
prajwalit / get-last.clj
Created August 29, 2011 11:46
Given a sequence return last item.
(defn get-last [vect]
(first (reverse (seq vect))))
(get-last [1 3 2])
(get-last '(1 3 2))
@prajwalit
prajwalit / map-to-querystring.clj
Created August 29, 2011 16:44
Convert map to querystring
(defn map->querystring [params]
(str
"?"
(reduce
#(str % "&" %2)
(map
#(str (first %) "=" (second %))
params))))
(map->querystring {"foo" 1 "bar" 2 "baz" "test"})
@prajwalit
prajwalit / vecs-to-map.clj
Created August 30, 2011 13:30
zipmap implementation.
(defn vecs->map [coll1 coll2]
(apply hash-map (mapcat vector coll1 coll2)))
(vecs->map [1 2 3] [\a \b \c])
;; => {"1" \a, "2" \b, "3" \c}
@prajwalit
prajwalit / drop-items.clj
Created September 1, 2011 06:47
[4clojure #41] Write a function which drops every Nth item from a sequence.
(defn drop-items [coll x]
(loop [coll coll
x x
res []
c 1]
(if (empty? coll)
res
(recur (rest coll)
x
(if (= c x)
@prajwalit
prajwalit / step-reduce.clj
Created September 1, 2011 16:39
[4clojure #60] Write a function which behaves like reduce, but returns each intermediate value of the reduction. Your function must accept either two or three arguments, and the return sequence must be lazy.
(fn step-reduce
([f coll]
(step-reduce f (first coll) (rest coll)))
([f x coll]
(if (seq coll)
(let [next-coll (rest coll)
next-x (f x (first coll))]
(cons x (lazy-seq (step-reduce f next-x next-coll))))
(cons x (lazy-seq '())))))
@prajwalit
prajwalit / smallest-common-number.clj
Created September 1, 2011 17:36
[4clojure #108] Given any number of sequences, each sorted from smallest to largest, find the smallest number which appears in each sequence. The sequences may be infinite, so be careful to search lazily.
(defn smallest-common-number [& seqs]
(letfn [;; Get max of first items of seqs
(max-first [seqs]
(apply max (map first seqs)))
;; Test if first elemets are equal
(first-equal? [seqs]
(apply = (map first seqs)))
;; Filter values less than x
(update-seq [x coll]
(if (> x (first coll))
@prajwalit
prajwalit / maze.clj
Created September 3, 2011 05:55
[4clojure #106] Given a pair of numbers, the start and end point, find a path between the two using only three possible operations: double, halve (odd numbers cannot be halved) add 2 Find the shortest path through the "maze". Because there are multiple sh
(defn next-nums [n]
(filter integer? ((juxt (partial + n) (partial * n) (partial / n)) 2)))
(defn collect-next [coll step x]
(if (coll x)
step
(let [res (set (mapcat next-nums coll))]
(recur res (inc step) x))))
(defn get-steps [initial final]
@prajwalit
prajwalit / rotate.clj
Created September 3, 2011 11:31
[4clojure #44] Write a function which can rotate a sequence in either direction.
(defn rotate [n coll]
(let [[f s] (split-at (mod n (count coll)) coll)]
(concat s f)))
(rotate 2 [1 2 3 4 5])
;; => (3 4 5 1 2)