Skip to content

Instantly share code, notes, and snippets.

View soegaard's full-sized avatar

Jens Axel Søgaard soegaard

View GitHub Profile
// main.swift
// Testing
import Foundation
import Cocoa
import Carbon
import AppKit
println("Hello, World!")
require('source-map-support').install()
function displayln(x) {
console.log(x);
}
/*** CONTROL CONSTRUCTS ***/
/* when expr body
Racket when */
@soegaard
soegaard / even.sjs
Last active August 29, 2015 14:02
The pattern class even
macro even {
function (stx) { /* stx is an array of syntax objects */
console.log(stx);
var name_stx = stx[0];
var arg = stx[1];
var val = arg.token.value;
var res = ( ((val%2)==0) ? [makeValue(true,name_stx)] : false );
if (res) return { result: [stx[1]],
rest: stx.slice(2) }
else return false }}
@soegaard
soegaard / gist:4fc5d05f547ae3c38e36
Created June 21, 2014 22:31
Define functions for phase 1
macro compiletimeutils {
case { $ctu ()}
=> { letstx $is_stx = [makeIdent("is_stx", #{$ctu})];
return #{function $is_stx(s) {
return ((typeof s)=="object") &&
!(s[0] == undefined) &&
((typeof(s[0]["token"]))=="object") } ;} }
}
macro m {
@soegaard
soegaard / rlet.js
Created June 21, 2014 18:56
TypeError: Cannot read property 'type' of undefined
/*** CONTROL CONSTUCTS ***/
/* rif expr expr1 expr2
equivalent to (expr ? expr1 : expr2)
Note: The Javascript if is a "statement-if", so this is the "expression-if" */
macro rif {
case {_ ($e:expr, $e1:expr, $e2:expr)}
=> {return #{(($e) ? ($e1) : ($e2))}}}
(define (cross a b)
(match-define (vector a1 a2 a3) (matrix->vector a))
(match-define (vector b1 b2 b3) (matrix->vector b))
(matrix [[(- (* a2 b3) (* a3 b2))]
[(- (* a3 b1) (* a1 b3))]
[(- (* a1 b2) (* a2 b1))]]))
(define (cross-product a b)
(for/matrix 3 1 ([n 3])
(define (ref c i) (matrix-ref c (modulo (+ n i) 3) 0))
(- (* (ref a 1) (ref b 2)) (* (ref a 2) (ref b 1)))))
(cross-product (col-matrix [1 0 0])
(col-matrix [0 1 0]))
#lang racket
(require math
(prefix-in racket: racket))
(define (+ . xs)
(cond
[(andmap number? xs) (apply racket:+ xs)]
[else (apply matrix+ xs)]))
(+ (matrix [[1 2] [3 4]])
@soegaard
soegaard / gist:2fe511173180d6b106a3
Created June 10, 2014 15:50
Print matrix in Matlab style
#lang racket
(require math)
(define old-printer (array-custom-printer))
(define (matrix-printer A name port mode)
(cond
[(not (matrix? A)) (old-printer A name port mode)]
[else (for ([r (matrix-rows A)])
#lang racket
(require syntax/parse)
(define-syntax-class term
(pattern ((~literal t) t)))
(define-syntax-class operator
(pattern ((~literal o) op)))
(define-splicing-syntax-class term-op