<commit_before>//
//  deferred-parallel.swift
//  async-deferred
//
//  Created by <NAME> on 06/11/2015.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import Dispatch

extension Deferred
{
  /// Initialize an array of `Deferred` to be computed in parallel, at the current quality of service level
  ///
  /// - parameter count: the number of parallel tasks to perform
  /// - parameter task: the computation to be performed in parallel
  /// - returns: an array of `Deferred`

  public static func inParallel(count count: Int, _ task: (index: Int) throws -> T) -> [Deferred<T>]
  {
    return (0..<count).deferredMap(task)
  }

  /// Initialize an array of `Deferred` to be computed in parallel, at the desired quality of service level
  ///
  /// - parameter count: the number of parallel tasks to perform
  /// - parameter qos: the desired quality of service class for the new `Deferred` objects
  /// - parameter task: the computation to be performed in parallel
  /// - returns: an array of `Deferred`

  public static func inParallel(count count: Int, qos: qos_class_t, task: (index: Int) throws -> T) -> [Deferred<T>]
  {
    return (0..<count).deferredMap(qos, task: task)
  }

  /// Initialize an array of `Deferred` to be computed in parallel, on the desired dispatch queue
  ///
  /// - parameter count: the number of parallel tasks to perform
  /// - parameter queue: the `dispatch_queue` onto which the `Deferreds` should be performed.
  /// - parameter task: the computation to be performed in parallel
  /// - returns: an array of `Deferred`

  public static func inParallel(count count: Int, queue: dispatch_queue_t, task: (index: Int) throws -> T) -> [Deferred<T>]
  {
    return (0..<count).deferredMap(queue, task: task)
  }
}

extension CollectionType
{
  /// Map a collection to an array of `Deferred` to be computed in parallel, at the current quality of service level
  ///
  /// - parameter task: the computation to be performed in parallel
  /// - returns: an array of `Deferred`

  public func deferredMap<T>(task: (Self.Generator.Element) throws -> T) -> [Deferred<T>]
  {
    return deferredMap(dispatch_get_global_queue(qos_class_self(), 0), task: task)
  }

  /// Map a collection to an array of `Deferred` to be computed in parallel, at the desired quality of service level
  ///
  /// - parameter qos: the desired quality of service class for the new `Deferred` objects
  /// - parameter task: the computation to be performed in parallel
  /// - returns: an array of `Deferred`

  public func deferredMap<T>(qos: qos_class_t, task: (Self.Generator.Element) throws -> T) -> [Deferred<T>]
  {
    return deferredMap(dispatch_get_global_queue(qos, 0), task: task)
  }

  /// Map a collection to an array of `Deferred` to be computed in parallel, on the desired dispatch queue
  ///
  /// - parameter queue: the `dispatch_queue` onto which the `Deferreds` should be performed.
  /// - parameter task: the computation to be performed in parallel
  /// - returns: an array of `Deferred`

  public func deferredMap<T>(queue: dispatch_queue_t, task: (Self.Generator.Element) throws -> T) -> [Deferred<T>]
  {
    // The following 2 lines exist to get around the fact that Self.Index.Distance does not convert to Int.
    let indices = Array(self.indices)
    let count = indices.count

    let deferreds = (indices).map { _ in TBD<T>() }
    dispatch_async(dispatch_get_global_queue(dispatch_queue_get_qos_class(queue, nil), 0)) {
      dispatch_apply(count, queue) {
        index in
        deferreds[index].beginExecution()
        let result = Result { try task(self[indices[index]]) }
        _ = try? deferreds[index].determine(result) // an error here means `deferred[index]` has been canceled
      }
    }
    return deferreds
  }
}
<commit_msg>un-silence a parameter name<commit_after>//
//  deferred-parallel.swift
//  async-deferred
//
//  Created by <NAME> on 06/11/2015.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import Dispatch

extension Deferred
{
  /// Initialize an array of `Deferred` to be computed in parallel, at the current quality of service level
  ///
  /// - parameter count: the number of parallel tasks to perform
  /// - parameter task: the computation to be performed in parallel
  /// - returns: an array of `Deferred`

  public static func inParallel(count count: Int, task: (index: Int) throws -> T) -> [Deferred<T>]
  {
    return (0..<count).deferredMap(task)
  }

  /// Initialize an array of `Deferred` to be computed in parallel, at the desired quality of service level
  ///
  /// - parameter count: the number of parallel tasks to perform
  /// - parameter qos: the desired quality of service class for the new `Deferred` objects
  /// - parameter task: the computation to be performed in parallel
  /// - returns: an array of `Deferred`

  public static func inParallel(count count: Int, qos: qos_class_t, task: (index: Int) throws -> T) -> [Deferred<T>]
  {
    return (0..<count).deferredMap(qos, task: task)
  }

  /// Initialize an array of `Deferred` to be computed in parallel, on the desired dispatch queue
  ///
  /// - parameter count: the number of parallel tasks to perform
  /// - parameter queue: the `dispatch_queue` onto which the `Deferreds` should be performed.
  /// - parameter task: the computation to be performed in parallel
  /// - returns: an array of `Deferred`

  public static func inParallel(count count: Int, queue: dispatch_queue_t, task: (index: Int) throws -> T) -> [Deferred<T>]
  {
    return (0..<count).deferredMap(queue, task: task)
  }
}

extension CollectionType
{
  /// Map a collection to an array of `Deferred` to be computed in parallel, at the current quality of service level
  ///
  /// - parameter task: the computation to be performed in parallel
  /// - returns: an array of `Deferred`

  public func deferredMap<T>(task: (Self.Generator.Element) throws -> T) -> [Deferred<T>]
  {
    return deferredMap(dispatch_get_global_queue(qos_class_self(), 0), task: task)
  }

  /// Map a collection to an array of `Deferred` to be computed in parallel, at the desired quality of service level
  ///
  /// - parameter qos: the desired quality of service class for the new `Deferred` objects
  /// - parameter task: the computation to be performed in parallel
  /// - returns: an array of `Deferred`

  public func deferredMap<T>(qos: qos_class_t, task: (Self.Generator.Element) throws -> T) -> [Deferred<T>]
  {
    return deferredMap(dispatch_get_global_queue(qos, 0), task: task)
  }

  /// Map a collection to an array of `Deferred` to be computed in parallel, on the desired dispatch queue
  ///
  /// - parameter queue: the `dispatch_queue` onto which the `Deferreds` should be performed.
  /// - parameter task: the computation to be performed in parallel
  /// - returns: an array of `Deferred`

  public func deferredMap<T>(queue: dispatch_queue_t, task: (Self.Generator.Element) throws -> T) -> [Deferred<T>]
  {
    // The following 2 lines exist to get around the fact that Self.Index.Distance does not convert to Int.
    let indices = Array(self.indices)
    let count = indices.count

    let deferreds = (indices).map { _ in TBD<T>() }
    dispatch_async(dispatch_get_global_queue(dispatch_queue_get_qos_class(queue, nil), 0)) {
      dispatch_apply(count, queue) {
        index in
        deferreds[index].beginExecution()
        let result = Result { try task(self[indices[index]]) }
        _ = try? deferreds[index].determine(result) // an error here means `deferred[index]` has been canceled
      }
    }
    return deferreds
  }
}

{'fraction_non_alphanumeric': 0.11487734107095753, 'fraction_numerical': 0.005011870218939594, 'mean_word_length': 3.15734649122807, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 51, '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': '6323459', 'n_tokens_mistral': 2254, 'n_tokens_neox': 2179, 'n_words': 853}