<commit_before>//
//  RxResult.swift
//  Rx
//
//  Created by <NAME> on 2/12/15.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

import Foundation

// Represents computation result.
//
// The result can be either successful or failure.
//
// It's a port of Scala (originally Twitter) `Try` type
// http://www.scala-lang.org/api/2.10.2/index.html#scala.util.Try
// The name `Result` was chosen because it better describes it's common usage.
//
// The reason why it's named differently is because `Try` doesn't make sense in the context
// of swift.
//
// In scala it would be used like `Try { throw new Exception("oh dear") }`
//
// The reason why `Result` has `Rx` prefix is because there could be name collisions with other
// result types.
//
public enum RxResult<T> {
    // Box is used is because swift compiler doesn't know
    // how to handle `Success(ResultType)` and it crashes.
    case Success(RxBox<T>)
    case Failure(ErrorType)
}

extension RxResult {
    
    // Returns the given function applied to the value from `Success` or returns `self` if this is a `Failure`.
    public func flatMap<U>(@noescape f: T -> RxResult<U>) -> RxResult<U> {
        switch self {
        case .Success(let boxedValue):
            return f(boxedValue.value)
        case .Failure(let error):
            return failure(error)
        }
    }
    
    // Maps the given function to the value from `Success` or returns `self` if this is a `Failure`.
    public func map<U>(@noescape f: T -> U) -> RxResult<U> {
        switch self {
        case .Success(let boxedValue):
            return success(f(boxedValue.value))
        case .Failure(let error):
            return failure(error)
        }
    }
    
    // Applies the given function `f` if this is a `Failure`, otherwise returns `self` if this is a `Success`.
    public func recover(@noescape f: (ErrorType) -> T) -> RxResult<T> {
        switch self {
        case .Success(_):
            return self<commit_msg>Adds isSuccess and isFailure to RxResult.<commit_after>//
//  RxResult.swift
//  Rx
//
//  Created by <NAME> on 2/12/15.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

import Foundation

// Represents computation result.
//
// The result can be either successful or failure.
//
// It's a port of Scala (originally Twitter) `Try` type
// http://www.scala-lang.org/api/2.10.2/index.html#scala.util.Try
// The name `Result` was chosen because it better describes it's common usage.
//
// The reason why it's named differently is because `Try` doesn't make sense in the context
// of swift.
//
// In scala it would be used like `Try { throw new Exception("oh dear") }`
//
// The reason why `Result` has `Rx` prefix is because there could be name collisions with other
// result types.
//
public enum RxResult<T> {
    // Box is used is because swift compiler doesn't know
    // how to handle `Success(ResultType)` and it crashes.
    case Success(RxBox<T>)
    case Failure(ErrorType)
}

extension RxResult {
    
    // Returns true if `self` is a `Success`, false otherwise.
    public var isSuccess: Bool {
        get {
            switch self {
            case .Success:
                return true
            default:
                return false
            }
        }
    }

    // Returns true if `self` is a `Failure`, false otherwise.
    public var isFailure: Bool {
        get {
            switch self {
            case .Failure:
                return true
            default:
                return false
            }
        }
    }
    
    // Returns the given function applied to the value from `Success` or returns `self` if this is a `Failure`.
    public func flatMap<U>(@noescape f: T -> RxResult<U>) -> RxResult<U> {
        switch self {
        case .Success(let boxedValue):
            return f(boxedValue.value)
        case .Failure(let error):
            return failure(error)
        }
    }
    
    // Maps the given function to the value from `Success` or returns `self` if this is a `Failure`.
    public func map<U>(@noescape f: T -> U) -> RxResult<U> {
        switch self {
        case .Success(let boxedValue):
            return success(f(boxedValue.value))
        case .Failure(let error):
            return failure(error)
        }
    }
    
    // Applies the given function `f` if this is a `Failure`, otherwise returns `self` if this is a `Success`.
    public func recover(@noescape f: (ErrorType) -> T) -> RxResult<T> {
        switch self {
        case .Success(_):
            return self
{'fraction_non_alphanumeric': 0.11211453744493392, 'fraction_numerical': 0.005726872246696035, 'mean_word_length': 2.116678105696637, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '945383', 'n_tokens_mistral': 1350, 'n_tokens_neox': 1285, 'n_words': 502}