Created
December 16, 2011 21:25
-
-
Save marijnh/1488059 to your computer and use it in GitHub Desktop.
Rust implementation demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
impl util for uint { | |
fn str() -> str { uint::str(self) } | |
fn times(f: block(uint)) { | |
let c = 0u; | |
while c < self { f(c); c += 1u; } | |
} | |
} | |
impl util<T> for [T] { | |
fn len() -> uint { vec::len(self) } | |
fn iter(f: block(T)) { for x in self { f(x); } } | |
fn map<U>(f: block(T) -> U) -> [U] { | |
let r = []; | |
for elt in self { r += [f(elt)]; } | |
r | |
} | |
} | |
fn main() { | |
log_err [1].len().str(); | |
log_err [3, 4].map({|a| a + 4}); | |
10u.times({|n| log_err n;}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment