<commit_before>/* 'van.js' (vanguard), because I load this module in advance of all others
 * Ubiquitious functional-style support functions
 * no dependencies
 */

var van = {};

/* Currying functions */
/* Example uses found in example/curring.js */

van.fixN = fixN = function(f,a,n){
  n=n||0;
  return function(b){
    args=Array.prototype.slice.call(arguments); // more general now
    args.splice(n,0,a);
    return f.apply(null,args);
  };
};

van.die = die = function(r){
  return Math.floor(Math.random()*r);
};

van.nullArray = nullArray = function(n){
  var temp = [];
  while(n-- > 0)temp.push(null);
  return temp;
};

van.fix1 = fix1 = function(f,a){
  return function(b){
    return f(a,b);
  };
};

van.fix2 = fix2 = function(f,b){
  return function(a){
    return f(a,b);
  };
};

/* Example usage in example/arrays.js */

van.negate = negate = function(p){
  return function(x){
    return !p(x);
  };
};

/* Example usage found in example/compose.js */

van.compose = compose = function(f,g){
  return function(x){
    return g(f(x));
  };
};

van.sum = sum = function(A){
  return A.reduce(function(a,b){
    return a+b;
  });
};

van.range = range = function(a,b){
  var temp=[];
  b = b || a;
  if(a===b)a=0; // beware, if a and b are equal, it indexes from 0
    // maybe that's not what you expect
  while(a<=b)temp.push(a++);
  return temp;
};

van.carte = carte = function(f,A,B){
  return A.map(function(a){
    return B.map(fix1(f,a));
  });
};

van.carteSquare = carteSquare = function(f,A){
  return A.map(function(a,i,z){
    return z.map(fix1(f,a));
  });
};

van.flatten = flatten = function(AA){
  return AA.reduce(function(A,B){
    return A.concat(B);
  });
};

/* Shuffle an array */

van.shuffle=shuffle=function(A){
  var i=A.length;
  while(i){
    var r=die(i--);
    var temp=A[i];
    A[i]=A[r];
    A[r]=temp;
  }
  return A;
};

van.cut=cut=function(A,n){<commit_msg>added some explanations<commit_after>/* 'van.js' (vanguard), because I load this module in advance of all others
 * Ubiquitious functional-style support functions
 * no dependencies
 */

var van = {};

/* Currying functions :: which take a function and some of its parameters as arguments
  and return a new function which calls the original with some of its arguments fixed.
  Using fix1 or fix2, we can 'fix' either argument of a binary function,
  and produce a new unary function.

  This is useful for when you want to map a function over an array.

  Use fixN when you have a function with more than two arguments,
  simply provide the new argument, and the index (from zero) of the arg you replace  */

/* Example uses found in example/currying.js */

van.fixN = fixN = function(f,a,n){
  n=n||0;
  return function(b){
    args=Array.prototype.slice.call(arguments); // more general now
    args.splice(n,0,a);
    return f.apply(null,args);
  };
};

van.fix1 = fix1 = function(f,a){
  return function(b){
    return f(a,b);
  };
};

van.fix2 = fix2 = function(f,b){
  return function(a){
    return f(a,b);
  };
};

/* function negation is useful when you intend to filter an array.
  negating the 'predicate' function you pass to the filter method,
  will return the 'set complement' of the original subset */

/* Example usage in example/arrays.js */

van.negate = negate = function(p){
  return function(x){
    return !p(x);
  };
};

/* Like currying or function negation, compose returns a new function
  which returns the result of the first function's output
  passed as an argument to the second.

  Use compose with the 'reduce' method to chain an arbitrary number of functions */

/* Example usage found in example/compose.js */

van.compose = compose = function(f,g){
  return function(x){
    return g(f(x));
  };
};

/* Compared to the previous functions, this one is rather boring
  It returns the sum of an array, an operation common enough (used within this lib)
  that I decided to include it here.
  Similarly, you could make a 'product' function by using multiplication */

/* See usage in van.stdDev, found below */

van.sum = sum = function(A){
  return A.reduce(function(a,b){
    return a+b;
  });
};

/* Array functions :: starting with 'range'
  range is a variadic function, taking zero, one, or two arguments.
  given no arguments, it returns [0]
  given one, it produces an inclusive range from zero to its argument.
  given two, it returns a range from its first to its second argument. */

/* example usage ALL OVER THE PLACE, range is a staple when working with arrays */

van.range = range = function(a,b){
  var temp=[];
  b = b || a;
  if(a===b)a=0; // beware, if a and b are equal, it indexes from 0
    // maybe that's not what you expect
  while(a<=b)temp.push(a++);
  return temp;
};

/* nullArray, for those times when you don't care about what's in an array
  instantiate an array of the appropriate length
  use map to fill it, or use it strictly for its side effects
  guaranteed not to exceed its bounds */

van.nullArray = nullArray = function(n){
  var temp = [];
  while(n-- > 0)temp.push(null);
  return temp;
};

/* Flatten :: accepts an array of arrays, and returns a new array
  the resulting array is the concatenation of all the nested arrays
  meaning the result will be one dimension smaller.
  A two dimensional array becomes a one dimensional array containing
  all the elements of the original data.
  If you want to flatten an n-dimensional array, you can just use recursion */

/* example usage found in example/arrays.js */

van.flatten = flatten = function(AA){
  return AA.reduce(function(A,B){
    return A.concat(B);
  });
};

/* Cartesian products!! take a binary function and two arrays
  return a two dimensional array containing the results of the binary function
  for every possible combination of arguments */

/* example usage found in example/array.js */

van.carte = carte = function(f,A,B){
  return A.map(function(a){
    return B.map(fix1(f,a));
  });
};

/* As above, except that it only takes a single array as an argument
  the two dimensional array that it returns is the product of that array
  applied against itself. */

van.carteSquare = carteSquare = function(f,A){
  return A.map(function(a,i,z){
    return z.map(fix1(f,a));
  });
};

/* Aleatoric functions (randomness) */

/* return a number */

van.die = die = function(r){
  return Math.floor(Math.random()*r);
};

van.shuffle=shuffle=function(A){
  var i=A.length;
  while(i){
    var r=die(i--);
    var temp=A[i];
    A[i]=A[r];
    A[r]=temp;
  }
  return A;
};

van.cut=cut=function(A,n){
{'fraction_non_alphanumeric': 0.13194020744356313, 'fraction_numerical': 0.003813300793166565, 'mean_word_length': 3.442411924119241, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '6734206', 'n_tokens_mistral': 2189, 'n_tokens_neox': 2156, 'n_words': 763}