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

Adding a fold method #268

Open
deanWombourne opened this issue Jul 11, 2018 · 5 comments
Open

Adding a fold method #268

deanWombourne opened this issue Jul 11, 2018 · 5 comments

Comments

@deanWombourne
Copy link

I have a few situations where I need to convert a Result to a common type, and looking at other languages fold seems to exist.

Essentially, this:

public extension Result {

    /// Convert either a success or failure into type `U`
    public func fold<U>(success: (Value) throws -> U, failure: (Error) throws -> U) rethrows -> U {
        switch self {
        case .success(let value): return try success(value)
        case .failure(let error): return try failure(error)
        }
    }
}

Good idea / bad idea?

@deanWombourne
Copy link
Author

deanWombourne commented Jul 11, 2018

(Similar to recover / ?? except I want to map Value or Error to a new type, U)

@deanWombourne
Copy link
Author

Wait - is this just the same as analysis?

@mdiep
Copy link
Contributor

mdiep commented Jul 11, 2018

Yes, this is analysis.

@deanWombourne
Copy link
Author

NICE. You mind if I raise a PR to add throws / rethrows to it?

@mdiep
Copy link
Contributor

mdiep commented Jul 12, 2018

Why would you want 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

2 participants