<commit_before>﻿/// <reference path="../_definitions.d.ts" />

export interface PromiseTaskExecutor<T> {
    (): Promise<T>
    (...args: any[]): Promise<T>
}

export interface PromiseListIterator<T, U> {
    (item: T, index: number, list: T[]): Promise<U>;
}

export interface PromiseReduceIterator<T> {
    (memo: T, item: T): Promise<T>;
}


export function each<T>(array: T[], iterator: PromiseListIterator<T, any>): Promise<void> {
    var promises = array.map(iterator);
    return Promise.all(promises).then(() => { return; });
}

export function eachSeries<T>(array: T[], iterator: PromiseListIterator<T, any>): Promise<void> {
    var p = Promise.resolve(),
        i = 0, len = array.length;

    for (; i < len; i++) {
        p = p.then(iterator.bind(null, array[i], i, array));
    }

    return p.then(() => { return; });
}


export function map<T, U>(array: T[], iterator: PromiseListIterator<T, U>): Promise<U[]> {
    var promises = array.map(iterator);
    return Promise.all(promises);
}

export function mapSeries<T, U>(array: T[], iterator: PromiseListIterator<T, U>): Promise<U[]> {
    var results: U[] = [];

    function mapper(item: T, index: number): Promise<void> {
        return iterator(item, index, array).then(result => {
            results[index] = result;
        });
    }

    return eachSeries(array, mapper).then(() => results);
}


export function filter<T>(array: T[], iterator: PromiseListIterator<T, boolean>): Promise<T[]> {
    var results: T[] = [],

        promises = array.map((value, index, list) => iterator(value, index, list).then(include => {
            if (include) {
                results.push(value);<commit_msg>Fix issues in eachSeries<commit_after>﻿/// <reference path="../_definitions.d.ts" />

export interface PromiseTaskExecutor<T> {
    (): Promise<T>
    (...args: any[]): Promise<T>
}

export interface PromiseListIterator<T, U> {
    (item: T, index: number, list: T[]): Promise<U>;
}

export interface PromiseReduceIterator<T> {
    (memo: T, item: T): Promise<T>;
}


export function each<T>(array: T[], iterator: PromiseListIterator<T, any>): Promise<void> {
    var promises = array.map(iterator);
    return Promise.all(promises).then(() => { return; });
}

export function eachSeries<T>(array: T[], iterator: PromiseListIterator<T, any>): Promise<void> {
    return new Promise<void>((resolve: any, reject) => {
        var p = Promise.resolve(),
            i = 0, len = array.length;

        function partial(value: T, index: number): () => Promise<any> {
            return () => iterator(value, index, array);
        }

        for (; i < len; i++) {
            p = p.then(partial(array[i], i));
        }

        return p.then(() => resolve(), reject);
    });
}


export function map<T, U>(array: T[], iterator: PromiseListIterator<T, U>): Promise<U[]> {
    var promises = array.map(iterator);
    return Promise.all(promises);
}

export function mapSeries<T, U>(array: T[], iterator: PromiseListIterator<T, U>): Promise<U[]> {
    var results: U[] = [];

    function mapper(item: T, index: number): Promise<void> {
        return iterator(item, index, array).then(result => {
            results[index] = result;
        });
    }

    return eachSeries(array, mapper).then(() => results);
}


export function filter<T>(array: T[], iterator: PromiseListIterator<T, boolean>): Promise<T[]> {
    var results: T[] = [],

        promises = array.map((value, index, list) => iterator(value, index, list).then(include => {
            if (include) {
                results.push(value);
{'fraction_non_alphanumeric': 0.18169092945128779, 'fraction_numerical': 0.0005599104143337066, 'mean_word_length': 3.310012062726176, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 68, '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': '5242967', 'n_tokens_mistral': 1126, 'n_tokens_neox': 1112, 'n_words': 282}