<commit_before>import Foundation

public func Success<T>(value: T) -> PromiseResult<T> {
	return PromiseResult(value: value, error: nil);
}

public func Failure<T>(error: NSError) -> PromiseResult<T> {
	return PromiseResult(value: nil, error: error);
}

public func future<T>(executionContext context: ExecutionContext = DefaultFutureExecutionContext(), action: ()->PromiseResult<T>) -> Future<T> {
	let promise = Promise<T>();
	context.run {
		let result = action()
		if let value = result.value {
			promise.success(value)
		} else if let error = result.error {
			promise.failure(error);
		}
	}
	return promise.future
}

public func future<T: Any>(executionContext context: ExecutionContext = DefaultFutureExecutionContext(), action: @autoclosure ()->(T)) -> Future<T> {
	return future(executionContext: DefaultFutureExecutionContext(), { return Success(action()) })
}

public class Future<T: Any> {
	private var successCallbacks:[FutureCallback<T>] = []
	private var failureCallbacks:[FutureCallback<NSError>] = []
	private var result:(value: T?, error: NSError?)?
	private let sync = Synchronizer()
	
	init() {}
	
	func complete(promiseResult: PromiseResult<T>) {
		result = (promiseResult.value, promiseResult.error)
		self.maybeRunCallbacks()
	}
	
	public func collect<U: Any>(executionContext context: ExecutionContext = DefaultFutureExecutionContext(), action: (T)->PromiseResult<U>) -> Future<U> {
		let promise = Promise<U>();
		
		self.onSuccess(executionContext: context) { thisResult in
			let result = action(thisResult)
			if let value = result.value {
				promise.success(value)
			} else if let error = result.error {
				promise.failure(error)
			}
		}
		
		return promise.future
	}
	
	public func collect<U: Any>(executionContext context: ExecutionContext = DefaultFutureExecutionContext(), action: (T)->U) -> Future<U> {
		return collect(executionContext: context, action: { thisResult in return Success(action(thisResult)) } )
	}
	
	public func isComplete() -> Bool { return result != nil ? true : false }
	
	public func value() -> T? { return result?.value }
	
	public func onSuccess(callback:(T)->()) { self.onSuccess(executionContext: MainExecutionContext(), callback) }
	
	public func onFailure(callback:(NSError)->()) { self.onFailure(executionContext: MainExecutionContext(), callback) }
	
	public func onSuccess(#executionContext: ExecutionContext, callback:(T)->()) {
		let successCallback = FutureCallback(callback: callback, executionContext: executionContext)
		sync.run {
			self.successCallbacks.append(successCallback)
		}
		maybeRunCallbacks()
	}
	
	public func onFailure(#executionContext: ExecutionContext, callback:(NSError)->()) {
		let failureCallback = FutureCallback(callback: callback, executionContext: executionContext)
		sync.run {
			self.failureCallbacks.append(failureCallback)
		}
		maybeRunCallbacks()
	}
	
	private func maybeRunCallbacks() {
		if let futureResult = result {
			let callbacks = clearAndReturnCallbacks()
			if let error = futureResult.error {
				RunFutureCallbacks(callbacks.failureCallbacks, error)<commit_msg>update for iOS 8.3 beta 1 compiler<commit_after>import Foundation

public func Success<T>(value: T) -> PromiseResult<T> {
	return PromiseResult(value: value, error: nil);
}

public func Failure<T>(error: NSError) -> PromiseResult<T> {
	return PromiseResult(value: nil, error: error);
}

public func future<T>(executionContext context: ExecutionContext = DefaultFutureExecutionContext(), action: ()->PromiseResult<T>) -> Future<T> {
	let promise = Promise<T>();
	context.run {
		let result = action()
		if let value = result.value {
			promise.success(value)
		} else if let error = result.error {
			promise.failure(error);
		}
	}
	return promise.future
}

public func future<T: Any>(executionContext context: ExecutionContext = DefaultFutureExecutionContext(), action: ()->(T)) -> Future<T> {
	return future(executionContext: DefaultFutureExecutionContext(), { return Success(action()) })
}

public class Future<T: Any> {
	private var successCallbacks:[FutureCallback<T>] = []
	private var failureCallbacks:[FutureCallback<NSError>] = []
	private var result:(value: T?, error: NSError?)?
	private let sync = Synchronizer()
	
	init() {}
	
	func complete(promiseResult: PromiseResult<T>) {
		result = (promiseResult.value, promiseResult.error)
		self.maybeRunCallbacks()
	}
	
	public func collect<U: Any>(executionContext context: ExecutionContext = DefaultFutureExecutionContext(), action: (T)->PromiseResult<U>) -> Future<U> {
		let promise = Promise<U>();
		
		self.onSuccess(executionContext: context) { thisResult in
			let result = action(thisResult)
			if let value = result.value {
				promise.success(value)
			} else if let error = result.error {
				promise.failure(error)
			}
		}
		
		return promise.future
	}
	
	public func collect<U: Any>(executionContext context: ExecutionContext = DefaultFutureExecutionContext(), action: (T)->U) -> Future<U> {
		return collect(executionContext: context, action: { thisResult in return Success(action(thisResult)) } )
	}
	
	public func isComplete() -> Bool { return result != nil ? true : false }
	
	public func value() -> T? { return result?.value }
	
	public func onSuccess(callback:(T)->()) { self.onSuccess(executionContext: MainExecutionContext(), callback: callback) }
	
	public func onFailure(callback:(NSError)->()) { self.onFailure(executionContext: MainExecutionContext(), callback: callback) }
	
	public func onSuccess(#executionContext: ExecutionContext, callback:(T)->()) {
		let successCallback = FutureCallback(callback: callback, executionContext: executionContext)
		sync.run {
			self.successCallbacks.append(successCallback)
		}
		maybeRunCallbacks()
	}
	
	public func onFailure(#executionContext: ExecutionContext, callback:(NSError)->()) {
		let failureCallback = FutureCallback(callback: callback, executionContext: executionContext)
		sync.run {
			self.failureCallbacks.append(failureCallback)
		}
		maybeRunCallbacks()
	}
	
	private func maybeRunCallbacks() {
		if let futureResult = result {
			let callbacks = clearAndReturnCallbacks()
			if let error = futureResult.error {
				RunFutureCallbacks(callbacks.failureCallbacks, error)
{'fraction_non_alphanumeric': 0.12564599483204134, 'fraction_numerical': 0.00048449612403100775, 'mean_word_length': 5.312945973496432, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 75, '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': '734700', 'n_tokens_mistral': 1934, 'n_tokens_neox': 1731, 'n_words': 440}