<commit_before>//
//  Executor.swift
//  Futuristics
//
//  Created by <NAME> on 03/08/2015.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import Foundation


public func onMainQueue<T, U>(closure: T throws -> U) -> (T -> Future<U>) {
    return onMainQueue(after: nil)(closure)
}

public func onMainQueue<T, U>(after delay: Double? = nil)(_ closure: T throws -> U) -> (T -> Future<U>) {
    return onQueue(dispatch_get_main_queue(), after: delay)(closure)
}

public func onBackgroundQueue<T, U>(closure: T throws -> U) -> (T -> Future<U>) {
    return onBackgroundQueue(after: nil)(closure)
}

public func onBackgroundQueue<T, U>(after delay: Double? = nil)(_ closure: T throws -> U) -> (T -> Future<U>) {
    return onQueue(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), after: delay)(closure)
}

public func onQueue<T, U>(queue: dispatch_queue_t, closure: T throws -> U) -> (T -> Future<U>) {
    return onQueue(queue, after: nil)(closure)
}

public func onQueue<T, U>(queue: dispatch_queue_t, after delay: Double? = nil)(_ closure: T throws -> U) -> (T -> Future<U>) {
    return { (parameter: T) in
        let promise = Promise<U>()
        if let delay = delay {
            let dispatchTime = dispatch_time(DISPATCH_TIME_NOW, Int64(delay * Double(NSEC_PER_SEC)))
            dispatch_after(dispatchTime, queue) {
                defer { promise.ensureResolution() }
                promise.resolve { try closure(parameter) }
            }
        } else {
            if queue === dispatch_get_main_queue() && NSThread.isMainThread() {
                defer { promise.ensureResolution() }
                promise.resolve { try closure(parameter) }
            } else {
                dispatch_async(queue) {
                    defer { promise.ensureResolution() }
                    promise.resolve { try closure(parameter) }
                }
            }
        }
        return promise.future
    }
}

public func await<T>(futures: Future<T> ...) {
    await(futures)
}

public func await<T>(futures: [Future<T>]) {
    guard !NSThread.isMainThread() else {
        fatalError("await will block main thread")
    }
    let semaphore = dispatch_semaphore_create(0)
    
    futures.forEach { future in
        future.finally {
            dispatch_semaphore_signal(semaphore)
        }
    }
    
    futures.forEach { _ in
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER)
    }
}<commit_msg>sickened down the implementation of asynchronous execution contexts and removed the versions with the delay parameter - this seems only relevant for testing until now<commit_after>//
//  Executor.swift
//  Futuristics
//
//  Created by <NAME> on 03/08/2015.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import Foundation

public func onMainQueue<T, U>(closure: T throws -> U) -> (T -> Future<U>) {
    let mainQueue = dispatch_get_main_queue()
    return onQueue(mainQueue)(closure: closure)
}


public func onBackgroundQueue<T, U>(closure: T throws -> U) -> (T -> Future<U>) {
    let aBackgroundQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)
    return onQueue(aBackgroundQueue)(closure: closure)
}

public func onQueue<T, U>(queue: dispatch_queue_t) -> (closure: T throws -> U) -> (T -> Future<U>) {
    return { [queue] (closure: T throws -> U) in
        return onQueue(queue, closure: closure)
    }
}

private func onQueue<T, U>(queue: dispatch_queue_t, closure: T throws -> U) -> (T -> Future<U>) {
    return { (parameter: T) in
        let promise = Promise<U>()
        if queue === dispatch_get_main_queue() && NSThread.isMainThread() {
            defer { promise.ensureResolution() }
            promise.resolve { try closure(parameter) }
        } else {
            dispatch_async(queue) {
                defer { promise.ensureResolution() }
                promise.resolve { try closure(parameter) }
            }
        }
        return promise.future
    }
}

public func await<T>(futures: Future<T> ...) {
    await(futures)
}

public func await<T>(futures: [Future<T>]) {
    guard !NSThread.isMainThread() else {
        fatalError("await will block main thread")
    }
    let semaphore = dispatch_semaphore_create(0)
    
    futures.forEach { future in
        future.finally {
            dispatch_semaphore_signal(semaphore)
        }
    }
    
    futures.forEach { _ in
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER)
    }
}
{'fraction_non_alphanumeric': 0.125196673409755, 'fraction_numerical': 0.006743088334457181, 'mean_word_length': 2.641571194762684, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 69, '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': '6241462', 'n_tokens_mistral': 1417, 'n_tokens_neox': 1374, 'n_words': 335}