Skip to content

[stdlib] Add flatMap example in Result.swift #32115

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

Merged
merged 2 commits into from
Jun 4, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions stdlib/public/core/Result.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,25 @@ public enum Result<Success, Failure: Error> {
/// Returns a new result, mapping any success value using the given
/// transformation and unwrapping the produced result.
///
/// Use this method to avoid a nested result when your transformation
/// produces another `Result` type.
///
/// In this example, note the difference in the result of using `map` and
/// `flatMap` with a transformation that returns an result type.
///
/// func getNextInteger() -> Result<Int, Error> {
/// .success(4)
/// }
/// func getNextAfterInteger(_ n: Int) -> Result<Int, Error> {
/// .success(n + 1)
/// }
///
/// let result = getNextInteger().map({ getNextAfterInteger($0) })
/// // result == .success(.success(5))
///
/// let result = getNextInteger().flatMap({ getNextAfterInteger($0) })
/// // result == .success(5)
///
/// - Parameter transform: A closure that takes the success value of the
/// instance.
/// - Returns: A `Result` instance with the result of evaluating `transform`
Expand Down