@@ -52,6 +52,16 @@ pure fn chain<T, U>(opt: option<T>, f: fn(T) -> option<U>) -> option<U> {
52
52
alt opt { some( x) { f ( x) } none { none } }
53
53
}
54
54
55
+ #[ inline( always) ]
56
+ pure fn while_some < T > ( +x : option < T > , blk : fn ( +T ) -> option < T > ) {
57
+ //! Applies a function zero or more times until the result is none.
58
+
59
+ let mut opt <- x;
60
+ while opt. is_some ( ) {
61
+ opt = blk ( unwrap ( opt) ) ;
62
+ }
63
+ }
64
+
55
65
pure fn is_none < T > ( opt : option < T > ) -> bool {
56
66
//! Returns true if the option equals `none`
57
67
@@ -106,18 +116,18 @@ impl extensions<T> for option<T> {
106
116
* Update an optional value by optionally running its content through a
107
117
* function that returns an option.
108
118
*/
109
- fn chain < U > ( f : fn ( T ) -> option < U > ) -> option < U > { chain ( self , f) }
119
+ pure fn chain < U > ( f : fn ( T ) -> option < U > ) -> option < U > { chain ( self , f) }
110
120
/// Applies a function to the contained value or returns a default
111
- fn map_default < U : copy > ( def : U , f : fn ( T ) -> U ) -> U
121
+ pure fn map_default < U : copy > ( def : U , f : fn ( T ) -> U ) -> U
112
122
{ map_default ( self , def, f) }
113
123
/// Performs an operation on the contained value or does nothing
114
- fn iter ( f : fn ( T ) ) { iter ( self , f) }
124
+ pure fn iter ( f : fn ( T ) ) { iter ( self , f) }
115
125
/// Returns true if the option equals `none`
116
- fn is_none ( ) -> bool { is_none ( self ) }
126
+ pure fn is_none ( ) -> bool { is_none ( self ) }
117
127
/// Returns true if the option contains some value
118
- fn is_some ( ) -> bool { is_some ( self ) }
128
+ pure fn is_some ( ) -> bool { is_some ( self ) }
119
129
/// Maps a `some` value from one type to another
120
- fn map < U : copy > ( f : fn ( T ) -> U ) -> option < U > { map ( self , f) }
130
+ pure fn map < U : copy > ( f : fn ( T ) -> U ) -> option < U > { map ( self , f) }
121
131
}
122
132
123
133
impl extensions < T : copy > for option < T > {
@@ -128,8 +138,8 @@ impl extensions<T: copy> for option<T> {
128
138
*
129
139
* Fails if the value equals `none`
130
140
*/
131
- fn get ( ) -> T { get ( self ) }
132
- fn get_default ( def : T ) -> T { get_default ( self , def) }
141
+ pure fn get ( ) -> T { get ( self ) }
142
+ pure fn get_default ( def : T ) -> T { get_default ( self , def) }
133
143
/**
134
144
* Gets the value out of an option, printing a specified message on
135
145
* failure
@@ -139,6 +149,8 @@ impl extensions<T: copy> for option<T> {
139
149
* Fails if the value equals `none`
140
150
*/
141
151
pure fn expect ( reason : ~str ) -> T { expect ( self , reason) }
152
+ /// Applies a function zero or more times until the result is none.
153
+ pure fn while_some ( blk : fn ( +T ) -> option < T > ) { while_some ( self , blk) }
142
154
}
143
155
144
156
#[ test]
@@ -177,6 +189,20 @@ fn test_unwrap_resource() {
177
189
assert * i == 1 ;
178
190
}
179
191
192
+ #[ test]
193
+ fn test_option_while_some ( ) {
194
+ let mut i = 0 ;
195
+ do some( 10 ) . while_some |j| {
196
+ i += 1 ;
197
+ if ( j > 0 ) {
198
+ some ( j-1 )
199
+ } else {
200
+ none
201
+ }
202
+ }
203
+ assert i == 11 ;
204
+ }
205
+
180
206
// Local Variables:
181
207
// mode: rust;
182
208
// fill-column: 78;
0 commit comments