This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- This is a test of Gist. | |
fact :: Integer -> Integer | |
fact 0 = 1 | |
fact n = n * fact (n-1) | |
main = putStrLn (show (fact 100)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = [], [], [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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))) |
OlderNewer