Skip to content

Instantly share code, notes, and snippets.

@levjj
levjj / fac.sjs
Last active August 29, 2015 13:56
Computing factorial with sweet.js
macro fac {
case infix { $acc:lit | fac 0 } => { return #{$acc} }
case infix { $acc:lit | fac $n:lit } => {
var acc = #{$acc}, n = #{$n};
var res = acc[0].token.value * n[0].token.value;
var n1 = n[0].token.value - 1;
return withSyntax($res = [makeValue(res,n)],
$n1 = [makeValue(n1,n)]) {
return #{$res fac $n1}
}
@levjj
levjj / sweet-units.sjs
Last active August 29, 2015 13:56
Units of measure with sweet.js
// Some minimal class for values with units
// (nothing exciting going on...)
function Unit(name,val) {
this.name = name;
this.val = val;
}
Unit.prototype.toString = function() {
return this.val + " " + this.name;
}
import Window
tick : Signal Float
tick = (((+) 100) . toFloat . ((flip mod) 100)) <~ count (fps 25)
shape : Float -> Form
shape i = ngon 5 20
|> filled (hsva (i / 16) 0.5 1.0 1.0)
|> rotate (64 / i)
|> scale (i / 10)
(~>) : Signal a -> (a -> b) -> Signal b
(~>) = flip lift
infixl 4 ~>
main : Signal Element
main = (fps 25
|> count)
~> flip mod 100
~> toFloat
~> ngon 3
@levjj
levjj / keybase.md
Created October 6, 2014 01:07
keybase.md

Keybase proof

I hereby claim:

  • I am levjj on github.
  • I am levjj (https://keybase.io/levjj) on keybase.
  • I have a public key whose fingerprint is 3E82 6587 6849 C5F1 C755 C4D0 AEE8 2C3C 2A03 EDFA

To claim this, I am signing this object:

@levjj
levjj / tco.sjs
Created February 12, 2015 03:30
Tail Call Optimization with sweet.js
let function = macro {
rule { // proper tail call
$n ( $x (,) ... ) {
if ($b ... ) return $l:expr ;
return $n ( $y:expr (,) ...) ;
}
} => {
function $n ( $( $x ,) ... $xl) {
var tmp = {};
while (!($b ...)) {