Skip to content

Instantly share code, notes, and snippets.

@prajwalit
prajwalit / requirejs-request-combiner.js
Created May 8, 2015 19:21
RequireJS Request Aggregator
/**
* A way to override RequireJS' loader so as to use request combiners
* like - https://github.com/rgrove/combohandler
*
* If you have multiple dependencies, say [a, b, c], requirejs fires
* three requests. This modification will allow you to combine these
* files and make a single request to `combo?a.js&b.js&c.js`
* On server-side, you can use a combohandler to serve concatinated
* files.
*/
@prajwalit
prajwalit / delegate.js
Last active December 21, 2015 08:39
Delegate for webkit
var delegate = function (node, type, listener, selector) {
var handler = node.addEventListener (type, function (e) {
var t = e.target, matched = false;
while (!node.isSameNode (t)) {
if (t.webkitMatchesSelector (selector)) {
matched = true;
break;
} else {
t = t.parentNode;
}
@prajwalit
prajwalit / gist:3162926
Created July 23, 2012 10:04
pre-commit hook (for mac)
#!/bin/sh
# A pre-commit hook for js
# It runs jshint on js files.
if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
@prajwalit
prajwalit / jsClosure.js
Created June 12, 2012 21:05
Closure explaination
var makeAdder = function (a) {
return function (num) {
return num + a;
}
};
var inc = makeAdder (1);
// Even after makeAdder has finished execution,
// inc maintains "a".
// That's Closure.
// ==UserScript==
// @include https://www.facebook.com/*
// @include https://facebook.com/*
// @include http://www.facebook.com/*
// @include http://facebook.com/*
// @run-at document-start
// ==/UserScript==
document.addEventListener ('DOMNodeInserted', checksearch, false);
@prajwalit
prajwalit / hanging-paren.clj
Created September 19, 2011 20:37
Just chillin..
(defn hey-paren--whats-up? []
(println "Nothing.. Just hanging out.")
)
@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)
@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 / 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 / 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 '())))))