Skip to content

Instantly share code, notes, and snippets.

@showell
showell / gist:1664425
Created January 23, 2012 17:38
example of CS/JS line mappings
-----
js:1 (function() {
js:2 var Access, Arr, Assign, Base, Block, Call, Class, Closure, Code, Comment, Existence, Extends, For, IDENTIFIER, IDENTIFIER_STR, IS_STRING, If, In, Index, LEVEL_ACCESS, LEVEL_COND, LEVEL_LIST, LEVEL_OP, LEVEL_PAREN, LEVEL_TOP, Literal, METHOD_DEF, NEGATE, NO, Obj, Op, Param, Parens, RESERVED, Range, Return, SIMPLENUM, Scope, Slice, Splat, Switch, TAB, THIS, Throw, Try, UTILITIES, Value, While, YES, compact, del, ends, extend, flatten, last, merge, multident, starts, unfoldSoak, utility, _ref,
js:3 __hasProp = Object.prototype.hasOwnProperty,
js:4 __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; },
js:5 __indexOf = Array.prototype.indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item
@showell
showell / gist:1663926
Created January 23, 2012 15:51
object creation pattern
root = this
$ = jQuery
root.logger = do ($) ->
logContainer = null
write = (msg) ->
logContainer.append("<p>#{msg}</p>")
pub =
init: (el) -> logContainer = el
log: (msg) -> write msg
@showell
showell / gist:1642339
Created January 19, 2012 20:21
setting up PHP on a mac
Follow instructions here:
http://www.php.net/manual/en/install.macosx.bundled.php
Create /Users/steve/Sites/steve.php:
<?php phpinfo(); ?>
Enable PHP and start Apache:
@showell
showell / gist:1571454
Created January 6, 2012 17:06
static methods in coffeescript
class StaticMath
@pi = 3.14
@sum = (a, b) -> a+b
console.log StaticMath.pi # 3.14
console.log StaticMath.sum(3,4) # 7
# If your class just has statics, this style may
# make more sense
MathNamespace =
@showell
showell / anon_parens.coffee
Created December 31, 2011 00:22
parens around anon functions
call_three_funcs = (f1, f2, f3) ->
f1()
f2()
f3()
# This style is highly unrecommended, but it shows
# that coffeescript can support delimiters around functions.
call_three_funcs (->
console.log "one"
),
@showell
showell / scoping.coffee
Created December 22, 2011 22:20
scoping in CoffeeScript
# This code demonstrates how CS scoping works within a file. Scroll down to the end to
# see how cross-file scoping works.
# Here are the rules.
#
# Scoping is all lexical. Read the file from top to bottom to determine variable scopes.
#
# 1) When you encounter any variable in the top-level nesting, its scope is top level.
# 2) Inside a function, if you encounter a variable name that still exists in an outer scope, then that
# variable name refers to the variable in the outer scope. (This is "closure".)
@showell
showell / line_number_notes.coffee
Created December 9, 2011 19:40
sketch of CS line number support
# This is a sketch of how to add line number support to CoffeeScript.
# It only goes as far as getting line numbers into the AST output from
# nodes. Even that would be a huge step forward, and it would set the
# groundwork for other features, such as line-number mappings.
#
# LEXER HOOKS (coffee-script.coffee)
#
@showell
showell / primes.coffee
Created December 7, 2011 06:52
counting primes in coffeescript (odometer style)
roll_odometer = (factors, odometer) ->
# digits are an array w/least significant digit in
# position 0; for example, [3, [0]] will roll as
# follows:
# [0] -> [1] -> [2] -> [0, 1]
[base, digits] = odometer
i = 0
while i < digits.length
digits[i] += 1
carry = digits[i] == base
@showell
showell / quine.py
Created December 6, 2011 22:44
counting quine in python
import re
def counted_quine():
"This function has been quined 0 times."
ds = counted_quine.__doc__
cnt = int(re.search('(\d+)', ds).group(1)) + 1
code = '''
def counted_quine():
"This function has been quined %d times."
ds = counted_quine.__doc__
@showell
showell / sud.py
Created December 5, 2011 20:16
sudoku
def ok_horizontal(m):
# for each horizontal row in the array, ensure
# that you dont have duplicate values
for lst in m:
# TODO, just use set
vals = {} # values encountered so far
for elem in lst:
if elem is None:
continue