Skip to content

Instantly share code, notes, and snippets.

View dbp's full-sized avatar

Daniel Patterson dbp

View GitHub Profile
@dbp
dbp / reflect.rs
Created September 1, 2012 02:36
data visitor
import intrinsic::{tydesc, get_tydesc, visit_tydesc, ty_visitor};
import libc::c_void;
trait MovablePtr {
fn move_ptr(adjustment: fn(*c_void) -> *c_void);
}
// FIXME #3262: this is a near-duplicate of code in core::vec.
type VecRepr = {
box_header: (uint, uint, uint, uint),
@dbp
dbp / fmt.rs
Created September 14, 2012 03:17
fmt datavisitor
extern mod std; // for tests
use reflect::*;
enum fmt_visitor = @{
out: io::WriterUtil,
};
// duplicated from syntax/ast.rs so as not to depend on it
enum mutability { m_mutbl, m_imm, m_const, }
@dbp
dbp / copy_type.rs
Created October 7, 2012 21:55
instantiating copy type warning
fn main() {
let x = (~"a",~"b");
io::println(x.second());
}
@dbp
dbp / gist:3856358
Created October 9, 2012 03:12
purity
struct C {mut d: ~[uint]}
fn main() {
let mut c = C {d: ~[1,2,3]};
f(&c);
}
fn f(x: &C) {
for x.d.each |e| {
io::println(~"a");
@dbp
dbp / gist:3861243
Created October 9, 2012 20:34
announce rustle
I'm announcing an initial version of an API search tool for Rust. It is inspired by the api search tool for haskell
called Hoogle (http://www.haskell.org/hoogle). It is very new (I started working on it a week ago), so it has a long
way to go, but for a taste of the kind of things it can currently do:
rustle> str -> uint
core::str::capacity - fn capacity(s: & const ~str) -> uint - Returns the number of single-byte characters the
string can hold without reallocating
core::str::char_len - fn char_len(s: & str) -> uint - Returns the number of characters that a string holds
core::str::len - fn len(s: & str) -> uint - Returns the string length/size in bytes not counting the null terminator
@dbp
dbp / gist:3887084
Created October 14, 2012 02:50
impl eq
struct A {n: uint}
impl A: cmp::Eq {
pure fn eq(other: &A) -> bool {
self.n == other.n
}
pure fn ne(other: &A) -> bool {
!self.eq(other)
}
}
@dbp
dbp / cps.rkt
Created January 3, 2013 18:14
Python CPS core example
#lang plai-typed
(require "python-micro-syntax.rkt"
"python-helpers.rkt")
(define (run-cps [e : UExp])
(begin
(sym-reset)
(UApp (cps e) (UFn '^x (UId '^x)))))
@dbp
dbp / desugar-control.rkt
Created January 3, 2013 18:29
Desugaring control flow to letCC
;; Control flow
;; basically, we provide a shadowed '^throw continuation, and then see what
;; we are handed. if it is an error (ie, an exception), we see if it matches
;; what we are catching, and if it doesn't, we re-throw. Since we are out of
;; scope of the current try/catch, we will now pick up whatever exception
;; continuation is outside. note that this obviously requires the entire program
;; to be wrapped in a try/catch that matches on everything (which it is).
[CTry
(bdy mat cat els)
@dbp
dbp / gist:6246396
Created August 16, 2013 01:11
simple discrete scrolling
$(function () {
for(var i = 0; i < 10; i++) {
$("#active").append($("<div class='elem'>Block " + i + "</div>"));
}
$("#pointer").bind("mousedown", start);
});
function start(event) {
window.startpos = parseInt($("#pointer").css("top"));
$(document).bind("mousemove", drag);