Skip to content

Commit e204aa2

Browse files
committed
Merge branch 'master' into register-snapshots
2 parents 11ecec1 + e8a5d81 commit e204aa2

15 files changed

+1018
-544
lines changed

src/libcore/core.rc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ This behavior can be disabled with the `no_core` crate attribute."
3030

3131
export box, char, float, bessel, f32, f64, int, str, ptr;
3232
export uint, u8, u32, u64, vec, bool;
33-
export either, option, result;
33+
export either, option, result, iter;
3434
export ctypes, sys, unsafe, comm, task, logging;
3535
export extfmt;
3636
export math;
@@ -64,7 +64,7 @@ mod either;
6464
mod option;
6565
mod result;
6666
mod tuple;
67-
67+
mod iter;
6868

6969
// Runtime and language-primitive support
7070

src/libcore/iter.rs

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
iface iterable<A> {
2+
fn iter(blk: fn(A));
3+
}
4+
5+
impl<A> of iterable<A> for fn@(fn(A)) {
6+
fn iter(blk: fn(A)) {
7+
self(blk);
8+
}
9+
}
10+
11+
// accomodate the fact that int/uint are passed by value by default:
12+
impl of iterable<int> for fn@(fn(int)) {
13+
fn iter(blk: fn(&&int)) {
14+
self {|i| blk(i)}
15+
}
16+
}
17+
18+
impl of iterable<uint> for fn@(fn(uint)) {
19+
fn iter(blk: fn(&&uint)) {
20+
self {|i| blk(i)}
21+
}
22+
}
23+
24+
impl<A> of iterable<A> for [A] {
25+
fn iter(blk: fn(A)) {
26+
vec::iter(self, blk)
27+
}
28+
}
29+
30+
impl<A> of iterable<A> for option<A> {
31+
fn iter(blk: fn(A)) {
32+
option::may(self, blk)
33+
}
34+
}
35+
36+
fn enumerate<A,IA:iterable<A>>(self: IA, blk: fn(uint, A)) {
37+
let i = 0u;
38+
self.iter {|a|
39+
blk(i, a);
40+
i += 1u;
41+
}
42+
}
43+
44+
// Here: we have to use fn@ for predicates and map functions, because
45+
// we will be binding them up into a closure. Disappointing. A true
46+
// region type system might be able to do better than this.
47+
48+
fn filter<A,IA:iterable<A>>(self: IA, prd: fn@(A) -> bool, blk: fn(A)) {
49+
self.iter {|a|
50+
if prd(a) { blk(a) }
51+
}
52+
}
53+
54+
fn map<A,B,IA:iterable<A>>(self: IA, cnv: fn@(A) -> B, blk: fn(B)) {
55+
self.iter {|a|
56+
let b = cnv(a);
57+
blk(b);
58+
}
59+
}
60+
61+
fn flat_map<A,B,IA:iterable<A>,IB:iterable<B>>(
62+
self: IA, cnv: fn@(A) -> IB, blk: fn(B)) {
63+
self.iter {|a|
64+
cnv(a).iter(blk)
65+
}
66+
}
67+
68+
fn foldl<A,B:copy,IA:iterable<A>>(self: IA, b0: B, blk: fn(B, A) -> B) -> B {
69+
let b = b0;
70+
self.iter {|a|
71+
b = blk(b, a);
72+
}
73+
ret b;
74+
}
75+
76+
fn to_list<A:copy,IA:iterable<A>>(self: IA) -> [A] {
77+
foldl::<A,[A],IA>(self, [], {|r, a| r + [a]})
78+
}
79+
80+
fn repeat(times: uint, blk: fn()) {
81+
let i = 0u;
82+
while i < times {
83+
blk();
84+
i += 1u;
85+
}
86+
}
87+
88+
89+
#[test]
90+
fn test_enumerate() {
91+
enumerate(["0", "1", "2"]) {|i,j|
92+
assert #fmt["%u",i] == j;
93+
}
94+
}
95+
96+
#[test]
97+
fn test_map_and_to_list() {
98+
let a = bind vec::iter([0, 1, 2], _);
99+
let b = bind map(a, {|i| i*2}, _);
100+
let c = to_list(b);
101+
assert c == [0, 2, 4];
102+
}
103+
104+
#[test]
105+
fn test_map_directly_on_vec() {
106+
let b = bind map([0, 1, 2], {|i| i*2}, _);
107+
let c = to_list(b);
108+
assert c == [0, 2, 4];
109+
}
110+
111+
#[test]
112+
fn test_filter_on_int_range() {
113+
fn is_even(&&i: int) -> bool {
114+
ret (i % 2) == 0;
115+
}
116+
117+
let l = to_list(bind filter(bind int::range(0, 10, _), is_even, _));
118+
assert l == [0, 2, 4, 6, 8];
119+
}
120+
121+
#[test]
122+
fn test_filter_on_uint_range() {
123+
fn is_even(&&i: uint) -> bool {
124+
ret (i % 2u) == 0u;
125+
}
126+
127+
let l = to_list(bind filter(bind uint::range(0u, 10u, _), is_even, _));
128+
assert l == [0u, 2u, 4u, 6u, 8u];
129+
}
130+
131+
#[test]
132+
fn test_flat_map_with_option() {
133+
fn if_even(&&i: int) -> option<int> {
134+
if (i % 2) == 0 { some(i) }
135+
else { none }
136+
}
137+
138+
let a = bind vec::iter([0, 1, 2], _);
139+
let b = bind flat_map(a, if_even, _);
140+
let c = to_list(b);
141+
assert c == [0, 2];
142+
}
143+
144+
#[test]
145+
fn test_flat_map_with_list() {
146+
fn repeat(&&i: int) -> [int] {
147+
let r = [];
148+
int::range(0, i) {|_j| r += [i]; }
149+
r
150+
}
151+
152+
let a = bind vec::iter([0, 1, 2, 3], _);
153+
let b = bind flat_map(a, repeat, _);
154+
let c = to_list(b);
155+
#debug["c = %?", c];
156+
assert c == [1, 2, 2, 3, 3, 3];
157+
}
158+
159+
#[test]
160+
fn test_repeat() {
161+
let c = [],
162+
i = 0u;
163+
repeat(5u) {||
164+
c += [(i * i)];
165+
i += 1u;
166+
};
167+
#debug["c = %?", c];
168+
assert c == [0u, 1u, 4u, 9u, 16u];
169+
}
170+
171+

