Skip to content

Add a loop-iterator for option (suggest better names) #2927

Closed
@bblum

Description

@bblum

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

No one assigned

    Labels

    E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions