Skip to content

Instantly share code, notes, and snippets.

-- This is a test of Gist.
fact :: Integer -> Integer
fact 0 = 1
fact n = n * fact (n-1)
main = putStrLn (show (fact 100))
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)
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)
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)
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 = [], [], []
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;
};
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;
(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)))))
;; 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]
;; 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)))