Skip to content

Instantly share code, notes, and snippets.

@jbclements
jbclements / parses-slowly.rs
Created May 2, 2013 22:08
ANTLR parses this incredibly slowly
use a::b::c;
pub fn mk_pass(name: ~str, op: @fn(&str) -> ~str) -> Pass {
let op = Cell(op);
Pass {
name: copy name,
f: |srv: astsrv::Srv, doc: doc::Doc| -> doc::Doc {
run(srv, doc, op.take())
}
}
@jbclements
jbclements / funny-pattern.rs
Created April 18, 2013 16:06
a rust file with an unusual pattern identifier
#[test] fn main() {
let z = match 3 {
x() => x
};
assert_eq!(z,3)
}
@jbclements
jbclements / blur-linkage.rkt
Created March 30, 2013 00:40
Linking to the rust library in racket.
;; link to the rust library:
(define rust-lib (ffi-lib (build-path here "libblur-68a2c114141ca-0.0")))
(define rust-blur-fun (get-ffi-obj "blur" rust-lib (_fun _uint _uint _cvector -> _void)))
(define (rust-blur width height data)
(define cvec (list->cvector data _byte))
(rust-blur-fun width height cvec)
(cvector->list cvec))
@jbclements
jbclements / blur-linkage.rs
Created March 30, 2013 00:37
The rust side of the linkage.
#[no_mangle]
pub extern fn blur(width: c_uint, height: c_uint, data: *mut u8) {
let width = width as uint;
let height = height as uint;
unsafe {
do vec::raw::mut_buf_as_slice(data, width * height) |data| {
let out_data = blur_rust(width, height, data);
vec::raw::copy_memory(data, out_data, width * height);
}
@jbclements
jbclements / blur.rs
Created March 30, 2013 00:34
Blur code in Rust. Written by Brian Anderson.
fn blur_rust(width: uint, height: uint, data: &[u8]) -> ~[u8] {
let filter = [[0.011, 0.084, 0.011],
[0.084, 0.620, 0.084],
[0.011, 0.084, 0.011]];
let mut newdata = ~[];
for uint::range(0, height) |y| {
for uint::range(0, width) |x| {
@jbclements
jbclements / blur.rkt
Created March 30, 2013 00:32
The 'blur' function for a rust-in-racket demo
;; the gaussian filter used in the racket blur.
;; boosted center value by 1/1000 to make sure that whites stay white.
(define filter '[[0.011 0.084 0.011]
[0.084 0.620 0.084]
[0.011 0.084 0.011]])
;; racket-blur: blur the image using the gaussian filter
;; number number list-of-bytes -> vector-of-bytes
(define (racket-blur width height data)
(define data-vec (list->vector data))
@jbclements
jbclements / gist:5158000
Last active December 14, 2015 22:19
string matching with number conversion
#lang racket
(define lines
(list "hootehutt 23 hoethoet 4"
"abc 9 thuoteh 9"))
(for/list ([l lines])
(match-define (list name age lastname donuts) (string-split l))
(list name (string->number age) lastname (string->number donuts)))
#lang racket
(define lines
(list "hootehutt 23 hoethoet 4"
"abc 9 thuoteh 9"))
(define (maybe-numify s)
(match (regexp-match #px"[0-9]+" s)
[#f s]
[else (string->number s)]))
@jbclements
jbclements / foo.rkt
Created March 6, 2013 00:14
updated file with #lang racket and quoted ()s.
#lang racket
;; Rafael George Homework 5
;; definition of structures for MUPL programs
(define-struct var (string)) ;; a variable, e.g., (make-var "foo")
(define-struct int (num)) ;; a constant number, e.g., (make-int 17)
(define-struct add (e1 e2)) ;; add two expressions
(define-struct ifgreater (e1 e2 e3 e4));;if e1 > e2 then e3 else e4
(define-struct fun (nameopt formal body)) ;; a recursive(?) 1-argument function
@jbclements
jbclements / captured-mutable.rs
Created February 20, 2013 21:01
This file displays the "mutable variables cannot be implicitly captured" error
pub struct Bogo {
x: fn@() -> uint
}
pub fn bugfun2(_exts: @mut uint)
-> uint { 3 }
pub fn bugfun() {
let mut exts = 146;
let _f_pre = @Bogo {