Skip to content

Instantly share code, notes, and snippets.

View gist:276094
-- This is a test of Gist.
fact :: Integer -> Integer
fact 0 = 1
fact n = n * fact (n-1)
main = putStrLn (show (fact 100))
View collision_counter.py
def normalize(word):
return word.lower()
def coordinates(word):
word = normalize(word)
coords = [0] * 26
for char in word:
coords[ord(char) - ord('a')] += 1
return tuple(coords)
View word_search.py
def all_equal(iterable):
it = iter(iterable)
try:
first = it.next()
return all(x == first for x in it)
except StopIteration:
return True
def median(seq, key=None):
sorted_seq = sorted(seq, key=key)
View word_kdtree.py
def all_equal(iterable):
it = iter(iterable)
try:
first = it.next()
return all(x == first for x in it)
except StopIteration:
return True
def median(seq, key=None):
sorted_seq = sorted(seq, key=key)
View word_kdtree.py
def all_equal(iterable):
it = iter(iterable)
first = next(it, None)
return all(x == first for x in it)
def median(seq, key=lambda x: x):
return sorted(seq, key=key)[len(seq) / 2]
def partition(seq, pivot, key=lambda x: x):
left, middle, right = [], [], []
View http-parser.cpp
typedef void (*notify_callback_t)();
typedef void (*result_callback_t)(const char *str, size_t n);
struct callbacks_t {
notify_callback_t request_begin, request_end;
result_callback_t request_method, request_url, request_protocol;
notify_callback_t header_begin, header_end;
result_callback_t header_name, header_value;
};
View parse.c
typedef void (*notify_callback_t)();
typedef void (*result_callback_t)(const char *str, size_t n);
typedef struct callbacks_t {
notify_callback_t request_begin, request_end;
result_callback_t request_method, request_url, request_protocol;
notify_callback_t header_begin, header_end;
result_callback_t header_name, header_value;
} callbacks_t;
View eager.clj
(defn zip [& xss]
(apply map (fn [& xs] xs) xss))
(defmacro eager [[f & xs]]
(let [syms-and-xs (zip (repeatedly gensym) xs)]
`(let [~@(apply concat syms-and-xs)]
(~f ~@(map first syms-and-xs)))))
View named-kwargs.clj
;; improved version from chouser
(defmacro named [kwargs defaults & body]
`(let [{:keys [~@(take-nth 2 defaults)] :or ~(apply array-map defaults)}
(apply array-map ~kwargs)]
~@body))
;; Example
(defn draw [x & args]
View matrix.clj
;; the double-array should be encapsulated inside a closure, with no escape possible.
;; here it's dangerously exposed for the ease of testing.
(defstruct matrix :num-rows :num-cols :entries)
(defn row-col-index [m row-index col-index]
(+ (* row-index (:num-cols m)) col-index))
(defn num-entries [m]
(* (:num-rows m) (:num-cols m)))
You can’t perform that action at this time.