@@ -109,20 +109,21 @@ fn chain_err<T: copy, U: copy, V: copy>(
109
109
}
110
110
}
111
111
112
- // ______________________________________________________________________
113
- // Note:
114
- //
115
- // These helper functions are written in a "pre-chained" (a.k.a,
116
- // deforested) style because I have found that, in practice, this is
117
- // the most concise way to do things. That means that they do not not
118
- // terminate with a call to `ok(v)` but rather `nxt(v)`. If you would
119
- // like to just get the result, just pass in `ok` as `nxt`.
112
+ impl methods < T : copy , E : copy > for result < T , E > {
113
+ fn chain < U : copy > ( op : fn ( T ) -> result < U , E > ) -> result < U , E > {
114
+ chain ( self , op)
115
+ }
116
+
117
+ fn chain_err < F : copy > ( op : fn ( E ) -> result < T , F > ) -> result < T , F > {
118
+ chain_err ( self , op)
119
+ }
120
+ }
120
121
121
122
#[ doc = "
122
123
Maps each element in the vector `ts` using the operation `op`. Should an
123
124
error occur, no further mappings are performed and the error is returned.
124
125
Should no error occur, a vector containing the result of each map is
125
- passed to the `nxt` function .
126
+ returned .
126
127
127
128
Here is an example which increments every integer in a vector,
128
129
checking for overflow:
@@ -131,27 +132,11 @@ checking for overflow:
131
132
if x == uint::max_value { ret err(\" overflow\" ); }
132
133
else { ret ok(x+1u); }
133
134
}
134
- map([1u, 2u, 3u], inc_conditionally) {|incd|
135
- assert incd == [2u, 3u, 4u];
136
- }
137
-
138
- Note: if you have to combine a deforested style transform with map,
139
- you should use `ok` for the `nxt` operation, as shown here (this is an
140
- alternate version of the previous example where the
141
- `inc_conditionally()` routine is deforested):
142
-
143
- fn inc_conditionally<T>(x: uint,
144
- nxt: fn(uint) -> result<T,str>) -> result<T,str> {
145
- if x == uint::max_value { ret err(\" overflow\" ); }
146
- else { ret nxt(x+1u); }
147
- }
148
- map([1u, 2u, 3u], inc_conditionally(_, ok)) {|incd|
135
+ map([1u, 2u, 3u], inc_conditionally).chain {|incd|
149
136
assert incd == [2u, 3u, 4u];
150
137
}
151
138
" ]
152
- fn map < T , U : copy , V : copy , W > ( ts : [ T ] ,
153
- op : fn ( T ) -> result < V , U > ,
154
- nxt : fn ( [ V ] ) -> result < W , U > ) -> result < W , U > {
139
+ fn map < T , U : copy , V : copy > ( ts : [ T ] , op : fn ( T ) -> result < V , U > ) -> result < [ V ] , U > {
155
140
let mut vs: [ V ] = [ ] ;
156
141
vec:: reserve ( vs, vec:: len ( ts) ) ;
157
142
for t in ts {
@@ -160,7 +145,7 @@ fn map<T,U:copy,V:copy,W>(ts: [T],
160
145
err ( u) { ret err ( u) ; }
161
146
}
162
147
}
163
- ret nxt ( vs) ;
148
+ ret ok ( vs) ;
164
149
}
165
150
166
151
#[ doc = "Same as map, but it operates over two parallel vectors.
@@ -170,11 +155,9 @@ length. While we do not often use preconditions in the standard
170
155
library, a precondition is used here because result::t is generally
171
156
used in 'careful' code contexts where it is both appropriate and easy
172
157
to accommodate an error like the vectors being of different lengths." ]
173
- fn map2 < S , T , U : copy , V : copy , W > ( ss : [ S ] , ts : [ T ] ,
174
- op : fn ( S , T ) -> result < V , U > ,
175
- nxt : fn ( [ V ] ) -> result < W , U > )
176
- : vec:: same_length( ss , ts )
177
- -> result < W , U > {
158
+ fn map2 < S , T , U : copy , V : copy > ( ss : [ S ] , ts : [ T ] , op : fn ( S , T ) -> result < V , U > )
159
+ : vec:: same_length( ss , ts ) -> result < [ V ] , U > {
160
+
178
161
let n = vec:: len ( ts) ;
179
162
let mut vs = [ ] ;
180
163
vec:: reserve ( vs, n) ;
@@ -186,13 +169,19 @@ fn map2<S,T,U:copy,V:copy,W>(ss: [S], ts: [T],
186
169
}
187
170
i += 1 u;
188
171
}
189
- ret nxt ( vs) ;
172
+ ret ok ( vs) ;
190
173
}
191
174
175
+ #[ doc = "
176
+ Applies op to the pairwise elements from `ss` and `ts`, aborting on
177
+ error. This could be implemented using `map2()` but it is more efficient
178
+ on its own as no result vector is built.
179
+ " ]
192
180
fn iter2 < S , T , U : copy > ( ss : [ S ] , ts : [ T ] ,
193
181
op : fn ( S , T ) -> result < ( ) , U > )
194
182
: vec:: same_length( ss , ts )
195
183
-> result < ( ) , U > {
184
+
196
185
let n = vec:: len ( ts) ;
197
186
let mut i = 0 u;
198
187
while i < n {
0 commit comments