<commit_before>/*jshint eqeqeq:true, proto:true, laxcomma:true, undef:true, node:true, expr: true, boss:true*/

var slice = Array.prototype.slice;

module.exports = function first (f) {
  var functions = [{f: f}], next = function () {
    var f, p = [];

    // get the first function
    if (f = functions.shift()) {
      p.push(f);
    } else {
      return;
    }

    // keep getting functions with the p flag set (parallelize)
    while(functions[0] && functions[0].p) {
      p.push(functions.shift());
    }

    // if there more than one function to run in parallel
    if (p.length > 1) {
      var  i = p.length
        , n = i
        , results = []
        // bucket creates a callback, that counts how often it was called
        // and will in turn fire a callback if a certain threshold is reached.
        // It also makes sure "results" (callback arguments) are kept in
        // order.
        , bucket = function (store) {
          
        return function () {
          results[store] = (slice.apply(arguments));
          if (!--n) {
            // thrshold was reached, call next!
            next.apply(this, results); 
          }
        };

      };

      while(i--) {
        // now run all functions simultaniously, giving them their respective
        // bucket and the arguments that where given to this call to next.
        p[i].f.apply(bucket(i), arguments);
      }

    } else {
      // if there's only one, simply run it, giving it next as this
      // and the arguments given to this call to next as arguments.
      p[0].f.apply(next, arguments);
    }
  };

  var deferred = {
    then: function(f) {
      functions.push({f: f});
      return this;
    },
    whilst: function(f) {
      functions.push({f: f, p: true});
      return this;
    }
  };

  process.nextTick(next);

  return deferred;
};<commit_msg>Cleaned up the architecture a bit<commit_after>/*jshint eqeqeq:true, proto:true, laxcomma:true, undef:true, node:true, expr: true*/

var slice = Array.prototype.slice, isArray = require('util').isArray;

module.exports = function first (f) {
  var functions = [f], next = function () {
    var f = functions.shift(), p = [];

    if (!f) return;

    // if there more than one function to run in parallel
    if (isArray(f)) {
      var i = f.length
        , n = i
        , results = []
        // bucket creates a callback, that counts how often it was called
        // and will in turn fire a callback if a certain threshold is reached.
        // It also makes sure "results" (callback arguments) are kept in
        // order.
        , bucket = function (store) {
          
        return function () {
          results[store] = (slice.apply(arguments));
          if (!--n) {
            // thrshold was reached, call next!
            next.apply(this, results); 
          }
        };

      };

      while(i--) {
        // now run all functions simultaniously, giving them their respective
        // bucket and the arguments that where given to this call to next.
        f[i].apply(bucket(i), arguments);
      }

    } else {
      // if there's only one, simply run it, giving it next as this
      // and the arguments given to this call to next as arguments.
      f.apply(next, arguments);
    }
  };

  var deferred = {
    then: function(f) {
      functions.push(f);
      return this;
    },
    whilst: function(f) {
      var last = functions.length-1;
      if (isArray (functions[last])) {
        functions[last].push(f);
      } else {
        functions[last] = [functions[last], f];
      }
      return this;
    }
  };

  process.nextTick(next);

  return deferred;
};
{'fraction_non_alphanumeric': 0.12342981977061715, 'fraction_numerical': 0.001365374112506827, 'mean_word_length': 1.9588045234248788, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '5951067', 'n_tokens_mistral': 1080, 'n_tokens_neox': 1056, 'n_words': 364}