Skip to content

Instantly share code, notes, and snippets.

@michaelficarra
michaelficarra / hammingDistance.c
Created February 4, 2012 00:19
hamming distance
int hammingDistance(int a, int b) {
int accum = 0;
for(int c = a ^ b; c; c >>= 1) {
accum += c & 1;
}
return accum;
}
@michaelficarra
michaelficarra / main.hs
Created February 19, 2012 01:54
higher order types in haskell
{-# LANGUAGE Rank2Types #-}
module Main where
main = print $ f succ' 'c' 4
succ' :: (forall a . (Enum a) => a -> a)
succ' = succ
f :: (Enum c, Show c, Enum d, Show d) => (forall a . (Enum a) => a->a) -> c -> d -> String
f g c d = show (g c) ++ show (g d)
@michaelficarra
michaelficarra / qsort.coffee
Created May 1, 2012 00:50
CoffeeScript quicksort
qsort = (list) ->
return list if list.length <= 1
pivotPoint = medianOfThree list
pivot = list[pivotPoint]
list.splice pivotPoint, 1, []
[left, right] = partition list, (e) -> e < pivot
[(qsort left)..., pivot, (qsort right)...]
medianOfThree = (list) ->
return 0 if list.length < 3
@michaelficarra
michaelficarra / CoffeeScriptIdioms.vim
Created May 17, 2012 22:47
vim substitutions for cleaning up coffeescript projects
:%s/[\r \t]\+$//
:%s/() ->/->/c
:%s/if \!/unless /c
:%s/;$//c
:%s/^\(\s*\)\(@\?[a-z0-9$_]\+\)()/\1do \2/ic
:%s/\!==?/isnt/c
:%s/===\?/is/c
:%s/\([a-z0-9$_]\)(\([^)]\+\))$/\1 \2/ic
:%s/true/yes/c
:%s/false/no/c
@michaelficarra
michaelficarra / input.coffee
Created June 8, 2012 21:56
comprehensions declare their vars in the containing scope; this causes highly unexpected behaviour
for b in a
for c in b
for d in c
do d
wtf = -> d = 0 # wtf, no declaration?
@michaelficarra
michaelficarra / input
Created June 20, 2012 22:25
poll: which AST is better and why?
"a#{b}cd#{"e"}f"
@michaelficarra
michaelficarra / CoffeeScript.peg
Created June 21, 2012 05:06
WIP pure PEG for CoffeeScript
start = program
program = (_ "\n")* (_ toplevelBlock)?
toplevelBlock = toplevelStatement (_ TERMINATOR _ toplevelStatement)* TERMINATOR?
toplevelStatement = !(return / continue / break) statement
block = statement (_ TERMINATOR _ statement)* TERMINATOR?
@michaelficarra
michaelficarra / gist:2968334
Created June 21, 2012 20:29
character sequences that introduce context in coffeescript (and their escapes)
INDENT DEDENT
""" """
''' '''
" "
' '
### ###
# \n
/// ///
/ /
` `
@michaelficarra
michaelficarra / USAGE
Created June 25, 2012 18:26
the program I've been using to test the CoffeeScriptRedux parser
make build && node testParser.js < someFileYouWouldLikeToParse.coffee
@michaelficarra
michaelficarra / gist:3178879
Created July 25, 2012 21:45
demo of first few compilation rules
$ bin/coffee --debug --js -i test.coffee
### PREPROCESSED ###
1 : f ->
2 : (INDENT)a 0, yes
3 : b 0.1, this
4 : (DEDENT)
### PARSED ###
{ type: 'CS.Program',
block:
{ type: 'CS.Block',