Skip to content

Instantly share code, notes, and snippets.

--type-add=ruby=.haml,.rake,.slim
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
$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]
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
require 'sinatra'
$fnids = {}
$counter = 0
def store_fn(fn)
($fnids[$counter += 1] = fn) and $counter
end
def dispatch_fn(fnid)
#!/usr/bin/env python
from flask import Flask, request
app = Flask(__name__)
idx = 0
fnids = {}
def new_fnid(fn):
"""
Generate id for closures.
#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
@rahulkmr
rahulkmr / ants.clj
Created April 17, 2012 13:43 — forked from michiakig/ants.clj
Clojure ant sim from Rich Hickey
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 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
@rahulkmr
rahulkmr / gist:2406645
Created April 17, 2012 15:11
Forked timeout
require 'timeout'
module ForkTimeout
def self.timeout(sec)
if (pid = fork).nil?
yield
else
begin
Timeout::timeout(sec) { Process.waitpid(pid) }
rescue Timeout::Error
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)