src/rustdoc/attr_parser.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ import core::tuple;
1111

1212
export crate_attrs, mod_attrs, fn_attrs, arg_attrs,
1313
const_attrs, enum_attrs, variant_attrs, res_attrs,
14-
iface_attrs, method_attrs;
14+
iface_attrs, method_attrs, impl_attrs;
1515
export parse_crate, parse_mod, parse_fn, parse_const,
1616
parse_enum, parse_variant, parse_res,
17-
parse_iface, parse_method;
17+
parse_iface, parse_method, parse_impl;
1818

1919
type crate_attrs = {
2020
name: option<str>
@@ -63,6 +63,11 @@ type iface_attrs = {
6363
desc: option<str>
6464
};
6565

66+
type impl_attrs = {
67+
brief: option<str>,
68+
desc: option<str>
69+
};
70+
6671
type method_attrs = fn_attrs;
6772

6873
#[cfg(test)]
@@ -499,3 +504,7 @@ fn parse_iface(attrs: [ast::attribute]) -> iface_attrs {
499504
fn parse_method(attrs: [ast::attribute]) -> method_attrs {
500505
parse_fn(attrs)
501506
}
507+
508+
fn parse_impl(attrs: [ast::attribute]) -> impl_attrs {
509+
parse_basic(attrs)
510+
}

0 commit comments

Comments
 (0)