Skip to content

Instantly share code, notes, and snippets.

class Behaviours
constructor: (@prototype) ->
attr_accessor: (attributes...) ->
for attr in attributes
value = null
@prototype['set_' + attr] = (v) -> value = v
@prototype['get_' + attr] = -> value
meta = (def) ->
def::meta.call(new Behaviours(def::))
express = require 'express'
app = module.exports = express.createServer()
app.configure ->
app.set 'views', __dirname + '/views'
app.set 'view engine', 'jade'
app.use express.bodyDecoder()
app.use express.methodOverride()
app.use app.router
@Ezku
Ezku / gist:755666
Created December 26, 2010 21:53
CoffeeScript + Vows + Should
require 'should'
vows = require 'vows'
Container = require '../container'
{EventEmitter} = require 'events'
async = (f) -> ->
promise = new EventEmitter
f arguments...,
(value) -> promise.emit 'success', value
(error) -> promise.emit 'error', error
@Ezku
Ezku / gist:1411585
Created November 30, 2011 22:51 — forked from ccapndave/gist:1411510
Loop timer
callback = (n) -> (-> console.log "Hello from #{n}")
for n in [0..10]
setTimeout (callback n), n * 1000
@Ezku
Ezku / fizzbuzz.coffee
Created December 27, 2011 13:10
Programming with nothing
{ok, deepEqual} = require 'assert'
# Church numerals
ZERO = (p) -> (x) -> x
ONE = (p) -> (x) -> p x
# "Evaluate n, then add another application of p"
SUCC = (n) -> (p) -> (x) -> p(n(p)(x))
toInteger = (p) -> p((n) -> n + 1)(0)
ok (toInteger ZERO) is 0
@Ezku
Ezku / gist:7719983
Last active December 29, 2015 19:49
indent = (amount, string) ->
return string if !amount
indentation = (" " for i in [0..amount-1]).join('')
indentation + string.replace /\n/g, "\n" + indentation
nothing = ""
empty = (stringable) ->
(not stringable?) or
stringable.empty or
@Ezku
Ezku / Thunk.coffee
Last active January 4, 2016 13:58 — forked from anonymous/Thunk.coffee
class Thunk extends Promise
evaluated: false
constructor: (resolver) ->
deferred = Promise.defer()
@eval = ->
new Promise(resolver).then(
deferred.resolve
deferred.reject
)
super (resolve, reject) ->
# See: https://github.com/fantasyland/fantasy-land#comonad
# data Store b a = Store b (b -> a)
# instance Comonad (Store b)
class Store
# { value: b, set: (b -> a) } -> Store b a
constructor: ({@value, @set}) ->
# () -> b
@Ezku
Ezku / gist:57715e88e5b6059fcdbb
Last active August 29, 2015 14:13
Motivating partial application

Consider doing a bunch of HTTP requests.

doRequest('POST', 'api.example.com/foos', foos)
doRequest('POST', 'api.examples.com/bars', bars)

You find you'd like to avoid repeating the POST part, or maybe just bring the concept of a POST request as a first-class entity that can be passed around. Thinking in terms of classes and objects, you might be inclined to do this.

class Requester
	constructor: (@method) ->
	doRequest: (args...) ->
@Ezku
Ezku / gist:21ae60e8aada984aea35
Created April 2, 2015 13:14
Running `source gvp` gets tedious fast, so here’s a script you can place in .zshrc
# At every working directory change, if there's a Godeps file then run gvp
function switch_godeps() {
if [[ -e "Godeps" ]]
then
source gvp
fi
}
chpwd_functions=(${chpwd_functions[@]} "switch_godeps")