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 graph-stats [^Graph g] | |
(vector (clustering-coefficient g) | |
(average-shortest-paths g) | |
(diameter g))) | |
(defn print-table [graph-generator sizes] | |
(println "+----------+------------+------------+------------+") | |
(println "| size | C | APL | D |") | |
(println "+----------+------------+------------+------------+") | |
(doseq [s sizes] |
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 *sep* "/") | |
(def *foo-jar* "foo.jar") | |
(def *foo-lib* (str-join *sep* ["lib" *foo-jar*])) | |
(def *where* | |
["../foo-idea/out/artifacts/foo_jar"]) | |
(defn find-foo [paths] | |
(if (seq paths) | |
(let [path (first paths) | |
full-path (str-join *sep* [path *foo-jar*])] |
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 generate_row(row_number): | |
val = 1 | |
r = row_number + 1 | |
yield val | |
for col in xrange(1, row_number+1): | |
val = (val * (r - col)) / col | |
yield val | |
#@memoize | |
def create_row(row_number): |
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
;; original posted by Marco (https://me.yahoo.com/a/CkZaE3IH2YClbGr69za54DVt8pHk) | |
;; as a comment of | |
;; http://www.enrico-franchi.org/2011/02/callcc-i-yield-bah-lazy-seq.html | |
(define node-value car) | |
(define node-left cadr) | |
(define node-right caddr) | |
(define children cdr) | |
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 tree->seq [tree] | |
(letfn [(tree->seq-aux | |
[stack] | |
(if-let [s (seq stack)] | |
(let [{:keys [value left right]} (first stack)] | |
(lazy-seq | |
(cons value | |
(tree->seq-aux | |
(concat (keep identity [left right]) | |
(rest stack)))))) |
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
(define (generate tree) | |
(let ((jump null)) | |
(lambda () | |
(if (null? jump) | |
(call/cc | |
(lambda (yield) | |
(let loop ((stack (list tree))) | |
(cond | |
((null? stack) null) | |
(else |
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
import collections | |
Node = collections.namedtuple('Node', 'value left right') | |
def make_node(value, left=None, right=None): | |
return Node(value, left, right) | |
def preorder(tree): | |
stack = [tree] | |
while stack: |
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 make-iterator [tree] | |
(let [stack (ref [tree])] | |
(proxy [Iterator] [] | |
(next [] | |
(if (seq @stack) | |
(let [{:keys [left right value]} (first @stack)] | |
(dosync | |
(alter stack | |
#(concat (keep identity [left right]) | |
(rest %))) |
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
import itertools | |
import random | |
import copy | |
import sys | |
import pickle | |
try: | |
import psycho | |
psycho.full() | |
except ImportError: |
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
(proclaim '(optimize (speed 3) (space 0) (debug 0))) | |
(defparameter *width* 80) | |
(defparameter *height* 23) | |
(defparameter *length* (* *width* *height*)) | |
(defun neighbours (pos) | |
(mapcar (lambda (x) (mod x *length*)) | |
(delete pos | |
(mapcan (lambda (pos) |