Skip to content

Instantly share code, notes, and snippets.

View stevenblenkinsop's full-sized avatar

Steven Blenkinsop stevenblenkinsop

View GitHub Profile
@stevenblenkinsop
stevenblenkinsop / gist:7165828
Created October 26, 2013 06:05
Demonstration of ref patterns for avoiding moving out of &
enum List<T> {
End,
Node { val: T, next: ~List<T> },
}
fn print_list<T:ToStr>(list: List<T>) {
match list {
End => { println("<X>"); },
Node{val, next} => {
println(val.to_str());
func Id(x T) T {
return x
}
use std::collections::hash_map::HashMap;
use std::cell::Cell;
#[deriving(Show)]
struct Adder<'a>(&'a Cell<uint>);
impl <'a> Adder<'a> {
fn add(&mut self, i: uint) {
let &Adder(cell) = self;
cell.set(cell.get() + i);
@stevenblenkinsop
stevenblenkinsop / add_types.rs
Last active August 29, 2015 14:10
Type-level add
#[allow(dead_code)]
use std::default::Default;
use std::fmt::{Show, Formatter, Error};
trait Value: Default + Show {}
trait Church: Value {
fn as_int(self) -> int;
}
#![allow(dead_code)]
macro_rules! pipe {
((); $e:expr; $($args:tt)*) => ($e);
(($m:ident | $($ms:ident)|+) ; $($args:tt)*) => ($m!(($($ms)|+) ; $($args)*));
(($m:ident) ; $($args:tt)*) => ($m!((); $($args)*));
($($ms:ident)|*) => (pipe!(($($ms)|*);));
}
macro_rules! incr {
#![allow(dead_code)]
macro_rules! pipe {
((); $e:expr; $($args:tt)*) => ($e);
(($m:ident | $($ms:ident)|+) ; $($args:tt)*) => ($m!(($($ms)|+) ; $($args)*));
(($m:ident) ; $($args:tt)*) => ($m!((); $($args)*));
($($ms:ident)|*) => (pipe!(($($ms)|*);));
}
macro_rules! zero {
@stevenblenkinsop
stevenblenkinsop / playground.rs
Last active August 29, 2015 14:24 — forked from anonymous/playground.rs
Rust monads with binding Fn
use std::ops::Mul;
pub mod hkt {
pub trait HktAt<T> {
type Out: HasHkt<Hkt=Self, Arg=T>;
}
pub trait HasHkt {
type Arg;
type Hkt: HktAt<Self::Arg, Out=Self>;
@stevenblenkinsop
stevenblenkinsop / playground.rs
Last active August 29, 2015 14:24 — forked from anonymous/playground.rs
Rust monads binding FnOnce
#![feature(fnbox)]
use std::ops::Mul;
pub mod hkt {
pub trait HktAt<T> {
type Out: HasHkt<Hkt=Self, Arg=T>;
}
pub trait HasHkt {
macro_rules! pipe {
(($isn:ident | $($rest:ident)|+): $($args:tt)*) => ($isn!(($($rest)|+): $($args)*));
(($isn:ident): $($args:tt)*) => ($isn!((): $($args)*));
((): $result:expr, $($rest:tt)*) => ($result);
}
macro_rules! succ {
($isns:tt: [$($n:expr),*], $e:expr, $($rest:tt)*) => (pipe!($isns: [$($n,)* $e], $e, $($rest)*));
}
type RenderData = u32;
struct Manager {
num_entities: usize,
render_data: Vec<Option<RenderData>>
}
pub fn main() {
let mut entity_manager = Manager {