2012-09-19 20:59:44 +00:00
|
|
|
// xfail-fast
|
2012-09-18 22:52:21 +00:00
|
|
|
#[legacy_modes];
|
|
|
|
|
2012-09-05 19:32:05 +00:00
|
|
|
use a::*;
|
2011-12-16 09:42:28 +00:00
|
|
|
|
2012-07-11 22:00:40 +00:00
|
|
|
trait plus {
|
|
|
|
fn plus() -> int;
|
|
|
|
}
|
|
|
|
|
2011-12-16 09:42:28 +00:00
|
|
|
mod a {
|
2012-09-22 01:10:45 +00:00
|
|
|
#[legacy_exports];
|
2012-08-08 01:10:06 +00:00
|
|
|
impl uint: plus { fn plus() -> int { self as int + 20 } }
|
2011-12-16 09:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mod b {
|
2012-09-22 01:10:45 +00:00
|
|
|
#[legacy_exports];
|
2012-08-08 01:10:06 +00:00
|
|
|
impl ~str: plus { fn plus() -> int { 200 } }
|
2012-07-11 22:00:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
trait uint_utils {
|
|
|
|
fn str() -> ~str;
|
|
|
|
fn multi(f: fn(uint));
|
2011-12-16 09:42:28 +00:00
|
|
|
}
|
|
|
|
|
2012-08-08 01:10:06 +00:00
|
|
|
impl uint: uint_utils {
|
2012-07-14 05:57:48 +00:00
|
|
|
fn str() -> ~str { uint::str(self) }
|
A new `times` method on numeric types
This method is intended to elegantly subsume two common iteration functions.
The first is `iter::range`, which is used identically to the method introduced
in this commit, but currently works only on uints. The second is a common case
of `{int, i8, uint, etc.}::range`, in the case where the inductive variable is
ignored. Compare the usage of the three:
```
for iter::range(100u) {
// do whatever
}
for int::range(0, 100) |_i| {
// do whatever
}
for 100.times {
// do whatever
}
```
I feel that the latter reads much more nicely than the first two approaches,
and unlike the first two the new method allows the user to ignore the specific
type of the number (ineed, if we're throwing away the inductive variable, who
cares what type it is?). A minor benefit is that this new method will be
somewhat familiar to users of Ruby, from which we borrow the name "times".
2012-07-06 02:12:26 +00:00
|
|
|
fn multi(f: fn(uint)) {
|
2012-03-22 15:39:41 +00:00
|
|
|
let mut c = 0u;
|
2011-12-18 18:30:40 +00:00
|
|
|
while c < self { f(c); c += 1u; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-11 22:00:40 +00:00
|
|
|
trait vec_utils<T> {
|
|
|
|
fn length_() -> uint;
|
|
|
|
fn iter_(f: fn(T));
|
2012-09-07 21:52:28 +00:00
|
|
|
fn map_<U: Copy>(f: fn(T) -> U) -> ~[U];
|
2012-07-11 22:00:40 +00:00
|
|
|
}
|
|
|
|
|
2012-08-08 01:10:06 +00:00
|
|
|
impl<T> ~[T]: vec_utils<T> {
|
2012-03-18 01:02:45 +00:00
|
|
|
fn length_() -> uint { vec::len(self) }
|
2012-09-19 23:55:01 +00:00
|
|
|
fn iter_(f: fn(T)) { for self.each |x| { f(*x); } }
|
2012-09-07 21:52:28 +00:00
|
|
|
fn map_<U: Copy>(f: fn(T) -> U) -> ~[U] {
|
2012-06-29 23:26:56 +00:00
|
|
|
let mut r = ~[];
|
2012-09-19 23:55:01 +00:00
|
|
|
for self.each |elt| { r += ~[f(*elt)]; }
|
2011-12-18 18:30:40 +00:00
|
|
|
r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-16 09:42:28 +00:00
|
|
|
fn main() {
|
|
|
|
assert 10u.plus() == 30;
|
2012-07-14 05:57:48 +00:00
|
|
|
assert (~"hi").plus() == 200;
|
2011-12-16 09:42:28 +00:00
|
|
|
|
2012-07-14 05:57:48 +00:00
|
|
|
assert (~[1]).length_().str() == ~"1";
|
2012-06-30 23:19:07 +00:00
|
|
|
assert (~[3, 4]).map_(|a| a + 4 )[0] == 7;
|
|
|
|
assert (~[3, 4]).map_::<uint>(|a| a as uint + 4u )[0] == 7u;
|
2012-03-22 15:39:41 +00:00
|
|
|
let mut x = 0u;
|
A new `times` method on numeric types
This method is intended to elegantly subsume two common iteration functions.
The first is `iter::range`, which is used identically to the method introduced
in this commit, but currently works only on uints. The second is a common case
of `{int, i8, uint, etc.}::range`, in the case where the inductive variable is
ignored. Compare the usage of the three:
```
for iter::range(100u) {
// do whatever
}
for int::range(0, 100) |_i| {
// do whatever
}
for 100.times {
// do whatever
}
```
I feel that the latter reads much more nicely than the first two approaches,
and unlike the first two the new method allows the user to ignore the specific
type of the number (ineed, if we're throwing away the inductive variable, who
cares what type it is?). A minor benefit is that this new method will be
somewhat familiar to users of Ruby, from which we borrow the name "times".
2012-07-06 02:12:26 +00:00
|
|
|
10u.multi(|_n| x += 2u );
|
2011-12-18 18:30:40 +00:00
|
|
|
assert x == 20u;
|
|
|
|
}
|