This file contains hidden or 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
| --type-add=ruby=.haml,.rake,.slim |
This file contains hidden or 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
| require 'set' | |
| require 'test/unit' | |
| def match(str, start, finish, trans) | |
| state = [start] | |
| str.each_char do |c| | |
| # Obtain next states. | |
| state = state.map {|s| trans[s][c] if trans[s] and trans[s][c] }.flatten | |
| break if state.nil? | |
| end or return false |
This file contains hidden or 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
| $graph = {a: [:b, :c], b: [:d, :e], c: [:f, :g]} | |
| def dfs_nr(node) | |
| queue = [node] | |
| seen = {} | |
| while not queue.empty? | |
| node = queue.pop | |
| puts node | |
| seen[node] = true | |
| if $graph[node] |
This file contains hidden or 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 graph_search(graph, initial, queue) | |
| queue.enqueue(initial) | |
| seen = {} | |
| while not queue.empty? | |
| node = queue.dequeue | |
| puts node | |
| seen[node] = true | |
| if graph[node] | |
| graph[node].each {|child| queue.enqueue child unless seen[child] } | |
| end |
This file contains hidden or 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
| require 'sinatra' | |
| $fnids = {} | |
| $counter = 0 | |
| def store_fn(fn) | |
| ($fnids[$counter += 1] = fn) and $counter | |
| end | |
| def dispatch_fn(fnid) |
This file contains hidden or 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
| #!/usr/bin/env python | |
| from flask import Flask, request | |
| app = Flask(__name__) | |
| idx = 0 | |
| fnids = {} | |
| def new_fnid(fn): | |
| """ | |
| Generate id for closures. |
This file contains hidden or 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
| #lang web-server | |
| (require web-server/servlet-env) | |
| (define-syntax page | |
| (syntax-rules () | |
| [(page x ...) | |
| (response/xexpr `(html (body ,x ...)))])) | |
| (define (ask action kont-field msg) | |
| (page |
This file contains hidden or 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
| ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Ant sim ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
| ; Copyright (c) Rich Hickey. All rights reserved. | |
| ; The use and distribution terms for this software are covered by the | |
| ; Common Public License 1.0 (http://opensource.org/licenses/cpl.php) | |
| ; which can be found in the file CPL.TXT at the root of this distribution. | |
| ; By using this software in any fashion, you are agreeing to be bound by | |
| ; the terms of this license. | |
| ; You must not remove this notice, or any other, from this software. | |
| ;dimensions of square world |
This file contains hidden or 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
| require 'timeout' | |
| module ForkTimeout | |
| def self.timeout(sec) | |
| if (pid = fork).nil? | |
| yield | |
| else | |
| begin | |
| Timeout::timeout(sec) { Process.waitpid(pid) } | |
| rescue Timeout::Error |
This file contains hidden or 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 sys | |
| def show(*args): | |
| for obj in args: | |
| frame = sys._getframe(1) | |
| for k, v in frame.f_locals.iteritems(): | |
| if v is obj: | |
| print "%s=%r" % (k, v) | |