Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

78
LINES

< > BotCompany Repo | #1012214 - Scorer - keeps score & total

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (3474L/21K).

sclass Scorer<A> {
  double score, total;
  L<A> successes, errors; // set to non-null if you want them filled
  bool verboseFailures, verboseAll;
  
  void addZeroToOne aka add(double score) {
    ++total;
    this.score += clamp(score, 0, 1);
  }
  
  void addZeroToOneError(double error) {
    addZeroToOne(1-error);
  }
  
  void addError { add(false); }
  void addError(A info) { add(false, info); }
  void error(A info) { addError(info); }
  void addOK { add(true); }
  void addOK(A info) { add(true, info); }
  void ok() { addOK(); }
  void ok(A info) { addOK(info); }
  
  bool add(bool correct) {
    ++total;
    if (correct) ++score;
    ret correct;
  }
  
  bool add(bool correct, A info) {
    main add(correct ? successes : errors, info);
    if (verboseAll || verboseFailures && !correct)
      _print((correct ? "[GOOD] " : "[BAD] ") + info);
    ret add(correct);
  }
  
  // works if you use Scorer or Scorer<S>
  void eq(O a, O b) {
    if (_eq(a, b))
      add(true);
    else
      add(false, (A) (a + " != " + b));
  }
  
  void print() {
    main print(toString());
  }
  
  toString {
    ret formatDouble(ratioToPercent(score, total), 1) + "% correct (n=" + formatDouble(total, 1) + ")";
  }
  
  double get() {
    ret ratioToPercent(score, total);
  }
  
  double percentScore() { ret get(); }
  double score() { ret get(); }
  
  bool allCorrect() {
    ret score == total;
  }
  
  void add(Scorer scorer) {
    if (scorer == null) ret;
    total += scorer.total;
    score += scorer.score;
    addAll(successes, scorer.successes);
    addAll(errors, scorer.errors);
  }
  
  void collectErrors() {
    errors = new L;
  }
  
  void collectSuccesses() {
    successes = new L;
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, whxojlpjdney

No comments. add comment

Snippet ID: #1012214
Snippet name: Scorer - keeps score & total
Eternal ID of this version: #1012214/34
Text MD5: 0896ebcc1ea6cf6ac082491030fb8c62
Transpilation MD5: bda091b3a98bd5b54aa7cb690b619a87
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-06-25 15:59:47
Source code size: 1789 bytes / 78 lines
Pitched / IR pitched: No / No
Views / Downloads: 417 / 1035
Version history: 33 change(s)
Referenced in: [show references]