Sources/OrgCore/Flashcards/FlashcardSM2.swift

4d8e517de5aef10e915fb482735feba5435a4a21
orgstar/Sources/OrgCore/Flashcards/FlashcardSM2.swift

44 lines · 2113 bytes

 1import Foundation
 2
 3extension Flashcards {
 4    /// org-fc's variant of SM2 (`org-fc-algo-sm2`, `org-fc-algorithm` `sm2-v1`) with its
 5    /// default parameters.
 6    public struct SM2: Sendable, Equatable {
 7        public var easeMin = 1.3
 8        public var easeMax = 5.0
 9        public var easeInitial = 2.5
10        public var fuzzMin = 0.9
11        public var fuzzMax = 1.1
12        public var changes: [Rating: Double] = [.again: -0.3, .hard: -0.15, .good: 0.0, .easy: 0.15]
13        /// Intervals in days of the first boxes.
14        public var intervals = [0.0, 0.01, 1.0, 6.0]
15
16        public static let headers = ["position", "ease", "box", "interval", "due"]
17
18        public init() {}
19
20        /// `org-fc-algo-initial-review-data`.
21        public func initial(_ name: String, now: Date) -> [String: String] {
22            ["position": name, "ease": String(format: "%.2f", easeInitial), "box": "0", "interval": String(format: "%.2f", 0.0),
23             "due": Flashcards.timestamp(now)]
24        }
25
26        /// `org-fc-algo-next-review-data`. `random` stands for `cl-random`'s value as a
27        /// fraction of its limit, in [0, 1).
28        public func next(_ current: [String: String], rating: Rating, now: Date, random: Double) -> [String: String] {
29            let ease = Flashcards.number(current["ease"])
30            let box = Int(Flashcards.number(current["box"]))
31            let interval = Flashcards.number(current["interval"])
32            let nextEase = box < 2 ? ease : min(max(ease + changes[rating]!, easeMin), easeMax)
33            let nextBox = box == 0 && rating == .easy ? 2 : rating == .again ? 0 : box + 1
34            let nextInterval: Double
35            if nextBox < intervals.count {
36                nextInterval = intervals[nextBox]
37            } else {
38                nextInterval = nextEase * interval * (fuzzMin + (fuzzMax - fuzzMin) * random)
39            }
40            return ["ease": String(format: "%.2f", nextEase), "box": String(nextBox), "interval": String(format: "%.2f", nextInterval),
41                    "due": Flashcards.timestamp(now, plus: nextInterval)]
42        }
43    }
44}
Keyboard shortcuts

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form