<commit_before>/*
 * Copyright 2013 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/** Memoize an async function. **/
function amemo(f) {
  var values;
  var waiters;

  return function(ret) {
    if ( values ) { ret.apply(null, values); return; }

    var first = ! waiters;

    if ( first ) waiters = [];

    waiters.push(ret);

    if ( first ) {
      f(function() {
	values = arguments;
	for (var i = 0 ; i < waiters.length; i++) {
	  waiters[i].apply(null, values);
	}
        waiters = [];
      });
    }
  };
}


/** Async Compose (like Function.prototype.O, but for async functions **/<commit_msg>Added new afunc methods.<commit_after>/*
 * Copyright 2013 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


/** NOP afunc. **/
function anop(ret) { ret && ret(undefined); }


/** afunc log. **/
function alog(val) {
  return function (ret) {
    console.log(val);
    ret && ret.apply(this, [].shift.call(arguments));
  };
}


/** Create an afunc which always returns the supplied constant value. **/
function aconstant(v) { return function(ret) { ret && ret(anop, v); } }

/** Execute the supplied afunc N times. **/
function arepeat(n, afunc) {
  return function(ret) {
    var a = [];
    for ( var i = 0 ; i < arguments.length ; i++ ) a[i] = arguments[i];

    var g = function() {
      if ( n-- == 1 ) { a[0] = ret; afunc.apply(this, a); return; };
      afunc.apply(this, a);
    };

    a[0] = g;
    g.apply(this, a);
  };
}

/** Time an afunc. **/
function atime(str, afunc) {
  return function(ret) {
    console.time(str);
    var a = arguments;
    var args = [function() {
      console.timeEnd(str);
      ret && ret.apply(this, [].shift.call(a));
    }];
    for ( var i = 1 ; i < a.length ; i++ ) args[i] = a[i];
    afunc.apply(this, args);
  };
}


/** Create a future value. **/
function afuture() {
  var set     = false;
  var values  = undefined;
  var waiters = [];

  return {
    set: function() {
      values = arguments;
      set = true;
      for (var i = 0 ; i < waiters.length; i++) {
        waiters[i].apply(null, values);
      }
      waiters = undefined;
    },

    get: function(ret) {
      if ( set ) { ret.apply(null, values); return; }
      waiters.push(ret);
    }
  };
};


/** Memoize an async function. **/
function amemo(f) {
  var values;
  var waiters;

  return function(ret) {
    if ( values ) { ret.apply(null, values); return; }

    var first = ! waiters;

    if ( first ) waiters = [];

    waiters.push(ret);

    if ( first ) {
      f(function() {
	values = arguments;
	for (var i = 0 ; i < waiters.length; i++) {
	  waiters[i].apply(null, values);
	}
        waiters = [];
      });
    }
  };
}


/** Async Compose (like Function.prototype.O, but for async functions **/
{'fraction_non_alphanumeric': 0.14322916666666666, 'fraction_numerical': 0.00625, 'mean_word_length': 2.689721421709894, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6353814', 'n_tokens_mistral': 1248, 'n_tokens_neox': 1206, 'n_words': 412}