Skip to content

Commit

Permalink
Refactored while_some (libstd/option.rs)
Browse files Browse the repository at this point in the history
The old 'while' needed to match 2 times for each iteration. With the new
'loop' there is just one match needed.

I have also replaced 'blk' by 'f' to be more consistent with parameter
names in other functions that are implemented for Option
  • Loading branch information
aochagavia committed Mar 13, 2014
1 parent 62f1d68 commit 6ff207b
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/libstd/option.rs
Expand Up @@ -295,10 +295,13 @@ impl<T> Option<T> {

/// Applies a function zero or more times until the result is `None`.
#[inline]
pub fn while_some(self, blk: |v: T| -> Option<T>) {
pub fn while_some(self, f: |v: T| -> Option<T>) {
let mut opt = self;
while opt.is_some() {
opt = blk(opt.unwrap());
loop {
match opt {
Some(x) => opt = f(x),
None => break
}
}
}

Expand Down

5 comments on commit 6ff207b

@bors
Copy link
Contributor

@bors bors commented on 6ff207b Mar 14, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 6ff207b Mar 14, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging aochagavia/rust/Optimize-while_some = 6ff207b into auto

@bors
Copy link
Contributor

@bors bors commented on 6ff207b Mar 14, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aochagavia/rust/Optimize-while_some = 6ff207b merged ok, testing candidate = d367482

@bors
Copy link
Contributor

@bors bors commented on 6ff207b Mar 14, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = d367482

Please sign in to comment.