<commit_before>//
//  BulkPromise.swift
//  Pods
//
//  Created by <NAME> on 12/29/16.
//
//

import Foundation

public class BulkPromise<T, O: Error>: Promise<[T], O> {
    
    let promises: [Promise<T, O>]
    var results: [(Int, T)] {
        didSet {
            if results.count == promises.count {
                success(with: results.sorted { $0.0 < $1.0 } => lastArgument)
            }
        }
    }
    
    init(promises: [Promise<T,O>], completionQueue: DispatchQueue = .main) {
        self.promises = promises
        results = []
        super.init(completionQueue: completionQueue)
        self.promises => { promise, index in
            promise
                .onSuccess {
                    self.results.append((index, $0))
                }
                .onError(call: self.error)
        }
    }
    
}

extension BulkPromise where T: Collection {
    
    var flattened: Promise<[T.Iterator.Element], O> {
        let promise = Promise<[T.Iterator.Element], O>()
        self.onSuccess { result in
            promise.success(with: result.flatMap(id))
        }
        self.onError(call: promise.error)
        return promise
    }
    
}<commit_msg>Making flattened bulk promises with nested promise<commit_after>//
//  BulkPromise.swift
//  Pods
//
//  Created by <NAME> on 12/29/16.
//
//

import Foundation

public class BulkPromise<T, O: Error>: Promise<[T], O> {
    
    let promises: [Promise<T, O>]
    var results: [(Int, T)] {
        didSet {
            if results.count == promises.count {
                success(with: results.sorted { $0.0 < $1.0 } => lastArgument)
            }
        }
    }
    
    init(promises: [Promise<T,O>], completionQueue: DispatchQueue = .main) {
        self.promises = promises
        results = []
        super.init(completionQueue: completionQueue)
        self.promises => { promise, index in
            promise
                .onSuccess {
                    self.results.append((index, $0))
                }
                .onError(call: self.error)
        }
    }
    
}

extension BulkPromise where T: Collection {
    
    var flattened: Promise<[T.Iterator.Element], O> {
        return nested { result in
            return result.flatMap(id)
        }
    }
    
}
{'fraction_non_alphanumeric': 0.12643678160919541, 'fraction_numerical': 0.009725906277630416, 'mean_word_length': 1.6654888103651355, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 20, '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': '4129290', 'n_tokens_mistral': 706, 'n_tokens_neox': 674, 'n_words': 151}