From cf9fcb975d5429a0a8e08cad8f5c026ca48b4b63 Mon Sep 17 00:00:00 2001 From: eunjiChung <> Date: Sun, 31 May 2020 17:43:22 +0900 Subject: [PATCH 1/2] [stdlib] Add flatMap example in Result.swift --- stdlib/public/core/Result.swift | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/stdlib/public/core/Result.swift b/stdlib/public/core/Result.swift index f92818baa5f21..5c0db142e719e 100644 --- a/stdlib/public/core/Result.swift +++ b/stdlib/public/core/Result.swift @@ -89,6 +89,21 @@ public enum Result { /// Returns a new result, mapping any success value using the given /// transformation and unwrapping the produced result. /// + /// Use this method when you need to transform the value of a `Result` + /// instance eventhough it produces a nested 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 { /* ... */ } + /// func getNextAfterInteger() -> Result { /* ... */ } + /// + /// 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` From ffe471ada81d72a4bbb3a46b125e8de5d4ad074f Mon Sep 17 00:00:00 2001 From: eunjiChung <> Date: Thu, 4 Jun 2020 21:39:12 +0900 Subject: [PATCH 2/2] [stdlib] Add flatMap example in Result.swift --- stdlib/public/core/Result.swift | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/stdlib/public/core/Result.swift b/stdlib/public/core/Result.swift index 5c0db142e719e..d7276880dbb54 100644 --- a/stdlib/public/core/Result.swift +++ b/stdlib/public/core/Result.swift @@ -89,14 +89,18 @@ public enum Result { /// Returns a new result, mapping any success value using the given /// transformation and unwrapping the produced result. /// - /// Use this method when you need to transform the value of a `Result` - /// instance eventhough it produces a nested result type. + /// 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 { /* ... */ } - /// func getNextAfterInteger() -> Result { /* ... */ } + /// func getNextInteger() -> Result { + /// .success(4) + /// } + /// func getNextAfterInteger(_ n: Int) -> Result { + /// .success(n + 1) + /// } /// /// let result = getNextInteger().map({ getNextAfterInteger($0) }) /// // result == .success(.success(5))