Skip to content

Instantly share code, notes, and snippets.

function test1 {
(*, false, true ) => 1,
(false, true, * ) => 2,
(*, *, false) => 3,
(*, *, true ) => 4
}
function test2 {
(1, { a: 1, b: 2 }) => 1,
(*, { a: 1, b: 2 }) => 2
function balance {
Black(Red(Red(a, x, b), y, c), z, d) => Red(Black(a, x, b), y, Black(c, z, d)),
Black(Red(a, x, Red(b, y, c)), z, d) => Red(Black(a, x, b), y, Black(c, z, d)),
Black(a, x, Red(Red(b, y, c), z, d)) => Red(Black(a, x, b), y, Black(c, z, d)),
Black(a, x, Red(b, y, Red(c, z, d))) => Red(Black(a, x, b), y, Black(c, z, d)),
t => t
}
function balance {
['black', ['red', ['red', *, *, *], *, *], *, *] => 'balance-1',
['black', ['red', *, *, ['red', *, *, *]], *, *] => 'balance-2',
['black', *, *, ['red', ['red', *, *, *], *, *]] => 'balance-3',
['black', *, *, ['red', *, *, ['red', *, *, *]]] => 'balance-4',
* => 'balanced'
}
function test1 {
Black(Red(Red(*, *, *), *, *), *, *) => 'balance-1',
Black(Red(*, *, Red(*, *, *)), *, *) => 'balance-2',
Black(*, *, Red(Red(*, *, *), *, *)) => 'balance-3',
Black(*, *, Red(*, *, Red(*, *, *))) => 'balance-4',
default => 'balanced'
}
function test2(tree) {
var res;
macro to_str {
case { _ ($toks ...) } => {
return [makeValue(#{ $toks ... }.map(unwrapSyntax).join(''), #{ here })];
}
}
to_str(1 2 3 4)
// '1234'
macro protocol {
case { _ $name:ident { $fn:protocol_fn ... } } => {
// Probably not a good idea to rely on `__fresh`. Sweet.js should provide
// some sort of blessed gensym capability.
letstx $id = [makeValue(__fresh(), #{ here })];
return #{
function $name(proto, impl) {
if (!impl) {
impl = proto;
proto = {};
module Control.Global (importLib) where
foreign import importLib
"function importLib(node) {\
\ return function(global) {\
\ if (typeof require !== 'undefined' && typeof process !== 'undefined') {\
\ return require(node);\
\ } else {\
\ return window[global];\
\ }\
@natefaubion
natefaubion / require.sh
Created June 7, 2012 02:52
Sprockets-like require directives in a short shell script
#!/bin/bash
#usage: ./require.sh <root_dir> <main_file> [<output_file>]
cd $1
ftype=`echo $2 | awk -F . '{print $NF}'`
files=""
function parsedeps {
files="$1 $files"
deps=`egrep '^(#|//|/\*)= require .*$' $1 | awk "{print \\$3 \".$ftype\"}" | tail -r`
for dep in $deps; do parsedeps $dep; done
}
@natefaubion
natefaubion / list-any.coffee
Created May 30, 2013 04:03
Parameterized data types using adt.js
{data, any, only} = require 'adt'
# List definition using `any`. While we can restrict the tail to be a only List types,
# we can't restrict the head to be a specific type.
List = data ->
Nil: null
Cons:
head: any
tail: only this
macro $testadd {
case { $$mac $expr } => {
var ident = makeIdent('test', #{ $$mac });
return withSyntax ($ident = [ident]) {
return #{
return $expr + $ident;
}
}
}
}