2012-09-18 17:52:21 -05:00
|
|
|
#[legacy_modes];
|
|
|
|
|
2012-09-05 14:32:05 -05:00
|
|
|
use a::*;
|
2011-12-16 03:42:28 -06:00
|
|
|
|
2012-07-11 17:00:40 -05:00
|
|
|
trait plus {
|
|
|
|
fn plus() -> int;
|
|
|
|
}
|
|
|
|
|
2011-12-16 03:42:28 -06:00
|
|
|
mod a {
|
2012-08-07 20:10:06 -05:00
|
|
|
impl uint: plus { fn plus() -> int { self as int + 20 } }
|
2011-12-16 03:42:28 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
mod b {
|
2012-08-07 20:10:06 -05:00
|
|
|
impl ~str: plus { fn plus() -> int { 200 } }
|
2012-07-11 17:00:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
trait uint_utils {
|
|
|
|
fn str() -> ~str;
|
|
|
|
fn multi(f: fn(uint));
|
2011-12-16 03:42:28 -06:00
|
|
|
}
|
|
|
|
|
2012-08-07 20:10:06 -05:00
|
|
|
impl uint: uint_utils {
|
2012-07-14 00:57:48 -05: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-05 21:12:26 -05:00
|
|
|
fn multi(f: fn(uint)) {
|
2012-03-22 10:39:41 -05:00
|
|
|
let mut c = 0u;
|
2011-12-18 12:30:40 -06:00
|
|
|
while c < self { f(c); c += 1u; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-11 17:00:40 -05:00
|
|
|
trait vec_utils<T> {
|
|
|
|
fn length_() -> uint;
|
|
|
|
fn iter_(f: fn(T));
|
2012-09-07 16:52:28 -05:00
|
|
|
fn map_<U: Copy>(f: fn(T) -> U) -> ~[U];
|
2012-07-11 17:00:40 -05:00
|
|
|
}
|
|
|
|
|
2012-08-07 20:10:06 -05:00
|
|
|
impl<T> ~[T]: vec_utils<T> {
|
2012-03-17 20:02:45 -05:00
|
|
|
fn length_() -> uint { vec::len(self) }
|
2012-06-30 18:19:07 -05:00
|
|
|
fn iter_(f: fn(T)) { for self.each |x| { f(x); } }
|
2012-09-07 16:52:28 -05:00
|
|
|
fn map_<U: Copy>(f: fn(T) -> U) -> ~[U] {
|
2012-06-29 18:26:56 -05:00
|
|
|
let mut r = ~[];
|
2012-06-30 18:19:07 -05:00
|
|
|
for self.each |elt| { r += ~[f(elt)]; }
|
2011-12-18 12:30:40 -06:00
|
|
|
r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-16 03:42:28 -06:00
|
|
|
fn main() {
|
|
|
|
assert 10u.plus() == 30;
|
2012-07-14 00:57:48 -05:00
|
|
|
assert (~"hi").plus() == 200;
|
2011-12-16 03:42:28 -06:00
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
assert (~[1]).length_().str() == ~"1";
|
2012-06-30 18:19:07 -05: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 10:39:41 -05: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-05 21:12:26 -05:00
|
|
|
10u.multi(|_n| x += 2u );
|
2011-12-18 12:30:40 -06:00
|
|
|
assert x == 20u;
|
|
|
|
}
|