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

74
LINES

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

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

Libraryless. Click here for Pure Java version (2502L/16K).

sclass Scorer<A> {
  double score, total;
  L<A> successes, errors; // set to non-null if you want them filled
  bool verboseFailures, verboseAll;
  
  void addZeroToOne(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;
  }
}

download  show line numbers  debug dex   

Travelled to 11 computer(s): aoiabmzegqzx, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, tslmcundralx, tvejysmllsmz, whxojlpjdney

No comments. add comment

Snippet ID: #1012214
Snippet name: Scorer - keeps score & total
Eternal ID of this version: #1012214/30
Text MD5: 41ccc88d593bb27f2f66a969b2e4646b
Transpilation MD5: d8e14611511e92f5f6035e82c2885547
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-11-11 13:58:52
Source code size: 1719 bytes / 74 lines
Pitched / IR pitched: No / No
Views / Downloads: 288 / 648
Version history: 29 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1012214 & http://1012214.tinybrain.de