<commit_before>//
//  Promise.swift
//  Promissum
//
//  Created by <NAME> on 2014-10-11.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

import Foundation

/**
## A future value

_A Promise represents a future value._

To access it's value, register a handler using the `then` method:

```
somePromise.then { value in
  print("The value is: \(value)")
}
```

You can register multiple handlers to access the value multiple times.
To register more multiple handlers, simply call `then` multiple times.
Once available, the value becomes immutable and will never change.

## Failure

A Promise can fail during the computation of the future value.
In that case the error can also be accessed by registering a handler with `trap`:

```
somePromise.trap { error in
  print("The error is: \(error)")
}
```


## States

A Promise is always in one of three states: Unresolved, Resolved, or Rejected.
Once a Promise changes from Unresolved to Resolved/Rejected the appropriate registered handlers are called.
After the Promise has changed from Unresolved, it will always stay either Resolved or Rejected.

It is possible to register for both the value and the error, like so:

```
somePromise
  .then { value in
    print("The value is: \(value)")
  }
  .trap { error in
    print("The error is: \(error)")
  }
```


## Types

The full type `Promise<Value, Error>` has two type arguments, for both the value and the error.

For example; the type `Promise<String, NSError>` represents a future value of type `String` that can potentially fail with a `NSError`.
When creating a Promise yourself, it is recommended to use a custom enum to represent the possible errors cases.

In cases where an error is not applicable, you can use the `Never` type.


## Transforming a Promise value

Similar to `Array`, a Promise has a `map` method to apply a transform the value of a Promise.

In this example an age (Promise of int) is transformed to a future isAdult boolean:

```
// agePromise has type Promise<Int, Never>
// isAdultPromise has type Promise<Bool, Never>
let isAdultPromise = agePromise.map { age in age >= 18 }

```

Again, similar to Arrays, `flatMap` is also available.


## Creating a Promise

To create a Promise, use a `PromiseSoure`.
Note that it is often not needed to create a new Promise.
If an existing Promise is available, transforming that using `map` or `flatMap` is often sufficient.

*/
public class Promise<Value, Error> where Error: Swift.Error {
  private let source: PromiseSource<Value, Error>


  // MARK: Initializers

  /// Initialize a resolved Promise with a value.
  ///
  /// Example: `Promise<Int, Never>(value: 42)`
  public init(value: Value) {
    self.source = PromiseSource(value: value)
  }

  /// Initialize a rejected Promise with an error.
  ///
  /// Example: `Promise<Int, Error>(error: MyError(message: "Oops"))`
  public init(error: Error) {
    self.source = PromiseSource(error: error)
  }

  internal init(source: PromiseSource<Value, Error>) {
    self.source = source
  }


  // MARK: Computed properties

  /// Optionally get the underlying value of this Promise.
  /// Will be `nil` if Promise is Rejected or still Unresolved.
  ///
  /// In most situations it is recommended to register a handler with `then` method instead of directly using this property.
  public var value: Value? {
    if case .resolved(let value) = source.state {<commit_msg>Add Promise.init with function argument<commit_after>//
//  Promise.swift
//  Promissum
//
//  Created by <NAME> on 2014-10-11.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

import Foundation

/**
## A future value

_A Promise represents a future value._

To access it's value, register a handler using the `then` method:

```
somePromise.then { value in
  print("The value is: \(value)")
}
```

You can register multiple handlers to access the value multiple times.
To register more multiple handlers, simply call `then` multiple times.
Once available, the value becomes immutable and will never change.

## Failure

A Promise can fail during the computation of the future value.
In that case the error can also be accessed by registering a handler with `trap`:

```
somePromise.trap { error in
  print("The error is: \(error)")
}
```


## States

A Promise is always in one of three states: Unresolved, Resolved, or Rejected.
Once a Promise changes from Unresolved to Resolved/Rejected the appropriate registered handlers are called.
After the Promise has changed from Unresolved, it will always stay either Resolved or Rejected.

It is possible to register for both the value and the error, like so:

```
somePromise
  .then { value in
    print("The value is: \(value)")
  }
  .trap { error in
    print("The error is: \(error)")
  }
```


## Types

The full type `Promise<Value, Error>` has two type arguments, for both the value and the error.

For example; the type `Promise<String, NSError>` represents a future value of type `String` that can potentially fail with a `NSError`.
When creating a Promise yourself, it is recommended to use a custom enum to represent the possible errors cases.

In cases where an error is not applicable, you can use the `Never` type.


## Transforming a Promise value

Similar to `Array`, a Promise has a `map` method to apply a transform the value of a Promise.

In this example an age (Promise of int) is transformed to a future isAdult boolean:

```
// agePromise has type Promise<Int, Never>
// isAdultPromise has type Promise<Bool, Never>
let isAdultPromise = agePromise.map { age in age >= 18 }

```

Again, similar to Arrays, `flatMap` is also available.


## Creating a Promise

To create a Promise, use a `PromiseSoure`.
Note that it is often not needed to create a new Promise.
If an existing Promise is available, transforming that using `map` or `flatMap` is often sufficient.

*/
public class Promise<Value, Error> where Error: Swift.Error {
  private let source: PromiseSource<Value, Error>


  // MARK: Initializers

  /// Initialize a resolved Promise with a value.
  ///
  /// Example: `Promise<Int, Never>(value: 42)`
  public init(value: Value) {
    self.source = PromiseSource(value: value)
  }

  /// Initialize a rejected Promise with an error.
  ///
  /// Example: `Promise<Int, Error>(error: MyError(message: "Oops"))`
  public init(error: Error) {
    self.source = PromiseSource(error: error)
  }

  public init(_ completion: (PromiseSource<Value, Error>) -> Void) {
    let source = PromiseSource<Value, Error>()
    self.source = source

    completion(source)
  }

  internal init(source: PromiseSource<Value, Error>) {
    self.source = source
  }


  // MARK: Computed properties

  /// Optionally get the underlying value of this Promise.
  /// Will be `nil` if Promise is Rejected or still Unresolved.
  ///
  /// In most situations it is recommended to register a handler with `then` method instead of directly using this property.
  public var value: Value? {
    if case .resolved(let value) = source.state {
{'fraction_non_alphanumeric': 0.09565341721475551, 'fraction_numerical': 0.004575350300257364, 'mean_word_length': 3.65093085106383, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 30, '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': '3604290', 'n_tokens_mistral': 2035, 'n_tokens_neox': 1917, 'n_words': 925}