<commit_before>//
//  Deferred.swift
//  PureFutures
//
//  Created by <NAME> on 2/11/15.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

import Foundation

public func deferred<T>(f: @autoclosure () -> T) -> Deferred<T> {
    return deferred(f as () -> T)
}

public func deferred<T>(f: () -> T) -> Deferred<T> {
    let p = Promise<T>()
    
    p.complete(f())
    
    return p.deferred
}

public final class Deferred<T>: DeferredType {
    
    typealias Element = T
    
    // MARK:- Type declarations
    
    public typealias Callback = T -> Void
    
    // MARK:- Private properties
    
    private var callbacks: [Callback] = []
    
    // MARK:- Internal properties
    
    internal var result: T? {
        didSet {
            assert(oldValue == nil, "Cannot complete Deferred more than once")
            assert(result != nil, "Result can't be nil")
            
            for callback in callbacks {
                callback(result!)
            }
            
            callbacks.removeAll()
        }
    }
    
    // MARK:- Public properties
    
    public var value: T? {
        return result
    }
    
    // MARK:- Initialization
    
    internal init() {
        
    }
    
    public init(_ f: () -> T) {
        dispatch_async(dispatch_get_global_queue(0, 0)) {
            self.result = f()
        }
    }
    
    public convenience init(_ f: @autoclosure () -> T) {
        self.init(f)
    }
    
    // MARK:- Public methods
    
    // MARK: Class methods
    
    public class func completed(value: T) -> Deferred {
        let d = Deferred()
        
        d.result = value
        
        return d
    }
    
    // MARK: Instance methods
    
    public func onComplete(c: Callback) -> Deferred {<commit_msg>Remove deferred functions & Fix initializer<commit_after>//
//  Deferred.swift
//  PureFutures
//
//  Created by <NAME> on 2/11/15.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

import Foundation

public final class Deferred<T>: DeferredType {
    
    typealias Element = T
    
    // MARK:- Type declarations
    
    public typealias Callback = T -> Void
    
    // MARK:- Private properties
    
    private var callbacks: [Callback] = []
    
    // MARK:- Internal properties
    
    internal var result: T? {
        didSet {
            assert(oldValue == nil, "Cannot complete Deferred more than once")
            assert(result != nil, "Result can't be nil")
            
            for callback in callbacks {
                callback(result!)
            }
            
            callbacks.removeAll()
        }
    }
    
    // MARK:- Public properties
    
    public var value: T? {
        return result
    }
    
    // MARK:- Initialization
    
    internal init() {
        
    }
    
    public init(_ f: () -> T) {
        dispatch_async(dispatch_get_global_queue(0, 0)) {
            self.result = f()
        }
    }
    
    public convenience init(_ f: @autoclosure () -> T) {
        self.init(f as () -> T)
    }
    
    // MARK:- Public methods
    
    // MARK: Class methods
    
    public class func completed(value: T) -> Deferred {
        let d = Deferred()
        
        d.result = value
        
        return d
    }
    
    // MARK: Instance methods
    
    public func onComplete(c: Callback) -> Deferred {
{'fraction_non_alphanumeric': 0.11224184375935349, 'fraction_numerical': 0.006584854833882071, 'mean_word_length': 1.501497005988024, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 30, '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': '485732', 'n_tokens_mistral': 1023, 'n_tokens_neox': 975, 'n_words': 275}