Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
usr-ein opened this issue Feb 14, 2024 · 0 comments
Open

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

usr-ein opened this issue Feb 14, 2024 · 0 comments

Comments

@usr-ein
Copy link

usr-ein commented Feb 14, 2024

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant