Skip to content

Instantly share code, notes, and snippets.

@michaelficarra
michaelficarra / coffee
Created December 22, 2010 03:59
fully transparent closure-wrapping of loop bodies, showcasing all features
fns = []
for k, v of obj
continue if k is 0
break if k is 1
fns.push (a) ->
console.log k, v
return k
return k if k is 2
@michaelficarra
michaelficarra / test_literals.coffee
Created December 30, 2010 21:01
some literal test
# But keyword assignment should be smart enough not to stringify variables.
func = ->
this == 'this'
ok func() is false
@michaelficarra
michaelficarra / Cakefile
Created December 30, 2010 22:59
only minify browser build if you have uglifyjs installed
task 'build:browser', 'rebuild the merged script for inclusion in the browser', ->
code = ''
for name in ['helpers', 'rewriter', 'lexer', 'parser', 'scope', 'nodes', 'coffee-script', 'browser']
code += """
require['./#{name}'] = new function() {
var exports = this;
#{fs.readFileSync "lib/#{name}.js"}
};
"""
code = """
result = fn a:1, b
result = fn a:1
b
result = fn
a:1
b
result = fn
a:a
b
c:c
result = fn a:1
b:2
@michaelficarra
michaelficarra / hmac.rb
Created June 17, 2011 17:13
HMAC function in ruby
require 'openssl'
def hmac key, message
sha = OpenSSL::Digest::SHA256.new
blocksize = 64 # SHA256 uses a 64 byte (512 bit) block size
def xor str0, str1 # assuming strings are of equal length
b0 = str0.bytes.to_a
b1 = str1.bytes.to_a
result = []
@michaelficarra
michaelficarra / test.js
Created June 24, 2011 00:40
fixed-point (Y) combinator in JS
// compute factorial of 5
Y(function(self){
return function(x){
return x == 0 ? 1 : x * self(x - 1);
};
})(5); // === 120
@michaelficarra
michaelficarra / random.c
Created June 27, 2011 22:44
fair pseudo-random number generation in C
int random(int min, int max) {
if(min == max) return min;
if(min > max) {
int tmp = max;
max = min;
min = tmp;
}
int range = max - min + 1;
int randMax = RAND_MAX - ((RAND_MAX - range + 1) % range);
@michaelficarra
michaelficarra / gist:1070574
Created July 7, 2011 21:25
change when module/require are added to sandbox in REPL
diff --git a/src/coffee-script.coffee b/src/coffee-script.coffee
--- a/src/coffee-script.coffee
+++ b/src/coffee-script.coffee
@@ -88,8 +88,7 @@ exports.eval = (code, options = {}) ->
sandbox[k] = v for own k, v of options.sandbox
sandbox.__filename = options.filename || 'eval'
sandbox.__dirname = path.dirname sandbox.__filename
- # define module/require only if they chose not to specify their own
- unless sandbox.module or sandbox.require
+ unless options.sandbox?