Closed
Description
I found myself wanting this a lot during ICFP (#2928):
fn sequence<T>(+x: option<T>, blk: fn(+T) -> option<T>) {
if x.is_some() {
sequence(blk(option::unwrap(x)), blk)
}
}
impl iteration<T: copy> for option<T> {
fn sequence(blk: fn(+T) -> option<T>) {
if self.is_some() {
blk(self.get()).sequence(blk)
}
}
}
(It would have to be written non-tail-recursively, for non-optimised builds to be able to infinite loop with it, but the tail recursive way is so much prettier.)
As it is, option::iter
is just a special case of option::map
where the return type of the block is unit. No need for it to be a different function.