<commit_before>//
//  CollectionsExtension.swift
//  EZSwiftExtensions
//
//  Created by <NAME> on 3/5/17.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import Foundation

extension Collection {

#if os(iOS)
    // EZSE : A parralelized map for collections, operation is non blocking
    public func parallelizedMap<R>(_ each: @escaping (Self.Iterator.Element) -> R) -> [R?] {
        let indices = indicesArray()
        var res = [R?](repeating: nil, count: indices.count)
        
        let queue = OperationQueue()
        let operations = indices.enumerated().map { index, elementIndex in
            return BlockOperation {
                res[index] = each(self[elementIndex])
            }
        }
        queue.addOperations(operations, waitUntilFinished: true)
        
        return res
    }

    
    /// EZSE : Helper method to get an array of collection indices
    private func indicesArray() -> [Self.Index] {
        var indicesArray: [Self.Index] = []
        var nextIndex = startIndex
        while nextIndex != endIndex {
            indicesArray.append(nextIndex)
            nextIndex = index(after: nextIndex)<commit_msg>Fixing parallelized map to work with swift 4 syntax.<commit_after>//
//  CollectionsExtension.swift
//  EZSwiftExtensions
//
//  Created by <NAME> on 3/5/17.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import Foundation

extension Collection {

#if os(iOS)
    // EZSE : A parralelized map for collections, operation is non blocking
    public func parallelizedMap<R>(_ each: @escaping (Self.Iterator.Element) -> R) -> [R?] {
        let indices = indicesArray()
        var res = [R?](repeating: nil, count: indices.count)
        
        let queue = OperationQueue()
        let operations = indices.enumerated().map { index in
            return BlockOperation {
                res[index.offset] = each(self[index.element])
            }
        }
        queue.addOperations(operations, waitUntilFinished: true)
        
        return res
    }

    
    /// EZSE : Helper method to get an array of collection indices
    private func indicesArray() -> [Self.Index] {
        var indicesArray: [Self.Index] = []
        var nextIndex = startIndex
        while nextIndex != endIndex {
            indicesArray.append(nextIndex)
            nextIndex = index(after: nextIndex)
{'fraction_non_alphanumeric': 0.10548702679710761, 'fraction_numerical': 0.007230965546575925, 'mean_word_length': 2.2531120331950207, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, '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': '4103332', 'n_tokens_mistral': 670, 'n_tokens_neox': 645, 'n_words': 192}