Skip to content

Instantly share code, notes, and snippets.

@cqfd
cqfd / sessions_helper.rb
Created October 21, 2010 02:19
Some sample code to illustrate that current_user = foo doesn't set @current_user
def sign_in(user)
cookies.permanent.signed[:remember_token] = [user.id, user.salt]
current_user = user
# self.current_user = user will work though
raise "#sign_in --> @current_user is still nil" unless @current_user == user
# gross hack; otherwise the method returns nil since raise would be last line
user
end
@cqfd
cqfd / uniques.ml
Created April 23, 2011 22:07
OCaml implementation of a nub function
let rec elem x xs =
match xs with
| [] -> false
| y :: ys -> x = y || elem x ys
let rec uniques l =
let rec helper l acc =
match l with
| [] -> acc
| x :: xs ->
@cqfd
cqfd / decorators.rb
Created July 21, 2011 21:19
Python-ish decorators for Ruby
#
# Adding Python-ish decorations to Ruby!
#
# Usage:
#
# class Foo
# extend WithDecorations
#
# decorate :bar do |b|
# puts "Do something here before bar gets invoked!"
@cqfd
cqfd / the_ascii_monad.txt
Created July 28, 2011 01:12
Monads, explained with Ascii art.
Monads are composed of three ingredients.
First, for a given monad m, there are monadic values of type m a, for some type
variable a. In Haskell, if a variable x has type t, we write x :: t.
Here's a picture of a monadic value of type m a.
+--------\
| | \
| m | a > :: m a
@cqfd
cqfd / bruteforce.hs
Created August 5, 2011 17:08
Haskell solution to the 1,3,4,6 problem
import Control.Monad (mapM_)
main :: IO ()
main = printSolutions 24 [1,3,4,6] "+-*/"
data BareTree a = BLeaf a | BBranch (BareTree a) (BareTree a) deriving Show
type Op = Char
toOp :: (Fractional a) => Op -> (a -> a -> a)
@cqfd
cqfd / gist:1510663
Created December 22, 2011 15:19
Adding Python/Clojure-style list comprehensions to Common Lisp
;; Beginner's attempt at adding list comprehensions to Common Lisp.
;; Examples:
;; (for ((x '(1 2 3))) (* x x)) # => (1 4 9)
;; (for ((x '(1 2)) (y '(a b))) (list x y)) # => ((1 a) (1 b) (2 a) (2 b))
(defun foldr (f z xs)
(cond ((null xs) z)
(t (funcall f (car xs) (foldr f z (cdr xs))))))
@cqfd
cqfd / gist:1689599
Created January 27, 2012 16:24
Async Backbone templates
$(function() {
var Template = Backbone.Model.extend({
url: function() {
return 'templates/' + this.get('name');
},
isReady: function() {
return this.get('src');
},
SM = function(options) {
console.log(options);
options || (options = {});
this.current = (options['start'] || this.current || 'start');
this._transitions = {};
this._leaves = {};
this._enters = {};
this._allowed = {};
};
@cqfd
cqfd / gist:2878571
Created June 5, 2012 22:37
Reducing some stuff into a tree
(defrecord Tree [id pid children])
(def comments [{:id 1 :pid 0}
{:id 2 :pid 1}
{:id 3 :pid 1}
{:id 4 :pid 3}
{:id 5 :pid 3}
{:id 6 :pid 3}
{:id 7 :pid 2}
{:id 8 :pid 7}])
@cqfd
cqfd / gists.py
Created July 16, 2012 15:50
POSTing to github's gist api
import json
import urllib
import urllib2
url = "https://api.github.com/gists"
data = {'description': 'gistin from a script',
'public': True,
'files': {
'foobarbaz.txt' : {'content': 'foo bar baz' }
}}