Skip to content

Commit 7ddad86

Browse files
committed
better fsrs type, function names
1 parent ef94efc commit 7ddad86

2 files changed

Lines changed: 31 additions & 31 deletions

File tree

src/db.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ use rusqlite::types::ValueRef;
3434
use crate::error::ErrorReport;
3535
use crate::error::Fallible;
3636
use crate::error::fail;
37-
use crate::fsrs::D;
37+
use crate::fsrs::Difficulty;
3838
use crate::fsrs::Grade;
39-
use crate::fsrs::S;
40-
use crate::fsrs::d_0;
39+
use crate::fsrs::Stability;
40+
use crate::fsrs::initial_difficulty;
41+
use crate::fsrs::initial_stability;
4142
use crate::fsrs::interval;
4243
use crate::fsrs::new_difficulty;
4344
use crate::fsrs::new_stability;
4445
use crate::fsrs::retrievability;
45-
use crate::fsrs::s_0;
4646
use crate::hash::Hash;
4747
use crate::parser::Card;
4848
use crate::parser::CardContent;
@@ -143,8 +143,8 @@ impl Database {
143143
let mut rows = stmt.query([hash])?;
144144
if let Some(row) = rows.next()? {
145145
let reviewed_at: Timestamp = row.get(0)?;
146-
let stability: S = row.get(1)?;
147-
let difficulty: D = row.get(2)?;
146+
let stability: Stability = row.get(1)?;
147+
let difficulty: Difficulty = row.get(2)?;
148148
let due_date: Date = row.get(3)?;
149149
Ok(Some(Performance {
150150
last_review: reviewed_at.into_date(),
@@ -192,8 +192,8 @@ pub struct Review {
192192
pub card_hash: Hash,
193193
pub reviewed_at: Timestamp,
194194
pub grade: Grade,
195-
pub stability: S,
196-
pub difficulty: D,
195+
pub stability: Stability,
196+
pub difficulty: Difficulty,
197197
pub due_date: Date,
198198
}
199199

@@ -344,8 +344,8 @@ struct InsertReview {
344344
card_hash: Hash,
345345
reviewed_at: Timestamp,
346346
grade: Grade,
347-
stability: S,
348-
difficulty: D,
347+
stability: Stability,
348+
difficulty: Difficulty,
349349
due_date: Date,
350350
}
351351

@@ -374,8 +374,8 @@ const TARGET_RECALL: f64 = 0.9;
374374
#[derive(Clone, Debug, PartialEq)]
375375
pub struct Performance {
376376
pub last_review: Date,
377-
pub stability: S,
378-
pub difficulty: D,
377+
pub stability: Stability,
378+
pub difficulty: Difficulty,
379379
pub due_date: Date,
380380
}
381381

@@ -396,7 +396,7 @@ impl Performance {
396396
let difficulty = new_difficulty(difficulty, grade);
397397
(stability, difficulty)
398398
}
399-
None => (s_0(grade), d_0(grade)),
399+
None => (initial_stability(grade), initial_difficulty(grade)),
400400
};
401401
let interval = f64::max(interval(TARGET_RECALL, stability).round(), 1.0);
402402
let interval_duration = chrono::Duration::days(interval as i64);

src/fsrs.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ pub const W: [f64; 19] = [
2828
];
2929

3030
pub type R = f64;
31-
pub type S = f64;
32-
pub type D = f64;
31+
pub type Stability = f64;
32+
pub type Difficulty = f64;
3333

3434
#[derive(Clone, Copy, PartialEq, Debug)]
3535
pub enum Grade {
@@ -93,15 +93,15 @@ pub type T = f64;
9393
const F: f64 = 19.0 / 81.0;
9494
const C: f64 = -0.5;
9595

96-
pub fn retrievability(t: T, s: S) -> R {
96+
pub fn retrievability(t: T, s: Stability) -> R {
9797
(1.0 + F * (t / s)).powf(C)
9898
}
9999

100-
pub fn interval(r_d: R, s: S) -> T {
100+
pub fn interval(r_d: R, s: Stability) -> T {
101101
(s / F) * (r_d.powf(1.0 / C) - 1.0)
102102
}
103103

104-
pub fn s_0(g: Grade) -> S {
104+
pub fn initial_stability(g: Grade) -> Stability {
105105
match g {
106106
Grade::Forgot => W[0],
107107
Grade::Hard => W[1],
@@ -110,7 +110,7 @@ pub fn s_0(g: Grade) -> S {
110110
}
111111
}
112112

113-
fn s_success(d: D, s: S, r: R, g: Grade) -> S {
113+
fn s_success(d: Difficulty, s: Stability, r: R, g: Grade) -> Stability {
114114
let t_d = 11.0 - d;
115115
let t_s = s.powf(-W[9]);
116116
let t_r = f64::exp(W[10] * (1.0 - r)) - 1.0;
@@ -121,7 +121,7 @@ fn s_success(d: D, s: S, r: R, g: Grade) -> S {
121121
s * alpha
122122
}
123123

124-
fn s_fail(d: D, s: S, r: R) -> S {
124+
fn s_fail(d: Difficulty, s: Stability, r: R) -> Stability {
125125
let d_f = d.powf(-W[12]);
126126
let s_f = (s + 1.0).powf(W[13]) - 1.0;
127127
let r_f = f64::exp(W[14] * (1.0 - r));
@@ -130,28 +130,28 @@ fn s_fail(d: D, s: S, r: R) -> S {
130130
f64::min(s_f, s)
131131
}
132132

133-
pub fn new_stability(d: D, s: S, r: R, g: Grade) -> S {
133+
pub fn new_stability(d: Difficulty, s: Stability, r: R, g: Grade) -> Stability {
134134
if g == Grade::Forgot {
135135
s_fail(d, s, r)
136136
} else {
137137
s_success(d, s, r, g)
138138
}
139139
}
140140

141-
fn clamp_d(d: D) -> D {
141+
fn clamp_d(d: Difficulty) -> Difficulty {
142142
d.clamp(1.0, 10.0)
143143
}
144144

145-
pub fn d_0(g: Grade) -> D {
145+
pub fn initial_difficulty(g: Grade) -> Difficulty {
146146
let g: f64 = g.into();
147147
clamp_d(W[4] - f64::exp(W[5] * (g - 1.0)) + 1.0)
148148
}
149149

150-
pub fn new_difficulty(d: D, g: Grade) -> D {
151-
clamp_d(W[7] * d_0(Grade::Easy) + (1.0 - W[7]) * dp(d, g))
150+
pub fn new_difficulty(d: Difficulty, g: Grade) -> Difficulty {
151+
clamp_d(W[7] * initial_difficulty(Grade::Easy) + (1.0 - W[7]) * dp(d, g))
152152
}
153153

154-
fn dp(d: D, g: Grade) -> f64 {
154+
fn dp(d: Difficulty, g: Grade) -> f64 {
155155
d + delta_d(g) * ((10.0 - d) / 9.0)
156156
}
157157

@@ -187,7 +187,7 @@ mod tests {
187187
/// D_0(1) = w_4
188188
#[test]
189189
fn test_initial_difficulty_of_forgetting() {
190-
assert_eq!(d_0(Grade::Forgot), W[4])
190+
assert_eq!(initial_difficulty(Grade::Forgot), W[4])
191191
}
192192

193193
/// A simulation step.
@@ -196,9 +196,9 @@ mod tests {
196196
/// The time when the review took place.
197197
t: T,
198198
/// New stability.
199-
s: S,
199+
s: Stability,
200200
/// New difficulty.
201-
d: D,
201+
d: Difficulty,
202202
/// Next interval.
203203
i: T,
204204
}
@@ -222,8 +222,8 @@ mod tests {
222222
assert!(!grades.is_empty());
223223
let mut grades = grades.clone();
224224
let g: Grade = grades.remove(0);
225-
let mut s: S = s_0(g);
226-
let mut d: D = d_0(g);
225+
let mut s: Stability = initial_stability(g);
226+
let mut d: Difficulty = initial_difficulty(g);
227227
let mut i: T = f64::max(interval(r_d, s).round(), 1.0);
228228
steps.push(Step { t, s, d, i });
229229

0 commit comments

Comments
 (0)