Libraryless. Click here for Pure Java version (3474L/21K).
1 | sclass Scorer<A> { |
2 | double score, total; |
3 | L<A> successes, errors; // set to non-null if you want them filled |
4 | bool verboseFailures, verboseAll; |
5 | |
6 | void addZeroToOne aka add(double score) { |
7 | ++total; |
8 | this.score += clamp(score, 0, 1); |
9 | } |
10 | |
11 | void addZeroToOneError(double error) { |
12 | addZeroToOne(1-error); |
13 | } |
14 | |
15 | void addError { add(false); } |
16 | void addError(A info) { add(false, info); } |
17 | void error(A info) { addError(info); } |
18 | void addOK { add(true); } |
19 | void addOK(A info) { add(true, info); } |
20 | void ok() { addOK(); } |
21 | void ok(A info) { addOK(info); } |
22 | |
23 | bool add(bool correct) { |
24 | ++total; |
25 | if (correct) ++score; |
26 | ret correct; |
27 | } |
28 | |
29 | bool add(bool correct, A info) { |
30 | main add(correct ? successes : errors, info); |
31 | if (verboseAll || verboseFailures && !correct) |
32 | _print((correct ? "[GOOD] " : "[BAD] ") + info); |
33 | ret add(correct); |
34 | } |
35 | |
36 | // works if you use Scorer or Scorer<S> |
37 | void eq(O a, O b) { |
38 | if (_eq(a, b)) |
39 | add(true); |
40 | else |
41 | add(false, (A) (a + " != " + b)); |
42 | } |
43 | |
44 | void print() { |
45 | main print(toString()); |
46 | } |
47 | |
48 | toString { |
49 | ret formatDouble(ratioToPercent(score, total), 1) + "% correct (n=" + formatDouble(total, 1) + ")"; |
50 | } |
51 | |
52 | double get() { |
53 | ret ratioToPercent(score, total); |
54 | } |
55 | |
56 | double percentScore() { ret get(); } |
57 | double score() { ret get(); } |
58 | |
59 | bool allCorrect() { |
60 | ret score == total; |
61 | } |
62 | |
63 | void add(Scorer scorer) { |
64 | if (scorer == null) ret; |
65 | total += scorer.total; |
66 | score += scorer.score; |
67 | addAll(successes, scorer.successes); |
68 | addAll(errors, scorer.errors); |
69 | } |
70 | |
71 | void collectErrors() { |
72 | errors = new L; |
73 | } |
74 | |
75 | void collectSuccesses() { |
76 | successes = new L; |
77 | } |
78 | } |
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: | 638 / 1358 |
Version history: | 33 change(s) |
Referenced in: | [show references] |