<commit_before><?php
/******************************************************************************
 * Copyright (c) 2014 <NAME> <<EMAIL>>
 *
 * Base class and primitives for collectors.
 */

require_once("checking.php");
require_once("values.php");

abstract class Collector {
    /* Expects an array. Tries to collect it's desired input from it and returns
     * it as a Value. Throws if desired content can not be found. A missing 
     * input is not to be considered as a regular error but rather points at 
     * some problem in the implementation or tampering with the input, thus we 
     * throw.
     */
    abstract public function collect($inp);
    /* Check whether Collector collects something. */
    abstract public function isNullaryCollector();

    /* Map a function over the collected value. */
    final public function map(FunctionValue $fun) {
        return new MappedCollector($this, $fun);    
    }
}

class MissingInputError extends Exception {
    private $_name; //string
    public function __construct($name) {
        $this->_name = $name;
        parent::__construct("Missing input $name.");
    }
}

/* A collector that collects nothing and will be dropped by apply collectors. */
final class NullaryCollector extends Collector {
    public function collect($inp) {
        die("NullaryCollector::collect: This should never be called.");
    }
    public function isNullaryCollector() {
        return true;
    }
}

/* A collector that always returns a constant value. */
final class ConstCollector extends Collector {
    private $_value; // Value

    public function __construct(Value $value) {
        $this->_value = $value;
    }

    public function collect($inp) {
        return $this->_value;
    }

    public function isNullaryCollector() {
        return false;
    }
}

/* A collector that applies the input from its left collector to the input
 * from its right collector.
 */
final class ApplyCollector extends Collector {
    private $_l;
    private $_r;

    public function __construct(Collector $left, Collector $right) {
        $this->_l = $left;
        $this->_r = $right;
    }

    public function collect($inp) {
        $l = $this->_l->collect($inp);
        $r = $this->_r->collect($inp);
        return $l->apply($r);
    }

    public function isNullaryCollector() {
        return false;
    }
}

/* A collector that does a predicate check on the input from another
 * collector and return an error if the predicate fails.
 */
final class CheckedCollector extends Collector {
    private $_collector; // Collector
    private $_predicate; // FunctionValue
    private $_error; // string

    public function __construct(Collector $collector, FunctionValue $predicate, $error) {
        guardIsString($error);
        guardHasArity($predicate, 1);
        $this->_collector = $collector;
        $this->_predicate = $predicate;
        $this->_error = $error;
    }

    public function collect($inp) {
        $res = $this->_collector->collect($inp);
        if ($res->isError()) {
            return $res;
        }

        // TODO: Maybe check for PlainValue on result before
        // doing this?
        if ($this->_predicate->apply($res)->get()) {
            return $res;
        }
        else {
            return _error($this->_error, $res);
        }
    }

    public function isNullaryCollector() {
        return false;
    }
}

/* A collector where the input is mapped by a function */
final class MappedCollector extends Collector {
    private $_collector; // Collector
    private $_function; // FunctionValue
    
    public function __construct(Collector $collector, FunctionValue $function) {
        guardHasArity($function, 1);
        if ($collector->isNullaryCollector()) {
            throw new TypeError("non nullary collector", typeName($collector));
        }
        $this->_collector = $collector;
        $this->_function = $function;
    }

    public function collect($inp) {
        $res = $this->_collector->collect($inp);
        if ($res->isError()) {
            return $res;
        }

        $res2 = $this->_function->apply($res);<commit_msg>naming<commit_after><?php
/******************************************************************************
 * Copyright (c) 2014 <NAME> <<EMAIL>>
 *
 * Base class and primitives for collectors.
 */

require_once("checking.php");
require_once("values.php");

abstract class Collector {
    /* Expects an array. Tries to collect it's desired input from it and returns
     * it as a Value. Throws if desired content can not be found. A missing 
     * input is not to be considered as a regular error but rather points at 
     * some problem in the implementation or tampering with the input, thus we 
     * throw.
     */
    abstract public function collect($inp);
    /* Check whether Collector collects something. */
    abstract public function isNullaryCollector();

    /* Map a function over the collected value. */
    final public function map(FunctionValue $transformation) {
        return new MappedCollector($this, $transformation);    
    }
}

class MissingInputError extends Exception {
    private $_name; //string
    public function __construct($name) {
        $this->_name = $name;
        parent::__construct("Missing input $name.");
    }
}

/* A collector that collects nothing and will be dropped by apply collectors. */
final class NullaryCollector extends Collector {
    public function collect($inp) {
        die("NullaryCollector::collect: This should never be called.");
    }
    public function isNullaryCollector() {
        return true;
    }
}

/* A collector that always returns a constant value. */
final class ConstCollector extends Collector {
    private $_value; // Value

    public function __construct(Value $value) {
        $this->_value = $value;
    }

    public function collect($inp) {
        return $this->_value;
    }

    public function isNullaryCollector() {
        return false;
    }
}

/* A collector that applies the input from its left collector to the input
 * from its right collector.
 */
final class ApplyCollector extends Collector {
    private $_l;
    private $_r;

    public function __construct(Collector $left, Collector $right) {
        $this->_l = $left;
        $this->_r = $right;
    }

    public function collect($inp) {
        $l = $this->_l->collect($inp);
        $r = $this->_r->collect($inp);
        return $l->apply($r);
    }

    public function isNullaryCollector() {
        return false;
    }
}

/* A collector that does a predicate check on the input from another
 * collector and return an error if the predicate fails.
 */
final class CheckedCollector extends Collector {
    private $_collector; // Collector
    private $_predicate; // FunctionValue
    private $_error; // string

    public function __construct(Collector $collector, FunctionValue $predicate, $error) {
        guardIsString($error);
        guardHasArity($predicate, 1);
        $this->_collector = $collector;
        $this->_predicate = $predicate;
        $this->_error = $error;
    }

    public function collect($inp) {
        $res = $this->_collector->collect($inp);
        if ($res->isError()) {
            return $res;
        }

        // TODO: Maybe check for PlainValue on result before
        // doing this?
        if ($this->_predicate->apply($res)->get()) {
            return $res;
        }
        else {
            return _error($this->_error, $res);
        }
    }

    public function isNullaryCollector() {
        return false;
    }
}

/* A collector where the input is mapped by a function */
final class MappedCollector extends Collector {
    private $_collector; // Collector
    private $_transformation; // FunctionValue
    
    public function __construct(Collector $collector, FunctionValue $transformation) {
        guardHasArity($transformation, 1);
        if ($collector->isNullaryCollector()) {
            throw new TypeError("non nullary collector", typeName($collector));
        }
        $this->_collector = $collector;
        $this->_transformation = $transformation;
    }

    public function collect($inp) {
        $res = $this->_collector->collect($inp);
        if ($res->isError()) {
            return $res;
        }

        $res2 = $this->_transformation->apply($res);
{'fraction_non_alphanumeric': 0.12576210400478183, 'fraction_numerical': 0.0016736401673640166, 'mean_word_length': 2.654871122761031, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 65, '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': '885747', 'n_tokens_mistral': 2294, 'n_tokens_neox': 2203, 'n_words': 725}