Skip to content

Instantly share code, notes, and snippets.

@disnet
disnet / gist:3407999
Created August 20, 2012 21:18 — forked from johnkpaul/gist:2361303
gruntjs Mocha task
/*
* grunt
* https://github.com/cowboy/grunt
*
* Copyright (c) 2012 "Cowboy" Ben Alman
* Copyright (c) 2012 John K. Paul @johnkpaul
* Licensed under the MIT license.
* http://benalman.com/about/license/
*/
@disnet
disnet / hoisting.md
Created September 5, 2012 18:28
JavaScript var hoisting

Here's the standard example:

var x = "bar";
(function() {
  console.log(x) // undefined
  var x = 42;
})()

But what happens when you have a parameter?

@disnet
disnet / gist:3855533
Created October 8, 2012 23:07
conspair
macro conspair {
case [$head] => { [$head] }
case [$head $tail ...] => {
[$head, conspair [$tail ...]]
}
}
conspair [1 2 3 4]
@disnet
disnet / annotate.js
Created October 16, 2012 21:19
parameter annotations (type, contract, etc.) in sweet.js
macro def {
case $name:ident ( $($params:ident : $type:ident) (,) ...) $body => {
// just throwing away the type annotation. The semantics of type
// annotations left as an exercise to the reader :)
function $name ($params (,) ...) $body
}
}
def add (a : Number, b : Number) {
return a + b;
@disnet
disnet / gist:4165526
Created November 28, 2012 23:29
javascript - sweet.js macro checking for null
macro null_helper {
case ($processed ...) ($rest) => {
$processed (.) ... . $rest
}
case ($processed ...) ($rest_head $rest ...) => {
$processed (.) ... . $rest_head
&& null_helper ($processed ... $rest_head) ($rest ...)
}
}
@disnet
disnet / scheme.sjs
Created October 8, 2012 19:02
start of scheme in sweet.js (modified from http://pastebin.com/0nPBg8LY)
macro sexp {
case () => {
;
}
case ($p) => {
$p
}
case ($x $y) => {
$x($y);
}
macro class {
case $className { constructor $constParam $constBody $rest ... } => {
function $className $constParam $constBody
class $className { $rest ... }
}
case $className { private_function $pMethodName $pMethodParam $pMethodBody $rest ...} => {
function $pMethodName $pMethodParam $pMethodBody
class $className { $rest ... }
@disnet
disnet / gist:6024833
Last active December 19, 2015 22:08
simple object structure case matching macro
macro _match_cond {
case $o ($field) => {
(typeof $o.$field !== 'undefined')
}
case $o ($field $rest ...) => {
_match_cond $o ($field) && _match_cond $o ($rest ...)
}
}
macro _match_var {
@disnet
disnet / gist:6024991
Created July 17, 2013 22:09
cond macro
macro _arms {
case (default => $value:expr) => {
else {
return $value;
}
}
case (case $cond:expr => $value:expr) => {
if($cond) {
return $value;
}
macro forThing {
case $val ($a:expr, $b:expr) => {(function($val){forEach.call($b, this)}).bind($a)}
case $val ($a:expr) => {(function($val){forEach.call(val,this)}).bind($a)}
}
function makeMutationObserver(tag){
var queue= tag.queue,
put= queue.put.bind(queue),
mutationAddObserve= forThing val (put, val.getElementsByTagName(this.tag)),
mutationObserver= forThing val (mutationAddObserve, val.addedNodes),
mutationsObserver= forThing val (mutationObserver)