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

import Foundation

public func map<F: FutureType, T>(fx: F, f: F.SuccessType -> T)(ec: ExecutionContextType) -> Future<T, F.ErrorType> {
    return transform(fx, f, id)(ec: ec)
}

public func transform<F: FutureType, S, E>(fx: F, s: F.SuccessType -> S, e: F.ErrorType -> E)(ec: ExecutionContextType) -> Future<S, E> {
    let p = Promise<S, E>()
    fx.onComplete(ec) {
        switch $0 as! Result<F.SuccessType, F.ErrorType> {
        case .Success(let box):
            p.success(s(box.value))
        case .Error(let box):
            p.error(e(box.value))
        }
    }
    return p.future
}

public func flatMap<F: FutureType, T>(fx: F, f: F.SuccessType -> Future<T, F.ErrorType>)(ec: ExecutionContextType) -> Future<T, F.ErrorType> {
    let p = Promise<T, F.ErrorType>()
    fx.onComplete(ec) {
        switch $0 as! Result<F.SuccessType, F.ErrorType> {
        case .Success(let box):
            p.completeWith(f(box.value))
        case .Error(let box):
            p.complete(Result(box.value))
        }
    }
    return p.future
}

public func filter<F: FutureType>(fx: F, p: F.SuccessType -> Bool)(ec: ExecutionContextType) -> Future<F.SuccessType?, F.ErrorType> {
    return map(fx) { x in p(x) ? x : nil }(ec: ec)
}

public func zip<F: FutureType, T: FutureType where F.ErrorType == T.ErrorType>(fa: F, fb: T)(ec: ExecutionContextType) -> Future<(F.SuccessType, T.SuccessType), F.ErrorType> {
    return flatMap(fa) { a in
        map(fb) { b in
            (a, b)
        }(ec: ec)
    }(ec: ec)
}

public func reduce<S: SequenceType, T where S.Generator.Element: FutureType>(fxs: S, initial: T, combine: (T, S.Generator.Element.SuccessType) -> T)(ec: ExecutionContextType) -> Future<T, S.Generator.Element.ErrorType> {
    return reduce(fxs, Future(Result(initial))) { acc, futureValue in
        flatMap(futureValue) { value in
            map(acc) { combine($0, value)}(ec: ec)
        }(ec: ec)
    }
}

public func traverse<S: SequenceType, F: FutureType>(xs: S, f: S.Generator.Element -> F)(ec: ExecutionContextType) -> Future<[F.SuccessType], F.ErrorType> {
    return reduce(map(xs, f), []) { $0 + [$1] }(ec: ec)
}

public func sequence<S: SequenceType where S.Generator.Element: FutureType>(fxs: S)(ec: ExecutionContextType) -> Future<[S.Generator.Element.SuccessType], S.Generator.Element.ErrorType> {
    return traverse(fxs, id)(ec: ec)
}

public func recover<F: FutureType>(fx: F, r: F.ErrorType -> F.SuccessType)(ec: ExecutionContextType) -> Future<F.SuccessType, F.ErrorType> {
    let p = Promise<F.SuccessType, F.ErrorType>()
    fx.onError(ec) { p.success(r($0)) }
    return p.future
}

public func recoverWith<F: FutureType>(fx: F, r: F.ErrorType -> Future<F.SuccessType, F.ErrorType>)(ec: ExecutionContextType) -> Future<F.SuccessType, F.ErrorType> {
    let p = Promise<F.SuccessType, F.ErrorType>()
    fx.onError(ec) { p.completeWith(r($0)) }
    return p.future
}

