-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Closed
Labels
E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
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.
Metadata
Metadata
Assignees
Labels
E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.