         }
     }
 
-    // Unwrap a success value or throw a failure value.
+    /**
+     Returns or throws the wrapped value or error.
+     
+     - throws: The wrapped error, if the result is a failure.
+     - returns: The wrapped value, if the result is a success.
+     
+     The opposite operation is `init(create:)`.
+     */
     public func value() throws -> Value {
         switch self {
         case .success(let value):

        }
    }

    // Return a failure if we have one.
    // Otherwise, transform the success value into a new success value.
    public func map<U>(_ transform: (Value) -> U) -> Result<U> {
        return tryMap(transform)
    }

    // The canonical flatMap.
    // Return a failure if we have one.
    // Otherwise, transform the success value into a new success or failure.
    public func flatMap<U>(_ transform: (Value) -> Result<U>) -> Result<U> {
        return tryMap { value in
            try transform(value).value()
        }
    }

    // An error-catching variation on flatMap.
    // Return a failure if we have one.
    // Otherwise, transform the success value into a new success value, or fail if an error is thrown.
    public func tryMap<U>(_ transform: (Value) throws -> U) -> Result<U> {
        return Result<U> {
            try transform(try value())


import Foundation

/**
 A model for a success or failure case.

 This type precisely represents the domain of results of a function call that either returns a value or throws an error.
 
 Result is used by `Promise` to represent success or failure outside the normal flow of synchronous execution.
 
 Result is a functor and a monad; you can map and flatMap.

 - `map` transforms a success value into a new success value, but preserves errors.
 - `flatMap` also preserves errors, but transforms a success value to a new Result, meaning it may produce a new failure.
 - `tryMap` works like `flatMap`, but uses a transformation function that returns or throws.
 */
public enum Result<Value> {

    /// A successful result wrapping a returned value.
    case success(Value)
    /// A failed result wrapping a thrown error.
    case failure(Error)

    /**
     Creates a result by immediately invoking a function that returns or throws.

     - parameter create: A function whose returned value or thrown error becomes wrapped by the result.

     The opposite operation is `value()`.
     */
    public init(create: () throws -> Value) {
        do {
            let value = try create()
