Skip to content

Instantly share code, notes, and snippets.

View benjamintanweihao's full-sized avatar

Benjamin Tan Wei Hao benjamintanweihao

View GitHub Profile
@benjamintanweihao
benjamintanweihao / cps.c
Created October 19, 2011 12:02
Awesome Continuation Passing.
p(n-1, k) + p(n-1,k-1) + p(n-1,k-2)
#include <stdio.h>
int pascal(int n, int k) {
int aux(int n, int k, int(*c)(int)) {
int c1(int ret1) {
int c2(int ret2) {
int c3(int ret3) {
@benjamintanweihao
benjamintanweihao / HaskellEgs.hs
Created October 20, 2011 01:51
Haskell Example for CS2104
### Eliminate consecutive duplicates of list elements. ###
compress [] = []
compress (x:xs) = [x] ++ (compress $ dropWhile (== x) xs
### Flatten a nested list structure. ###
flatten :: NestedList a -> [a]
flatten (Elem a ) = [a]
flatten (List (x:xs)) = flatten x ++ flatten (List xs)
@benjamintanweihao
benjamintanweihao / gist:1375509
Created November 18, 2011 03:28 — forked from rctay/sieve1.py
[fork] Sieve
"""Translated from Haskell:
let sieve(p:xs) = p : sieve (filter (\ x -> x `mod` p /= 0) xs) in sieve [2..]
"""
from itertools import ifilter
def ints(k):
while True:
yield k
k+=1
@benjamintanweihao
benjamintanweihao / bash_profile
Created December 10, 2011 13:50
My current pimped PS1
c_cyan=`tput setaf 6`
c_sgr0=`tput setaf 14`
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
PS1="\n\[\e[30;1m\]\[\016\]\[\017\](\[\e[34;1m\]\w\[\e[30;1m\])-(\[\e[1;33m\]\@ \d\[\e[30;1m\])-(\[\e[32;1m\]\$(ls -1 | wc -l | sed 's: ::g') files, \$(ls -lah | grep -m 1 total | sed 's/total //')b\[\e[30;1m\]):\[${c_cyan}\]\e[30;1m\](\e[1;31m\]\$(parse_git_branch)\e[30;1m\]) \[\e[0m\]\n\[\e[1;33m\]#\[${c_sgr0}\] "
@benjamintanweihao
benjamintanweihao / gist:1597877
Created January 12, 2012 01:22
Faster Ruby 1.9.2!
# First get a baseline measurement
cd /your/rails/app
time script/rails runner "puts 1"
# Install a patched ruby
curl https://gist.github.com/raw/996418/e2b346fbadeed458506fc69ca213ad96d1d08c3e/require-performance-fix-r31758.patch > /tmp/require-performance-fix.patch
rvm install ruby-head --patch /tmp/require-performance-fix.patch -n patched
# ... get a cup of tea, this took about 8 minutes on my MBP
# Get a new measurement
@benjamintanweihao
benjamintanweihao / gist:1939043
Created February 29, 2012 08:05
VM from Martin's site.
function VMCheney (instructionArray, heapSlot)
{
this.instructionArray = instructionArray;
this.heapSlot = heapSlot;
}
VMCheney.prototype.HEAP_SIZE = 400;
VMCheney.prototype.SPACE_SIZE = 200;
VMCheney.prototype.peek = function (address, size)

Capybara

save_and_open_page

Matchers

have_button(locator)
@benjamintanweihao
benjamintanweihao / chain.exs
Created June 19, 2013 17:26
My own notes in explains the chain.exs in Programming Elixir, Chapter 12
defmodule Chain do
# There are a couple of interesting things to note in this example.
# Take a look at the 'counter' function:
# It takes in a pid, NOT 'n'.
# 'n' will be received in the form of a message (i.e. pid <- msg )
# In a sense, we are 'loading/setting up' the function first.
def counter(next_pid) do
receive do
n ->
@benjamintanweihao
benjamintanweihao / fred_and_betty.exs
Created June 20, 2013 04:08
Fred and Betty exercise from Programming Elixir, Chapter 12 Write a program that spawns two processes, and then passes each a unique token (for example “fred” and “betty”). Have them send the tokens back.
defmodule Echo do
def echo(token, caller_pid) do
IO.puts "Sending #{inspect token} to #{inspect caller_pid}"
caller_pid <- { caller_pid, token }
end
end
defmodule Receiver do
def report(times) do
Enum.map(1..times,