<commit_before>//
//  Result.swift
//  swiftiandispatch
//
//  Created by <NAME> on 2015-07-16.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import Foundation

/// A Result type, approximately like everyone else has done.
///
/// Unlike other implementations, the error case does not encode type beyond ErrorType.
/// This way there is no need to ever map between error types, which is a thing that cannot make sense.

public enum Result<T>: CustomStringConvertible
{
  case Value(T)
  case Error(ErrorType)

  public init(value: T)
  {
    self = .Value(value)
  }

  public init(error: ErrorType)
  {
    self = .Error(error)
  }

  public init(@noescape task: () throws -> T)
  {
    do {
      let v = try task()
      self = .Value(v)
    }
    catch {
      self = .Error(error)
    }
  }


  public var value: T? {
    switch self
    {
    case .Value(let value): return value
    case .Error:            return nil
    }
  }

  public var error: ErrorType? {
    switch self
    {
    case .Value:            return nil
    case .Error(let error): return error
    }
  }

  public func asValue() throws -> T
  {
    switch self
    {
    case .Value(let value): return value
    case .Error(let error): throw error
    }
  }


  public var description: String {
    switch self
    {
    case .Value(let value): return "\(value)"
    case .Error(let error): return "Error: \(error)"
    }
  }


  public func map<U>(transform: (T) throws -> U) -> Result<U>
  {
    switch self
    {
    case .Value(let value): return Result<U> { try transform(value) }
    case .Error(let error): return .Error(error)
    }
  }

  public func flatMap<U>(transform: (T) -> Result<U>) -> Result<U>
  {
    switch self
    {
    case .Value(let value): return transform(value)
    case .Error(let error): return .Error(error)
    }
  }

  public func apply<U>(transform: Result<(T) throws -> U>) -> Result<U>
  {
    switch (self, transform)
    {
    case (.Value(let value), .Value(let transform)):
      return Result<U> { try transform(value) }

    case (.Value, .Error(let error)):
      return .Error(error)

    case (.Error(let error), _):
      return .Error(error)

    default: fatalError("The compiler made me do it.")
    }
  }
}

public func ?? <T> (possible: Result<T>, @autoclosure alternate: () -> T) -> T
{
  switch possible
  {
  case .Value(let value): return value
  case .Error:            return alternate()
  }
}
<commit_msg>Get rid of an unreachable switch case<commit_after>//
//  Result.swift
//  swiftiandispatch
//
//  Created by <NAME> on 2015-07-16.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import Foundation

/// A Result type, approximately like everyone else has done.
///
/// Unlike other implementations, the error case does not encode type beyond ErrorType.
/// This way there is no need to ever map between error types, which is a thing that cannot make sense.

public enum Result<T>: CustomStringConvertible
{
  case Value(T)
  case Error(ErrorType)

  public init(value: T)
  {
    self = .Value(value)
  }

  public init(error: ErrorType)
  {
    self = .Error(error)
  }

  public init(@noescape task: () throws -> T)
  {
    do {
      let v = try task()
      self = .Value(v)
    }
    catch {
      self = .Error(error)
    }
  }


  public var value: T? {
    switch self
    {
    case .Value(let value): return value
    case .Error:            return nil
    }
  }

  public var error: ErrorType? {
    switch self
    {
    case .Value:            return nil
    case .Error(let error): return error
    }
  }

  public func asValue() throws -> T
  {
    switch self
    {
    case .Value(let value): return value
    case .Error(let error): throw error
    }
  }


  public var description: String {
    switch self
    {
    case .Value(let value): return "\(value)"
    case .Error(let error): return "Error: \(error)"
    }
  }


  public func map<U>(transform: (T) throws -> U) -> Result<U>
  {
    switch self
    {
    case .Value(let value): return Result<U> { try transform(value) }
    case .Error(let error): return .Error(error)
    }
  }

  public func flatMap<U>(transform: (T) -> Result<U>) -> Result<U>
  {
    switch self
    {
    case .Value(let value): return transform(value)
    case .Error(let error): return .Error(error)
    }
  }

  public func apply<U>(transform: Result<(T) throws -> U>) -> Result<U>
  {
    switch self
    {
    case .Value(let value):
      switch transform
      {
      case .Value(let transform):
        return Result<U> { try transform(value) }

      case .Error(let error):
        return .Error(error)
      }

    case .Error(let error):
      return .Error(error)
    }
  }
}

public func ?? <T> (possible: Result<T>, @autoclosure alternate: () -> T) -> T
{
  switch possible
  {
  case .Value(let value): return value
  case .Error:            return alternate()
  }
}

{'fraction_non_alphanumeric': 0.12878168438266557, 'fraction_numerical': 0.004905968928863451, 'mean_word_length': 2.2685370741482966, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5079844', 'n_tokens_mistral': 1603, 'n_tokens_neox': 1542, 'n_words': 504}