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

import Foundation

/// Flattens a nested Promise of Promise into a single Promise.
///
/// The returned Promise resolves (or rejects) when the nested Promise resolves.
public func flatten<Value, Error>(_ promise: Promise<Promise<Value, Error>, Error>) -> Promise<Value, Error> {
  return promise.flatMap { $0 }
}


/// Creates a Promise that resolves when both arguments resolve.
///
/// The new Promise's value is of a tuple type constructed from both argument promises.
///
/// If either of the two Promises fails, the returned Promise also fails.
public func whenBoth<A, B, Error>(_ promiseA: Promise<A, Error>, _ promiseB: Promise<B, Error>) -> Promise<(A, B), Error> {
  return promiseA.flatMap { valueA in promiseB.map { valueB in (valueA, valueB) } }
}<commit_msg>whenBoth returns on first error<commit_after>//
//  Combinators.swift
//  Promissum
//
//  Created by <NAME> on 2014-10-11.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

import Foundation

/// Flattens a nested Promise of Promise into a single Promise.
///
/// The returned Promise resolves (or rejects) when the nested Promise resolves.
public func flatten<Value, Error>(_ promise: Promise<Promise<Value, Error>, Error>) -> Promise<Value, Error> {
  return promise.flatMap { $0 }
}


/// Creates a Promise that resolves when both arguments resolve.
///
/// The new Promise's value is of a tuple type constructed from both argument promises.
///
/// If either of the two Promises fails, the returned Promise also fails.
public func whenBoth<A, B, Error>(_ promiseA: Promise<A, Error>, _ promiseB: Promise<B, Error>) -> Promise<(A, B), Error> {
  let source = PromiseSource<(A, B), Error>()

  promiseA.trap(source.reject)
  promiseB.trap(source.reject)

  promiseA.then { valueA in
    promiseB.then { valueB in
      source.resolve((valueA, valueB))
    }
  }

  return source.promise
}
{'fraction_non_alphanumeric': 0.12834489593657086, 'fraction_numerical': 0.01288404360753221, 'mean_word_length': 3.4966592427616927, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 28, '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': '2863470', 'n_tokens_mistral': 631, 'n_tokens_neox': 599, 'n_words': 230}