Skip to content

Commit

Permalink
refactor: use switch pattern matching
Browse files Browse the repository at this point in the history
- change `if`, `if else`, else` clause to switch pattern matching
- add additional space
- add missing comma on long argument
  • Loading branch information
brainwo committed Aug 27, 2024
1 parent b586f55 commit 269ffa1
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 50 deletions.
67 changes: 35 additions & 32 deletions lib/src/fsrs_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,39 +26,42 @@ class FSRS {
final s = SchedulingCards(card);
s.updateState(card.state);

if (card.state == State.newState) {
_initDS(s);

s.again.due = now.add(Duration(minutes: 1));
s.hard.due = now.add(Duration(minutes: 5));
s.good.due = now.add(Duration(minutes: 10));
final easyInterval = _nextInterval(s.easy.stability);
s.easy.scheduledDays = easyInterval;
s.easy.due = now.add(Duration(days: easyInterval));
} else if (card.state == State.learning || card.state == State.relearning) {
final hardInterval = 0;
final goodInterval = _nextInterval(s.good.stability);
final easyInterval =
max(_nextInterval(s.easy.stability), goodInterval + 1);

s.schedule(now, hardInterval.toDouble(), goodInterval.toDouble(),
easyInterval.toDouble());
} else if (card.state == State.review) {
final interval = card.elapsedDays;
final lastD = card.difficulty;
final lastS = card.stability;
final retrievability = _forgettingCurve(interval, lastS);
_nextDS(s, lastD, lastS, retrievability);

var hardInterval = _nextInterval(s.hard.stability);
var goodInterval = _nextInterval(s.good.stability);
hardInterval = min(hardInterval, goodInterval);
goodInterval = max(goodInterval, hardInterval + 1);
final easyInterval =
max(_nextInterval(s.easy.stability), goodInterval + 1);
s.schedule(now, hardInterval.toDouble(), goodInterval.toDouble(),
easyInterval.toDouble());
switch (card.state) {
case State.newState:
_initDS(s);

s.again.due = now.add(Duration(minutes: 1));
s.hard.due = now.add(Duration(minutes: 5));
s.good.due = now.add(Duration(minutes: 10));
final easyInterval = _nextInterval(s.easy.stability);
s.easy.scheduledDays = easyInterval;
s.easy.due = now.add(Duration(days: easyInterval));
case State.learning:
case State.relearning:
final hardInterval = 0;
final goodInterval = _nextInterval(s.good.stability);
final easyInterval =
max(_nextInterval(s.easy.stability), goodInterval + 1);

s.schedule(now, hardInterval.toDouble(), goodInterval.toDouble(),
easyInterval.toDouble());
case State.review:
final interval = card.elapsedDays;
final lastD = card.difficulty;
final lastS = card.stability;
final retrievability = _forgettingCurve(interval, lastS);
_nextDS(s, lastD, lastS, retrievability);

var hardInterval = _nextInterval(s.hard.stability);
var goodInterval = _nextInterval(s.good.stability);
hardInterval = min(hardInterval, goodInterval);
goodInterval = max(goodInterval, hardInterval + 1);
final easyInterval =
max(_nextInterval(s.easy.stability), goodInterval + 1);
s.schedule(now, hardInterval.toDouble(), goodInterval.toDouble(),
easyInterval.toDouble());
}

return s.recordLog(card, now);
}

Expand Down
42 changes: 24 additions & 18 deletions lib/src/models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -110,27 +110,33 @@ class SchedulingCards {
}

void updateState(State state) {
if (state == State.newState) {
again.state = State.learning;
hard.state = State.learning;
good.state = State.learning;
easy.state = State.review;
} else if (state == State.learning || state == State.relearning) {
again.state = state;
hard.state = state;
good.state = State.review;
easy.state = State.review;
} else if (state == State.review) {
again.state = State.relearning;
hard.state = State.review;
good.state = State.review;
easy.state = State.review;
again.lapses++;
switch (state) {
case State.newState:
again.state = State.learning;
hard.state = State.learning;
good.state = State.learning;
easy.state = State.review;
case State.learning:
case State.relearning:
again.state = state;
hard.state = state;
good.state = State.review;
easy.state = State.review;
case State.review:
again.state = State.relearning;
hard.state = State.review;
good.state = State.review;
easy.state = State.review;
again.lapses++;
}
}

void schedule(DateTime now, double hardInterval, double goodInterval,
double easyInterval) {
void schedule(
DateTime now,
double hardInterval,
double goodInterval,
double easyInterval,
) {
again.scheduledDays = 0;
hard.scheduledDays = hardInterval.toInt();
good.scheduledDays = goodInterval.toInt();
Expand Down

0 comments on commit 269ffa1

Please sign in to comment.