Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use switch pattern matching #7

Merged
merged 1 commit into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading