diff --git a/src/libcore/option.rs b/src/libcore/option.rs index 80b3915cb90d..dabfa04ef245 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -52,6 +52,16 @@ pure fn chain(opt: option, f: fn(T) -> option) -> option { alt opt { some(x) { f(x) } none { none } } } +#[inline(always)] +pure fn while_some(+x: option, blk: fn(+T) -> option) { + //! Applies a function zero or more times until the result is none. + + let mut opt <- x; + while opt.is_some() { + opt = blk(unwrap(opt)); + } +} + pure fn is_none(opt: option) -> bool { //! Returns true if the option equals `none` @@ -106,18 +116,18 @@ impl extensions for option { * Update an optional value by optionally running its content through a * function that returns an option. */ - fn chain(f: fn(T) -> option) -> option { chain(self, f) } + pure fn chain(f: fn(T) -> option) -> option { chain(self, f) } /// Applies a function to the contained value or returns a default - fn map_default(def: U, f: fn(T) -> U) -> U + pure fn map_default(def: U, f: fn(T) -> U) -> U { map_default(self, def, f) } /// Performs an operation on the contained value or does nothing - fn iter(f: fn(T)) { iter(self, f) } + pure fn iter(f: fn(T)) { iter(self, f) } /// Returns true if the option equals `none` - fn is_none() -> bool { is_none(self) } + pure fn is_none() -> bool { is_none(self) } /// Returns true if the option contains some value - fn is_some() -> bool { is_some(self) } + pure fn is_some() -> bool { is_some(self) } /// Maps a `some` value from one type to another - fn map(f: fn(T) -> U) -> option { map(self, f) } + pure fn map(f: fn(T) -> U) -> option { map(self, f) } } impl extensions for option { @@ -128,8 +138,8 @@ impl extensions for option { * * Fails if the value equals `none` */ - fn get() -> T { get(self) } - fn get_default(def: T) -> T { get_default(self, def) } + pure fn get() -> T { get(self) } + pure fn get_default(def: T) -> T { get_default(self, def) } /** * Gets the value out of an option, printing a specified message on * failure @@ -139,6 +149,8 @@ impl extensions for option { * Fails if the value equals `none` */ pure fn expect(reason: ~str) -> T { expect(self, reason) } + /// Applies a function zero or more times until the result is none. + pure fn while_some(blk: fn(+T) -> option) { while_some(self, blk) } } #[test] @@ -177,6 +189,20 @@ fn test_unwrap_resource() { assert *i == 1; } +#[test] +fn test_option_while_some() { + let mut i = 0; + do some(10).while_some |j| { + i += 1; + if (j > 0) { + some(j-1) + } else { + none + } + } + assert i == 11; +} + // Local Variables: // mode: rust; // fill-column: 78;