Skip to content

Instantly share code, notes, and snippets.

@beala
beala / gist:2246700
Created March 30, 2012 05:13
Add these mappings to your .vimrc and you'll be able to adjust width and height of your split windows with Alt-Up, Alt-Down, etc.
nnoremap <A-Right> :vertical res +1<cr>
nnoremap <A-Left> :vertical res -1<cr>
nnoremap <A-Up> :res +1<cr>
nnoremap <A-Down> :res -1<cr>
@beala
beala / fib.go
Created March 30, 2012 15:06
Solution for the fib exercise on the Go language tour.
package main
import "fmt"
// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
last := 0
cur := 1
count := 0
@beala
beala / gist:2252361
Created March 30, 2012 15:45
Mappings for OS X because of its non-standard option/alt key *sigh*
nnoremap <leader><Right> :vertical res +5<cr>
nnoremap <leader><Left> :vertical res -5<cr>
nnoremap <leader><Up> :res +5<cr>
nnoremap <leader><Down> :res -5<cr>
@beala
beala / Interp.smalla
Created April 4, 2012 23:24
Interpreter in Smalla
/* 8*7 */
val input1: {l:Int, op:[Plus:Unit, Minus:Unit, Times:Unit, Eq:Unit, Lt:Unit, Gt:Unit, Not:Unit, And:Unit, Or:Unit], r:Int} =
{l:8, op:inj[Plus:Unit, Minus:Unit, Times:Unit, Eq:Unit, Lt:Unit, Gt:Unit, Not:Unit, And:Unit, Or:Unit](Times:()), r:-7}
in
/* 0>10 */
val input2: {l:Int, op:[Plus:Unit, Minus:Unit, Times:Unit, Eq:Unit, Lt:Unit, Gt:Unit, Not:Unit, And:Unit, Or:Unit], r:Int} =
{l:0, op:inj[Plus:Unit, Minus:Unit, Times:Unit, Eq:Unit, Lt:Unit, Gt:Unit, Not:Unit, And:Unit, Or:Unit](Gt:()), r:10}
in
/* 10+10 */
val input3: {l:Int, op:[Plus:Unit, Minus:Unit, Times:Unit, Eq:Unit, Lt:Unit, Gt:Unit, Not:Unit, And:Unit, Or:Unit], r:Int} =
@beala
beala / gist:2307309
Created April 5, 2012 01:40
inferType() and step() test functions
def testInferType(inferType: (TypEnv, Expr) => Typ): Boolean = {
def tester( test: (Expr, Typ) ) = {
val (sourceExpr, expectRes) = test
val res = inferType(typenvEmpty,sourceExpr) == expectRes
if (res == false) {
println("Failed: " + test)
println("Result: " + inferType(typenvEmpty,sourceExpr))
}
res
}
@beala
beala / substr.py
Created April 5, 2012 21:20
Functional implementation of classic substring problem.
def substr(sub, match):
'''Returns True if `sub` is a substring of `match`.
Example: substr("abc", "abababc") # True
'''
def sub_helper(sub, match, fc):
# Base case: empty string is always a substring
if len(sub) == 0:
return True
# Reached end of `match`, but haven't matched all of sub.
# Call failure continuation.
@beala
beala / rev_words.py
Created April 7, 2012 18:47
Reverse Word Order
def rev_words(s):
'''Returns the string `s` with its words in the reverse order.
Example: rev_words("Reverse this!") #"this! Reverse"
'''
def helper(s, word_acc, str_acc):
# Base case: Return `word_acc` plus whatever words
# you have in the string acc.
if len(s) == 0:
return word_acc + str_acc
# This is the end of a word. Clear `word_acc`, and start with
@beala
beala / interning.py
Created April 20, 2012 19:52
The weird behavior of Python's id() function.
import gc
import inspect
gc.disable()
test_counter = 0
def print_test_msg(message):
global test_counter
test_counter += 1
print "%d. %s Line: %d" % (test_counter, message, inspect.currentframe().f_back.f_lineno)
@beala
beala / perm.py
Created May 5, 2012 19:37
Permutations interview question.
# Print each permutation.
def perm(l, n, str_a):
if len(str_a) == n:
print str_a
else:
for c in l:
perm(l, n, str_a+c)
# Return a list of permutations.
def perm2(l, n, str_a, perm_a):
@beala
beala / gist:3254234
Created August 4, 2012 03:55
n-gram
// Some Scala implementations of concepts from "Natural Language Processing For The Working Programmer"
// http://nlpwp.org/book/chap-ngrams.xhtml
/**
* Return all lists of n adjacent words.
* ngram("I love Scala!", 2) = List(List(I, love), List(love, Scala!)
*/
def ngram(sent:String, n:Int):List[List[String]]={
def helper(sent:List[String]):List[List[String]] = {
sent match{