An idiomatic and configurable Swift implementation of the FSRS spaced repetition algorithm.
Add the following to your Package.swift
file:
dependencies: [
.package(url: "https://github.com/4rays/swift-fsrs")
]
The workhorse of the algorithm is the Scheduler
protocol. It takes a card and a review and returns a new card and review log object.
Out of the box, the library ships with a short-term and a long-term scheduler. Use the short-term scheduler when you want to support multiple reviews of the same card in a single day. Use the long-term scheduler otherwise.
Here is how you can create your own scheduler:
import SwiftFSRS
struct CustomScheduler: Scheduler {
func schedule(
card: Card,
algorithm: FSRSAlgorithm,
reviewRating: ReviewRating,
reviewTime: Date
) -> CardReview {
// Implement your custom algorithm here
}
}
The library implements v5
of the FSRS algorithm out of the box. It can also support custom implementations.
import SwiftFSRS
let customAlgorithm = FSRSAlgorithm(
decay: -0.5,
factor: 19 / 81,
requestRetention: 0.9,
maximumInterval: 36500,
parameters: [/* ... */]
)
scheduler.schedule(
card: card,
algorithm: customAlgorithm,
reviewRating: .good,
reviewTime: Date()
)
To schedule a review for a given card:
import SwiftFSRS
let scheduler = LongTermScheduler()
let card = Card()
let review = scheduler.schedule(
card: card,
algorithm: .v5,
reviewRating: .good, // or .easy, .hard, .again
reviewTime: Date()
)
print(review.card)
print(review.log)
Cards don't have any content properties and are meant to be properties of your own type.
import SwiftFSRS
struct MyFlashCard {
let question: String
let answer: String
let fsrsCard: Card
}
SwiftFSRS is available under the MIT license. See the LICENSE file for more info.