public func toDeferred<F: FutureType>(fx: F, r: F.ErrorType -> F.SuccessType)(ec: ExecutionContextType) -> Deferred<F.SuccessType> {
    let p = PurePromise<F.SuccessType>()
    fx.onComplete(ec) {<commit_msg>Fix recover and recoverWith implementations<commit_after>//
//  FutureTypeFunctions.swift
//  PureFutures
//
//  Created by <NAME> on 2/24/15.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

import Foundation

public func map<F: FutureType, T>(fx: F, f: F.SuccessType -> T)(ec: ExecutionContextType) -> Future<T, F.ErrorType> {
    return transform(fx, f, id)(ec: ec)
}

public func transform<F: FutureType, S, E>(fx: F, s: F.SuccessType -> S, e: F.ErrorType -> E)(ec: ExecutionContextType) -> Future<S, E> {
    let p = Promise<S, E>()
    fx.onComplete(ec) {
        switch $0 as! Result<F.SuccessType, F.ErrorType> {
        case .Success(let box):
            p.success(s(box.value))
        case .Error(let box):
            p.error(e(box.value))
        }
    }
    return p.future
}

public func flatMap<F: FutureType, T>(fx: F, f: F.SuccessType -> Future<T, F.ErrorType>)(ec: ExecutionContextType) -> Future<T, F.ErrorType> {
    let p = Promise<T, F.ErrorType>()
    fx.onComplete(ec) {
        switch $0 as! Result<F.SuccessType, F.ErrorType> {
        case .Success(let box):
            p.completeWith(f(box.value))
        case .Error(let box):
            p.complete(Result(box.value))
        }
    }
    return p.future
}

public func filter<F: FutureType>(fx: F, p: F.SuccessType -> Bool)(ec: ExecutionContextType) -> Future<F.SuccessType?, F.ErrorType> {
    return map(fx) { x in p(x) ? x : nil }(ec: ec)
}

public func zip<F: FutureType, T: FutureType where F.ErrorType == T.ErrorType>(fa: F, fb: T)(ec: ExecutionContextType) -> Future<(F.SuccessType, T.SuccessType), F.ErrorType> {
    return flatMap(fa) { a in
        map(fb) { b in
            (a, b)
        }(ec: ec)
    }(ec: ec)
}

public func reduce<S: SequenceType, T where S.Generator.Element: FutureType>(fxs: S, initial: T, combine: (T, S.Generator.Element.SuccessType) -> T)(ec: ExecutionContextType) -> Future<T, S.Generator.Element.ErrorType> {
    return reduce(fxs, Future(Result(initial))) { acc, futureValue in
        flatMap(futureValue) { value in
            map(acc) { combine($0, value)}(ec: ec)
        }(ec: ec)
    }
}

public func traverse<S: SequenceType, F: FutureType>(xs: S, f: S.Generator.Element -> F)(ec: ExecutionContextType) -> Future<[F.SuccessType], F.ErrorType> {
    return reduce(map(xs, f), []) { $0 + [$1] }(ec: ec)
}

public func sequence<S: SequenceType where S.Generator.Element: FutureType>(fxs: S)(ec: ExecutionContextType) -> Future<[S.Generator.Element.SuccessType], S.Generator.Element.ErrorType> {
    return traverse(fxs, id)(ec: ec)
}

public func recover<F: FutureType>(fx: F, r: F.ErrorType -> F.SuccessType)(ec: ExecutionContextType) -> Future<F.SuccessType, F.ErrorType> {
    let p = Promise<F.SuccessType, F.ErrorType>()
    fx.onComplete(ec) {
        switch $0 as! Result<F.SuccessType, F.ErrorType> {
        case .Success(let box):
            p.success(box.value)
        case .Error(let box):
            p.success(r(box.value))
        }
    }
    return p.future
}

public func recoverWith<F: FutureType>(fx: F, r: F.ErrorType -> Future<F.SuccessType, F.ErrorType>)(ec: ExecutionContextType) -> Future<F.SuccessType, F.ErrorType> {
    let p = Promise<F.SuccessType, F.ErrorType>()
    fx.onComplete(ec) {
        switch $0 as! Result<F.SuccessType, F.ErrorType> {
        case .Success(let box):
            p.success(box.value)
        case .Error(let box):
            p.completeWith(r(box.value))
        }
    }
    return p.future
}

public func toDeferred<F: FutureType>(fx: F, r: F.ErrorType -> F.SuccessType)(ec: ExecutionContextType) -> Deferred<F.SuccessType> {
    let p = PurePromise<F.SuccessType>()
    fx.onComplete(ec) {
{'fraction_non_alphanumeric': 0.1623096811261132, 'fraction_numerical': 0.004596380350474002, 'mean_word_length': 3.1669658886894076, 'pattern_counts': {'":': 0, '<': 71, '<?xml version=': 0, '>': 113, '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': '3295361', 'n_tokens_mistral': 2463, 'n_tokens_neox': 2407, 'n_words': 574}