Skip to content

Best way to collapse Option<Option<T>> to Option<T> #83

Description

@usr-ein

Hello!

I was happily using this lib (great lib!), and I ran into a case where I ran a function that looks like this:

const addNullStartPadding = <T>(
    arr: T[][],
    extraRows: number,
    extraCols: number,
): Option<T>[][] =>

On a matrix that already contained Options, (e.g. const a = [[Some(1), None, Some(2)], [...], [...]]).

This thus returned me an Option<Option<number>>[][], and I want to "collapse" it to Option<number>[][] by merging the two options.

I found that I could do:

const result: Option<Option<number>>[][] = ...;
return result.map((row) => row.map((value) => value.andThen((v) => v)))

which is an effective way to do so, but I was expecting a more practical way to do this. Perhaps value.collapse(), or something.

What other, simpler way would there be to collapse like that?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions