Skip to content

Instantly share code, notes, and snippets.

View bstrie's full-sized avatar

bstrie bstrie

View GitHub Profile
#![feature(macro_rules)]
macro_rules! foo {
($head:expr, $($tail:expr),+) => ({
println!("{}", $head);
foo!($($tail),+);
});
($last:expr) => ({
println!("{}", $last);
});
19:45 < Guest4321> @kmc: Assuming Rust has HKT, why not just define a Functor as a higher-kinded struct and define a generic impl on
it? Why do we need traits?
19:45 < Guest4321> had*
19:46 < frozendevil!Adium@moz-62FB197F.com [Quit: Leaving.]
19:46 < mcpherrin> kmc: https://github.com/mozilla/rust/issues/6393
19:46 <@cmr> Guest4321: functor describes behavior of an interface, not a single concrete thing.
19:46 <@kmc> Guest4321: because the implementation of fmap varies according to the type inside
19:46 <@cmr> that'd be like making a monoid struct
19:46 <@cmr> what would the point be
19:47 <@kmc> it's not parametric polymorphism
use Foo::Bar;
mod Foo {
pub struct Bar {
pub x: int,
y: int
}
impl Bar {
pub fn new(new_x: int, new_y: int) -> Bar {
struct Foo<T> {
x: i16,
y: T
}
fn bar<T>(foo: Foo<T>) -> i16 {
foo.x
}
fn main() {
use std::iter::{Iterator, FromIterator};
fn map<T, IT: Iterator<T>, U, IU: FromIterator<U>>(fun: |T|->U, coll: IT) -> IU {
let mut acc = Vec::new();
for elt in coll.move_iter() { // error: type `IT` does not implement any method in scope named `move_iter`
acc.push(fun(elt));
}
acc.move_iter().collect()
}
fn map<T, U>(fun: |T|->U, coll: ~[T]) -> ~[U] {
let mut acc = Vec::new();
for elt in coll.move_iter() {
acc.push(fun(elt));
}
acc.move_iter().collect()
}
fn main() {
let x = map(|x| x+2, ~[1u,2,3]);
fn foo<T, U>(x: T) -> U {
x // error: mismatched types: expected `U` but found `T` (expected type parameter but found type parameter)
}
fn main() {
foo(1u);
}
repr.rs:5:5: 5:15 error: discriminant value outside specified type
repr.rs:5 Qux = 5000
^~~~~~~~~~
repr.rs:1:8: 1:10 note: discriminant type specified here
repr.rs:1 #[repr(i8)]
^~
use std::f32::consts::PI;
use std::io::{BufferedWriter, stdout};
use std::rand::{Rng, StdRng};
struct Vec2 {
x: f32,
y: f32,
}
fn lerp(a: f32, b: f32, v: f32) -> f32 { a * (1.0 - v) + b * v }
@bstrie
bstrie / um.rs
Created February 13, 2014 21:44
fn main() {
let mut pixels = [0f32, ..8];
for &mut pixel in pixels.mut_iter() {
pixel = 2.0;
}
println!("{:?}", pixels); // [0f32, 0f32, 0f32, 0f32, 0f32, 0f32, 0f32, 0f32]
}