Skip to content

Commit 8a97660

Browse files
committed
Change a bunch of places in the stdlib to use blocks.
1 parent de4b383 commit 8a97660

File tree

7 files changed

+42
-48
lines changed

7 files changed

+42
-48
lines changed

src/lib/bitv.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ fn create(nbits: uint, init: bool) -> t {
3636
ret @{storage: storage, nbits: nbits};
3737
}
3838

39-
fn process(op: &fn(uint, uint) -> uint , v0: &t, v1: &t) -> bool {
39+
fn process(op: &block(uint, uint) -> uint , v0: &t, v1: &t) -> bool {
4040
let len = ivec::len(v1.storage);
4141
assert (ivec::len(v0.storage) == len);
4242
assert (v0.nbits == v1.nbits);

src/lib/either.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,8 @@ import option::none;
55

66
tag t[T, U] { left(T); right(U); }
77

8-
type operator[T, U] = fn(&T) -> U ;
9-
10-
fn either[T, U,
11-
V](f_left: &operator[T, V], f_right: &operator[U, V],
12-
value: &t[T, U]) -> V {
8+
fn either[T, U, V](f_left: &block(&T) -> V, f_right: &block(&U) -> V,
9+
value: &t[T, U]) -> V {
1310
alt value { left(l) { f_left(l) } right(r) { f_right(r) } }
1411
}
1512

src/lib/ivec.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import option::some;
55
import uint::next_power_of_two;
66
import ptr::addr_of;
77

8-
type operator2[T, U, V] = fn(&T, &U) -> V ;
9-
108
native "rust-intrinsic" mod rusti {
119
fn ivec_len[T](v: &[T]) -> uint;
1210
}
@@ -192,7 +190,7 @@ fn grow_set[@T](v: &mutable [mutable T], index: uint, initval: &T, val: &T) {
192190

193191
// Functional utilities
194192

195-
fn map[@T, @U](f: fn(&T) -> U , v: &[mutable? T]) -> [U] {
193+
fn map[@T, @U](f: &block(&T) -> U , v: &[mutable? T]) -> [U] {
196194
let result = ~[];
197195
reserve(result, len(v));
198196
for elem: T in v {
@@ -202,7 +200,8 @@ fn map[@T, @U](f: fn(&T) -> U , v: &[mutable? T]) -> [U] {
202200
ret result;
203201
}
204202

205-
fn filter_map[@T, @U](f: fn(&T) -> option::t[U] , v: &[mutable? T]) -> [U] {
203+
fn filter_map[@T, @U](f: &block(&T) -> option::t[U],
204+
v: &[mutable? T]) -> [U] {
206205
let result = ~[];
207206
for elem: T in v {
208207
let elem2 = elem; // satisfies alias checker
@@ -214,20 +213,20 @@ fn filter_map[@T, @U](f: fn(&T) -> option::t[U] , v: &[mutable? T]) -> [U] {
214213
ret result;
215214
}
216215

217-
fn foldl[@T, @U](p: fn(&U, &T) -> U , z: &U, v: &[mutable? T]) -> U {
216+
fn foldl[@T, @U](p: &block(&U, &T) -> U , z: &U, v: &[mutable? T]) -> U {
218217
let sz = len(v);
219218
if sz == 0u { ret z; }
220219
let first = v.(0);
221220
let rest = slice(v, 1u, sz);
222-
ret p(foldl[T, U](p, z, rest), first);
221+
ret p(foldl(p, z, rest), first);
223222
}
224223

225-
fn any[T](f: fn(&T) -> bool , v: &[T]) -> bool {
224+
fn any[T](f: &block(&T) -> bool , v: &[T]) -> bool {
226225
for elem: T in v { if f(elem) { ret true; } }
227226
ret false;
228227
}
229228

230-
fn all[T](f: fn(&T) -> bool , v: &[T]) -> bool {
229+
fn all[T](f: &block(&T) -> bool , v: &[T]) -> bool {
231230
for elem: T in v { if !f(elem) { ret false; } }
232231
ret true;
233232
}
@@ -243,9 +242,9 @@ fn count[T](x: &T, v: &[mutable? T]) -> uint {
243242
ret cnt;
244243
}
245244

246-
fn find[@T](f: fn(&T) -> bool , v: &[T]) -> option::t[T] {
247-
for elt: T in v { if f(elt) { ret some[T](elt); } }
248-
ret none[T];
245+
fn find[@T](f: &block(&T) -> bool , v: &[T]) -> option::t[T] {
246+
for elt: T in v { if f(elt) { ret some(elt); } }
247+
ret none;
249248
}
250249

251250
fn unzip[@T, @U](v: &[{_0: T, _1: U}]) -> {_0: [T], _1: [U]} {

src/lib/list.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ fn from_vec[@T](v: vec[T]) -> list[T] {
1313
ret l;
1414
}
1515

16-
fn foldl[@T, @U](ls_: &list[T], u: &U, f: fn(&T, &U) -> U ) -> U {
16+
fn foldl[@T, @U](ls_: &list[T], u: &U, f: &block(&T, &U) -> U ) -> U {
1717
let accum: U = u;
1818
let ls = ls_;
1919
while true {
@@ -25,7 +25,8 @@ fn foldl[@T, @U](ls_: &list[T], u: &U, f: fn(&T, &U) -> U ) -> U {
2525
ret accum;
2626
}
2727

28-
fn find[@T, @U](ls_: &list[T], f: fn(&T) -> option::t[U] ) -> option::t[U] {
28+
fn find[@T, @U](ls_: &list[T], f: &block(&T) -> option::t[U])
29+
-> option::t[U] {
2930
let ls = ls_;
3031
while true {
3132
alt ls {

src/lib/option.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@
22

33
tag t[@T] { none; some(T); }
44

5-
type operator[@T, @U] = fn(&T) -> U ;
6-
75
fn get[@T](opt: &t[T]) -> T {
86
alt opt {
97
some(x) { x }
108
none. { fail "option none" }
119
}
1210
}
1311

14-
fn map[@T, @U](f: &operator[T, U], opt: &t[T]) -> t[U] {
12+
fn map[@T, @U](f: &block(&T) -> U, opt: &t[T]) -> t[U] {
1513
alt opt { some(x) { some(f(x)) } none. { none } }
1614
}
1715

@@ -25,12 +23,12 @@ fn from_maybe[@T](def: &T, opt: &t[T]) -> T {
2523
alt opt { some(x) { x } none. { def } }
2624
}
2725

28-
fn maybe[@T, @U](def: &U, f: fn(&T) -> U , opt: &t[T]) -> U {
26+
fn maybe[@T, @U](def: &U, f: &block(&T) -> U, opt: &t[T]) -> U {
2927
alt opt { none. { def } some(t) { f(t) } }
3028
}
3129

3230
// Can be defined in terms of the above when/if we have const bind.
33-
fn may[@T](f: fn(&T) , opt: &t[T]) {
31+
fn may[@T](f: &block(&T), opt: &t[T]) {
3432
alt opt { none. {/* nothing */ } some(t) { f(t); } }
3533
}
3634

src/lib/sort.rs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ export merge_sort;
99
export quick_sort;
1010
export quick_sort3;
1111

12-
type lteq[T] = fn(&T, &T) -> bool ;
12+
type lteq[T] = block(&T, &T) -> bool ;
1313

14-
fn merge_sort[@T](le: lteq[T], v: vec[T]) -> vec[T] {
15-
fn merge[@T](le: lteq[T], a: vec[T], b: vec[T]) -> vec[T] {
14+
fn merge_sort[@T](le: &lteq[T], v: vec[T]) -> vec[T] {
15+
fn merge[@T](le: &lteq[T], a: vec[T], b: vec[T]) -> vec[T] {
1616
let rs: vec[T] = [];
1717
let a_len: uint = len[T](a);
1818
let a_ix: uint = 0u;
@@ -42,8 +42,8 @@ fn swap[@T](arr: vec[mutable T], x: uint, y: uint) {
4242
arr.(y) = a;
4343
}
4444

45-
fn part[@T](compare_func: lteq[T], arr: vec[mutable T], left: uint,
46-
right: uint, pivot: uint) -> uint {
45+
fn part[@T](compare_func: &lteq[T], arr: vec[mutable T], left: uint,
46+
right: uint, pivot: uint) -> uint {
4747
let pivot_value = arr.(pivot);
4848
swap[T](arr, pivot, right);
4949
let storage_index: uint = left;
@@ -59,8 +59,8 @@ fn part[@T](compare_func: lteq[T], arr: vec[mutable T], left: uint,
5959
ret storage_index;
6060
}
6161

62-
fn qsort[@T](compare_func: lteq[T], arr: vec[mutable T], left: uint,
63-
right: uint) {
62+
fn qsort[@T](compare_func: &lteq[T], arr: vec[mutable T], left: uint,
63+
right: uint) {
6464
if right > left {
6565
let pivot = (left + right) / 2u;
6666
let new_pivot = part[T](compare_func, arr, left, right, pivot);
@@ -72,7 +72,7 @@ fn qsort[@T](compare_func: lteq[T], arr: vec[mutable T], left: uint,
7272
}
7373
}
7474

75-
fn quick_sort[@T](compare_func: lteq[T], arr: vec[mutable T]) {
75+
fn quick_sort[@T](compare_func: &lteq[T], arr: vec[mutable T]) {
7676
if len[T](arr) == 0u { ret; }
7777
qsort[T](compare_func, arr, 0u, len[T](arr) - 1u);
7878
}
@@ -82,7 +82,7 @@ fn quick_sort[@T](compare_func: lteq[T], arr: vec[mutable T]) {
8282
// http://www.cs.princeton.edu/~rs/talks/QuicksortIsOptimal.pdf
8383
// According to these slides this is the algorithm of choice for
8484
// 'randomly ordered keys, abstract compare' & 'small number of key values'
85-
fn qsort3[@T](compare_func_lt: lteq[T], compare_func_eq: lteq[T],
85+
fn qsort3[@T](compare_func_lt: &lteq[T], compare_func_eq: &lteq[T],
8686
arr: vec[mutable T], left: int, right: int) {
8787
if right <= left { ret; }
8888
let v: T = arr.(right);
@@ -130,7 +130,7 @@ fn qsort3[@T](compare_func_lt: lteq[T], compare_func_eq: lteq[T],
130130
qsort3[T](compare_func_lt, compare_func_eq, arr, i, right);
131131
}
132132

133-
fn quick_sort3[@T](compare_func_lt: lteq[T], compare_func_eq: lteq[T],
133+
fn quick_sort3[@T](compare_func_lt: &lteq[T], compare_func_eq: &lteq[T],
134134
arr: vec[mutable T]) {
135135
if vec::len[T](arr) == 0u { ret; }
136136
qsort3[T](compare_func_lt, compare_func_eq, arr, 0,
@@ -144,8 +144,8 @@ mod ivector {
144144

145145
type lteq[T] = fn(&T, &T) -> bool ;
146146

147-
fn merge_sort[@T](le: lteq[T], v: &[T]) -> [T] {
148-
fn merge[@T](le: lteq[T], a: &[T], b: &[T]) -> [T] {
147+
fn merge_sort[@T](le: &lteq[T], v: &[T]) -> [T] {
148+
fn merge[@T](le: &lteq[T], a: &[T], b: &[T]) -> [T] {
149149
let rs: [T] = ~[];
150150
let a_len: uint = ilen[T](a);
151151
let a_ix: uint = 0u;
@@ -175,8 +175,8 @@ mod ivector {
175175
arr.(y) = a;
176176
}
177177

178-
fn part[@T](compare_func: lteq[T], arr: &[mutable T], left: uint,
179-
right: uint, pivot: uint) -> uint {
178+
fn part[@T](compare_func: &lteq[T], arr: &[mutable T], left: uint,
179+
right: uint, pivot: uint) -> uint {
180180
let pivot_value = arr.(pivot);
181181
swap[T](arr, pivot, right);
182182
let storage_index: uint = left;
@@ -192,8 +192,8 @@ mod ivector {
192192
ret storage_index;
193193
}
194194

195-
fn qsort[@T](compare_func: lteq[T], arr: &[mutable T], left: uint,
196-
right: uint) {
195+
fn qsort[@T](compare_func: &lteq[T], arr: &[mutable T], left: uint,
196+
right: uint) {
197197
if right > left {
198198
let pivot = (left + right) / 2u;
199199
let new_pivot = part[T](compare_func, arr, left, right, pivot);
@@ -205,7 +205,7 @@ mod ivector {
205205
}
206206
}
207207

208-
fn quick_sort[@T](compare_func: lteq[T], arr: &[mutable T]) {
208+
fn quick_sort[@T](compare_func: &lteq[T], arr: &[mutable T]) {
209209
if ilen[T](arr) == 0u { ret; }
210210
qsort[T](compare_func, arr, 0u, ilen[T](arr) - 1u);
211211
}
@@ -216,8 +216,8 @@ mod ivector {
216216
// According to these slides this is the algorithm of choice for
217217
// 'randomly ordered keys, abstract compare' & 'small number of key
218218
// values'
219-
fn qsort3[@T](compare_func_lt: lteq[T], compare_func_eq: lteq[T],
220-
arr: &[mutable T], left: int, right: int) {
219+
fn qsort3[@T](compare_func_lt: &lteq[T], compare_func_eq: &lteq[T],
220+
arr: &[mutable T], left: int, right: int) {
221221
if right <= left { ret; }
222222
let v: T = arr.(right);
223223
let i: int = left - 1;
@@ -264,8 +264,8 @@ mod ivector {
264264
qsort3[T](compare_func_lt, compare_func_eq, arr, i, right);
265265
}
266266

267-
fn quick_sort3[@T](compare_func_lt: lteq[T], compare_func_eq: lteq[T],
268-
arr: &[mutable T]) {
267+
fn quick_sort3[@T](compare_func_lt: &lteq[T], compare_func_eq: &lteq[T],
268+
arr: &[mutable T]) {
269269
if ilen[T](arr) == 0u { ret; }
270270
qsort3[T](compare_func_lt, compare_func_eq, arr, 0,
271271
(ilen[T](arr) as int) - 1);

src/test/stdtest/vec.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,8 @@ fn test_slice() {
3939
#[test]
4040
fn test_map() {
4141
fn square(x: &int) -> int { ret x * x; }
42-
let op: option::operator[int, int] = square;
4342
let v: vec[int] = [1, 2, 3, 4, 5];
44-
let s: vec[int] = map[int, int](op, v);
43+
let s: vec[int] = map(square, v);
4544
let i: int = 0;
4645
while i < 5 { assert (v.(i) * v.(i) == s.(i)); i += 1; }
4746
}
@@ -92,4 +91,4 @@ fn test_position_pred() {
9291
let v1: vec[int] = [5, 4, 3, 2, 1];
9392
assert (position_pred(less_than_three, v1) == option::some[uint](3u));
9493
assert (position_pred(is_eighteen, v1) == option::none[uint]);
95-
}
94+
}

0 commit comments

Comments
 (0)