Skip to content

Instantly share code, notes, and snippets.

View janewang's full-sized avatar

Jane Wang janewang

View GitHub Profile
@janewang
janewang / state-monad.coffee
Created July 8, 2012 04:15 — forked from igstan/state-monad.coffee
State Monad in CoffeeScript
push = (element) -> (stack) ->
newStack = [element].concat stack
{value: element, stack: newStack}
pop = (stack) ->
element = stack[0]
newStack = stack.slice 1
{value: element, stack: newStack}
bind = (stackOperation, continuation) -> (stack) ->
@janewang
janewang / gist:3103046
Created July 13, 2012 06:11
Battleship for the Console
from pprint import pprint
import random
import sys
def main():
aircraft_carrier = []
battle_ship = []
submarine = []
destroyer = []
@janewang
janewang / mini_chat.erl
Created July 23, 2012 21:09 — forked from ingrid/mini_chat.erl
Erlang Chat
-module(mini_chat).
-compile(export_all).
-define(ACTIVE_OPTIONS, [{reuseaddr, true}]).
% Commands start with "/", char 47
is_command(Str) -> hd(Str) =:= 47.
chatroom(Users) ->
process_flag(trap_exit, true),
@janewang
janewang / gist:3233545
Created August 2, 2012 04:25 — forked from swannodette/gist:3217582
sudoku_compact.clj
(ns sudoku
(:refer-clojure :exclude [==])
(:use clojure.core.logic))
(defn get-square [rows x y]
(for [x (range x (+ x 3))
y (range y (+ y 3))]
(get-in rows [x y])))
(defn init [vars hints]
@janewang
janewang / gist:3250611
Created August 3, 2012 19:21
Chatty Blubbering
Eshell V5.9.1 (abort with ^G)
1> c(mini_chat).
{ok,mini_chat}
2> mini_chat:start().
true
3> Jane = spawn(mini_chat, user, ["Jane"]).
SYSTEM: Jane has joined the channel.
<0.42.0>
4> Zach = spawn(mini_chat, user, ["Zach"]).
SYSTEM: Zach has joined the channel.
@janewang
janewang / gist:3258829
Created August 4, 2012 17:17
YCombinator Factorial in Erlang
Eshell V5.9.1 (abort with ^G)
% The Y combinator allows recursion to be defined as a set of rewrite rules without requiring
% native recursion support in the language.
c(y_comb_single). % takes single param
{ok,y_comb_single}
(y_comb_single:y2(
fun (F) ->
fun(0) -> 1;
@janewang
janewang / gist:3258841
Created August 4, 2012 17:21
YCombinator List Inversion in Erlang
Eshell V5.9.1 (abort with ^G)
% The Y combinator allows recursion to be defined as a set of rewrite rules without requiring
% native recursion support in the language.
c(y_comb). % takes 2 params
{ok,y_comb}
(y_comb:y2(
fun (R) ->
fun
@janewang
janewang / gist:3259652
Created August 4, 2012 20:07
Y Combinator (standard and recursive) vs. U Combinator vs. Typical Recursion
// Y combinator
var Y = function (f) {
return (
(function (x) {
return f(function (v) { return x(x)(v); }); })
(function (x) {
return f(function (v) { return x(x)(v); }); })
);
}
@janewang
janewang / gist:3259724
Created August 4, 2012 20:22
Read this later
http://matt.might.net/articles/implementation-of-recursive-fixed-point-y-combinator-in-javascript-for-memoization/
http://matt.might.net/articles/js-church/
https://en.wikipedia.org/wiki/Fixed-point_combinator
http://www.cs.brown.edu/courses/cs173/2002/Lectures/2002-10-28-lc.pdf
http://www.dsi.uniroma1.it/~labella/absMcAdam.ps
http://www.cs.utexas.edu/users/wcook/Drafts/2009/sblp09-memo-mixins.pdf
@janewang
janewang / gist:3259869
Created August 4, 2012 20:55 — forked from arjans/gist:3259868
Recursive and Memoized Y-Combinator Fibonnaci Functions
// timer
// console.time('fib');
// console.timeEnd('fib');
//Recursive fibonnaci
var fib_recur = function (n) {
if (n == 0) return 0;
if (n == 1) return 1;
return fib_recur(n-1) + fib_recur(n-2);
};