<commit_before><?php

namespace collections;

/**
 * Reduces $collection to a value which is the $accumulator result of running each
 * element in $collection thru $iteratee, where each successive invocation is supplied
 * the return value of the previous.
 *
 * If $accumulator is not given, the first element of $collection is used as the
 * initial value.
 *
 * The $iteratee is invoked with four arguments:
 * ($accumulator, $value, $index|$key, $collection).
 *
 ** __::reduce([1, 2], function ($sum, $number) {
 **     return $sum + $number;
 ** }, 0);
 ** // >> 3
 *
 ** $a = [
 **     ['state' => 'IN', 'city' => 'Indianapolis', 'object' => 'School bus'],
 **     ['state' => 'IN', 'city' => 'Indianapolis', 'object' => 'Manhole'],
 **     ['state' => 'IN', 'city' => 'Plainfield', 'object' => 'Basketball'],
 **     ['state' => 'CA', 'city' => 'San Diego', 'object' => 'Light bulb'],
 **     ['state' => 'CA', 'city' => 'Mountain View', 'object' => 'Space pen'],
 ** ];
 ** $iteratee = function ($accumulator, $value) {
 **     if (isset($accumulator[$value['city']]))
 **         $accumulator[$value['city']]++;
 **     else
 **         $accumulator[$value['city']] = 1;
 **     return $accumulator;
 ** };
 ** __::reduce($c, $iteratee, []);
 ** // >> [
 ** // >>    'Indianapolis' => 2,
 ** // >>    'Plainfield' => 1,
 ** // >>    'San Diego' => 1,
 ** // >>    'Mountain View' => 1,
 ** // >> ]
 *
 ** $object = new \stdClass();
 ** $object->a = 1;
 ** $object->b = 2;
 ** $object->c = 1;
 ** __::reduce($object, function ($result, $value, $key) {
 **     if (!isset($result[$value]))
 **         $result[$value] = [];
 **     $result[$value][] = $key;
 **     return $result;
 ** }, [])
 ** // >> [
 ** // >>     '1' => ['a', 'c'],
 ** // >>     '2' => ['b']
 ** // >> ]
 *
 * @param array|object  $collection The collection to iterate over.
 * @param \Closure $iteratee The function invoked per iteration.
 * @param (*) [$accumulator] The initial value.
 *
 * @return (*): Returns the accumulated value.
 *
 */
function reduce($collection, \Closure $iteratee, $accumulator = NULL)
{
    if ($accumulator === NULL) {
        $accumulator = \__::first($collection);
    }
    foreach ($collection as $key => $value) {
        $accumulator = $iteratee($accumulator, $value, $key, $collection);
    }
    return $accumulator;
}<commit_msg>This space is not needed thus superfluous<commit_after><?php

namespace collections;

/**
 * Reduces $collection to a value which is the $accumulator result of running each
 * element in $collection thru $iteratee, where each successive invocation is supplied
 * the return value of the previous.
 *
 * If $accumulator is not given, the first element of $collection is used as the
 * initial value.
 *
 * The $iteratee is invoked with four arguments:
 * ($accumulator, $value, $index|$key, $collection).
 *
 ** __::reduce([1, 2], function ($sum, $number) {
 **     return $sum + $number;
 ** }, 0);
 ** // >> 3
 *
 ** $a = [
 **     ['state' => 'IN', 'city' => 'Indianapolis', 'object' => 'School bus'],
 **     ['state' => 'IN', 'city' => 'Indianapolis', 'object' => 'Manhole'],
 **     ['state' => 'IN', 'city' => 'Plainfield', 'object' => 'Basketball'],
 **     ['state' => 'CA', 'city' => 'San Diego', 'object' => 'Light bulb'],
 **     ['state' => 'CA', 'city' => 'Mountain View', 'object' => 'Space pen'],
 ** ];
 ** $iteratee = function ($accumulator, $value) {
 **     if (isset($accumulator[$value['city']]))
 **         $accumulator[$value['city']]++;
 **     else
 **         $accumulator[$value['city']] = 1;
 **     return $accumulator;
 ** };
 ** __::reduce($c, $iteratee, []);
 ** // >> [
 ** // >>    'Indianapolis' => 2,
 ** // >>    'Plainfield' => 1,
 ** // >>    'San Diego' => 1,
 ** // >>    'Mountain View' => 1,
 ** // >> ]
 *
 ** $object = new \stdClass();
 ** $object->a = 1;
 ** $object->b = 2;
 ** $object->c = 1;
 ** __::reduce($object, function ($result, $value, $key) {
 **     if (!isset($result[$value]))
 **         $result[$value] = [];
 **     $result[$value][] = $key;
 **     return $result;
 ** }, [])
 ** // >> [
 ** // >>     '1' => ['a', 'c'],
 ** // >>     '2' => ['b']
 ** // >> ]
 *
 * @param array|object $collection The collection to iterate over.
 * @param \Closure $iteratee The function invoked per iteration.
 * @param (*) [$accumulator] The initial value.
 *
 * @return (*): Returns the accumulated value.
 *
 */
function reduce($collection, \Closure $iteratee, $accumulator = NULL)
{
    if ($accumulator === NULL) {
        $accumulator = \__::first($collection);
    }
    foreach ($collection as $key => $value) {
        $accumulator = $iteratee($accumulator, $value, $key, $collection);
    }
    return $accumulator;
}
{'fraction_non_alphanumeric': 0.2360905436852126, 'fraction_numerical': 0.005923418658768775, 'mean_word_length': 3.1041666666666665, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 97, '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': '2859982', 'n_tokens_mistral': 1606, 'n_tokens_neox': 1510, 'n_words': 416}