├── .gitignore ├── .swiftpm └── xcode │ └── package.xcworkspace │ ├── contents.xcworkspacedata │ └── xcshareddata │ └── IDEWorkspaceChecks.plist ├── Tests ├── LinuxMain.swift └── SpacedRepetitionSchedulerTests │ ├── XCTestManifests.swift │ └── SpacedRepetitionSchedulerTests.swift ├── .swiftformat ├── Package.swift ├── Sources └── SpacedRepetitionScheduler │ ├── Array+RecallEaseLinearSearch.swift │ ├── PromptSchedulingMode.swift │ ├── RecallEase.swift │ ├── TimeInterval+Fuzzing.swift │ ├── Documentation.docc │ └── Documentation.md │ ├── SchedulingParameters.swift │ └── PromptSchedulingMetadata.swift ├── CHANGELOG.md ├── README.md └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | /.build 3 | /Packages 4 | /*.xcodeproj 5 | xcuserdata/ 6 | -------------------------------------------------------------------------------- /.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Tests/LinuxMain.swift: -------------------------------------------------------------------------------- 1 | // Copyright © 2020 Brian Dewey. Covered by the Apache 2.0 license. 2 | 3 | import XCTest 4 | 5 | import SpacedRepetitionSchedulerTests 6 | 7 | var tests = [XCTestCaseEntry]() 8 | tests += SpacedRepetitionSchedulerTests.allTests() 9 | XCTMain(tests) 10 | -------------------------------------------------------------------------------- /.swiftpm/xcode/package.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Tests/SpacedRepetitionSchedulerTests/XCTestManifests.swift: -------------------------------------------------------------------------------- 1 | // Copyright © 2020 Brian Dewey. Covered by the Apache 2.0 license. 2 | 3 | import XCTest 4 | 5 | #if !canImport(ObjectiveC) 6 | public func allTests() -> [XCTestCaseEntry] { 7 | return [ 8 | testCase(SpacedRepetitionSchedulerTests.allTests), 9 | ] 10 | } 11 | #endif 12 | -------------------------------------------------------------------------------- /.swiftformat: -------------------------------------------------------------------------------- 1 | --binarygrouping none 2 | --decimalgrouping none 3 | --exclude Package.swift 4 | --header "Copyright © 2020 Brian Dewey. Covered by the Apache 2.0 license." 5 | --hexgrouping none 6 | --indent 2 7 | --octalgrouping none 8 | --patternlet inline 9 | --stripunusedargs closure-only 10 | --wraparguments beforefirst 11 | --wrapcollections beforefirst 12 | --self init-only 13 | -------------------------------------------------------------------------------- /Package.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version:5.5 2 | 3 | import PackageDescription 4 | 5 | let package = Package( 6 | name: "SpacedRepetitionScheduler", 7 | products: [ 8 | .library( 9 | name: "SpacedRepetitionScheduler", 10 | targets: ["SpacedRepetitionScheduler"] 11 | ), 12 | ], 13 | dependencies: [], 14 | targets: [ 15 | .target( 16 | name: "SpacedRepetitionScheduler", 17 | dependencies: [] 18 | ), 19 | .testTarget( 20 | name: "SpacedRepetitionSchedulerTests", 21 | dependencies: ["SpacedRepetitionScheduler"] 22 | ), 23 | ] 24 | ) 25 | -------------------------------------------------------------------------------- /Sources/SpacedRepetitionScheduler/Array+RecallEaseLinearSearch.swift: -------------------------------------------------------------------------------- 1 | // Copyright © 2020 Brian Dewey. Covered by the Apache 2.0 license. 2 | 3 | import Foundation 4 | 5 | public extension Array where Element == (key: RecallEase, value: PromptSchedulingMetadata) { 6 | /// Performs a linear search through the receiver for a given ``RecallEase`` and returns the associated ``PromptSchedulingMetadata``. 7 | subscript(_ answer: RecallEase) -> PromptSchedulingMetadata? { 8 | for (candidateAnswer, item) in self where candidateAnswer == answer { 9 | return item 10 | } 11 | return nil 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## Version 0.5.0 -- 2024-03-23 4 | 5 | - Add `Sendable` conformances 6 | 7 | ## Version 0.4.0 -- 2024-01-18 8 | 9 | - Added many synthesized protocol conformances (thanks @DandyLyons) 10 | 11 | ## Version 0.3.0 -- 2021-09-18 12 | 13 | - Uses swift-tools 5.5 now that Xcode 13 is at a Release Candidate. This adds DocC support for this package. 14 | 15 | ## Version 0.2.1 -- 2021-06-27 16 | 17 | ### Changed 18 | 19 | - Switched back to swift-tools 5.4. This loses DocC. Otherwise, this is compatible with release 0.2.0 20 | 21 | ## Version 0.2.0 -- 2021-06-27 22 | 23 | ### Changed 24 | 25 | - A pretty big refactor of the APIs to strive for more fluent usage at callsites. This version is not compatable with Version 0.1. 26 | -------------------------------------------------------------------------------- /Sources/SpacedRepetitionScheduler/PromptSchedulingMode.swift: -------------------------------------------------------------------------------- 1 | // Copyright © 2020 Brian Dewey. Covered by the Apache 2.0 license. 2 | 3 | import Foundation 4 | 5 | /// Determines which time interval recommendation algorithm to use with prompts. 6 | public enum PromptSchedulingMode: Hashable, Codable, Sendable { 7 | /// Represents a prompt in the *learning* mode. 8 | /// 9 | /// An item stays in the learning state until it has been recalled a specific number of times, determined by the number of items in the ``SchedulingParameters/learningIntervals`` array. 10 | /// - parameter step: How many learning steps have been completed. `step == 0` implies a new card. 11 | case learning(step: Int) 12 | 13 | /// Represents a prompt in the *review* mode. 14 | /// 15 | /// Items in the review state are scheduled at increasingly longer intervals with each successful recall. 16 | case review 17 | } 18 | -------------------------------------------------------------------------------- /Sources/SpacedRepetitionScheduler/RecallEase.swift: -------------------------------------------------------------------------------- 1 | // Copyright © 2020 Brian Dewey. Covered by the Apache 2.0 license. 2 | 3 | import Foundation 4 | 5 | /// Rating for how easy it is to recall the information associated with a prompt. 6 | public enum RecallEase: Int, CaseIterable, Codable, Hashable, Sendable { 7 | /// The learner was not able to recall the information associated with a prompt. 8 | case again 9 | 10 | /// The learner had difficulty recalling the information associated with a prompt and would like to review the corresponding prompt more frequently. 11 | case hard 12 | 13 | /// The learner could recall the information associated with a prompt with the expected amount of difficulty, and would like to normally space out future reviews of this prompt. 14 | case good 15 | 16 | /// The learner effortlessly recalled the information associated with a prompt, and would like to greatly space out future reviews of the prompt. 17 | case easy 18 | } 19 | -------------------------------------------------------------------------------- /Sources/SpacedRepetitionScheduler/TimeInterval+Fuzzing.swift: -------------------------------------------------------------------------------- 1 | // Copyright © 2020 Brian Dewey. Covered by the Apache 2.0 license. 2 | 3 | import Foundation 4 | 5 | public extension TimeInterval { 6 | /// A TimeInterval that is close to, but not necessarily identical to, the receiver. 7 | /// - note: The value will fall within the bounds defined by `self.fuzzRange` 8 | func fuzzed() -> TimeInterval { 9 | return Double.random(in: fuzzRange) 10 | } 11 | 12 | /// To keep cards added at the same time from always being scheduled together, we apply "fuzz" to the time interval. 13 | /// - note: This logic is transcribed from anki schedv2.py _fuzzIvlRange 14 | var fuzzRange: ClosedRange { 15 | if self < 2 * .day { 16 | return self ... self 17 | } 18 | if self < 3 * .day { 19 | return 2 * .day ... 3 * .day 20 | } 21 | var fuzz: TimeInterval 22 | if self < 7 * .day { 23 | fuzz = self / 4 24 | } else if self < 30 * .day { 25 | fuzz = max(2 * .day, self * 0.15) 26 | } else { 27 | fuzz = max(4 * .day, self * 0.05) 28 | } 29 | fuzz = max(1, fuzz) 30 | return (self - fuzz) ... (self + fuzz) 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Sources/SpacedRepetitionScheduler/Documentation.docc/Documentation.md: -------------------------------------------------------------------------------- 1 | # ``SpacedRepetitionScheduler`` 2 | 3 | Implements an Anki-style spaced repetition scheduler for active recall prompts. 4 | 5 | ## Overview 6 | 7 | In a learning system that uses [active recall](https://en.wikipedia.org/wiki/Active_recall), 8 | learners are presented with a *prompt* and rate their ability to recall the corresponding information. 9 | `SpacedRepetitionScheduler` recommends a time interval to wait before showing a learner the same *prompt* again, given 10 | his/her history of recalling the information associated with this prompt and how well he/she did 11 | recalling the associated information this time. The recommended time intervals for a given prompt will increase the more frequently 12 | the learner recalls the information associated with a prompt. 13 | 14 | `SpacedRepetitionScheduler` considers a prompt to be in one of two modes when making time interval recommendations: *learning* or *review*. 15 | 16 | - In the *learning* mode, the learner must successfully recall the corresponding information a specific number of times with specific time intervals between recall attempts. After successfully recalling the associated information the specified number of times, the prompt graduates to the *review* state. 17 | - In the *review* mode, the amount of time between successive reviews of a prompt increases by a geometric progression with each successful recall. 18 | 19 | 20 | ## Topics 21 | 22 | ### Representing prompts 23 | 24 | - ``PromptSchedulingMetadata`` 25 | - ``PromptSchedulingMode`` 26 | 27 | ### Scheduling parameters 28 | 29 | - ``RecallEase`` 30 | - ``SchedulingParameters`` 31 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SpacedRepetitionScheduler 2 | 3 | Implements an Anki-style spaced repetition scheduler for active recall prompts. 4 | 5 | ## Overview 6 | 7 | In a learning system that uses [active recall](https://en.wikipedia.org/wiki/Active_recall), 8 | learners are presented with a *prompt* and rate their ability to recall the corresponding information. 9 | `SpacedRepetitionScheduler` recommends a time interval to wait before showing a learner the same *prompt* again, given 10 | his/her history of recalling the information associated with this prompt and how well he/she did 11 | recalling the associated information this time. The recommended time intervals for a given prompt will increase the more frequently 12 | the learner recalls the information associated with a prompt. 13 | 14 | `SpacedRepetitionScheduler` considers a prompt to be in one of two modes when making time interval recommendations: *learning* or *review*. 15 | 16 | - In the *learning* mode, the learner must successfully recall the corresponding information a specific number of times with specific time intervals between recall attempts. After successfully recalling the associated information the specified number of times, the prompt graduates to the *review* state. 17 | - In the *review* mode, the amount of time between successive reviews of a prompt increases by a geometric progression with each successful recall. 18 | 19 | 20 | ## Where to start in the code 21 | 22 | ### Representing prompts 23 | 24 | - `PromptSchedulingMetadata` -- Information needed to determine the recommended time to review a prompt again. 25 | - `PromptSchedulingMode` -- Whether the prompt is in *learning* or *review* mode. 26 | 27 | ### Scheduling parameters 28 | 29 | - `RecallEase` -- Rating for how easy it is to recall the information associated with a prompt. 30 | - `SchedulingParameters` -- Holds parameters used to determine the recommended time to schedule the next review of a prompt. 31 | -------------------------------------------------------------------------------- /Sources/SpacedRepetitionScheduler/SchedulingParameters.swift: -------------------------------------------------------------------------------- 1 | // Copyright © 2020 Brian Dewey. Covered by the Apache 2.0 license. 2 | 3 | import Foundation 4 | 5 | public extension TimeInterval { 6 | static let minute: TimeInterval = 60 7 | static let day: TimeInterval = 60 * 60 * 24 8 | } 9 | 10 | /// Holds parameters used to determine the recommended time to schedule the next review of a prompt. 11 | public struct SchedulingParameters: Codable, Equatable, Sendable { 12 | /// Creates a `SpacedReptitionScheduler` with the specified scheduling parameters. 13 | /// 14 | /// - parameter learningIntervals: The time intervals between successive successful recalls of a prompt in *learning* mode. The number of items in this array determines how many times a learner must successfully recall a prompt for it to graduate to the *review* mode. 15 | /// - parameter easyGraduatingInterval: The ideal interval for reviewing a prompt again after it graduates from *learning* to *review* with a recall ease of *easy*. 16 | /// - parameter goodGraduatingInterval: The ideal interval for reviewing a prompt again after it graduates from *learning* to *review* with a recall ease of *good*. 17 | /// - parameter easyBoost: An additional mutiplicative factor for the scheduling interval when a prompt is in *review* mode and its recall ease is *easy*. 18 | public init( 19 | learningIntervals: [TimeInterval], 20 | easyGraduatingInterval: TimeInterval = 4 * .day, 21 | goodGraduatingInterval: TimeInterval = 1 * .day, 22 | easyBoost: Double = 1.3 23 | ) { 24 | self.learningIntervals = learningIntervals 25 | self.easyGraduatingInterval = easyGraduatingInterval 26 | self.goodGraduatingInterval = goodGraduatingInterval 27 | self.easyBoost = easyBoost 28 | } 29 | 30 | /// The intervals between successive steps when "learning" an item. 31 | public let learningIntervals: [TimeInterval] 32 | 33 | /// The ideal interval for reviewing a prompt again after it graduates from *learning* to *review* with a recall ease of *easy*. 34 | public let easyGraduatingInterval: TimeInterval 35 | 36 | /// The ideal interval for reviewing a prompt again after it graduates from *learning* to *review* with a recall ease of *good*. 37 | public let goodGraduatingInterval: TimeInterval 38 | 39 | /// An additional mutiplicative factor for the scheduling interval when a prompt is in *review* mode and its recall ease is *easy*. 40 | public let easyBoost: Double 41 | } 42 | -------------------------------------------------------------------------------- /Tests/SpacedRepetitionSchedulerTests/SpacedRepetitionSchedulerTests.swift: -------------------------------------------------------------------------------- 1 | // Copyright © 2020 Brian Dewey. Covered by the Apache 2.0 license. 2 | 3 | import SpacedRepetitionScheduler 4 | import XCTest 5 | 6 | final class SpacedRepetitionSchedulerTests: XCTestCase { 7 | let schedulingParameters = SchedulingParameters( 8 | learningIntervals: [1 * .minute, 10 * .minute] 9 | ) 10 | 11 | func testScheduleNewCard() { 12 | let newItem = PromptSchedulingMetadata(mode: .learning(step: 0)) 13 | let results = newItem.allPossibleUpdates(with: schedulingParameters) 14 | 15 | // shouldn't be a "hard" answer 16 | XCTAssertEqual(results.count, RecallEase.allCases.count - 1) 17 | // Check that the repetition count increased for all items. 18 | for result in results { 19 | XCTAssertEqual(result.value.reviewCount, 1) 20 | } 21 | 22 | XCTAssertEqual(results[.again]?.mode, .learning(step: 0)) 23 | XCTAssertEqual(results[.again]?.interval, schedulingParameters.learningIntervals[0]) 24 | 25 | // Cards that were "easy" immediately leave the learning state. 26 | XCTAssertEqual(results[.easy]?.mode, .review) 27 | XCTAssertEqual(results[.easy]?.interval, schedulingParameters.easyGraduatingInterval) 28 | 29 | // Cards that were "good" move to the next state. 30 | XCTAssertEqual(results[.good]?.mode, .learning(step: 1)) 31 | XCTAssertEqual(results[.good]?.interval, schedulingParameters.learningIntervals[1]) 32 | } 33 | 34 | func testScheduleReadyToGraduateCard() { 35 | let readyToGraduateItem = PromptSchedulingMetadata( 36 | mode: .learning(step: schedulingParameters.learningIntervals.count - 1) 37 | ) 38 | let results = readyToGraduateItem.allPossibleUpdates(with: schedulingParameters) 39 | 40 | // shouldn't be a "hard" answer 41 | XCTAssertEqual(results.count, RecallEase.allCases.count - 1) 42 | // Check that the repetition count increased for all items. 43 | for result in results { 44 | XCTAssertEqual(result.value.reviewCount, 1) 45 | } 46 | 47 | XCTAssertEqual(results[.again]?.mode, .learning(step: 0)) 48 | XCTAssertEqual(results[.again]?.interval, schedulingParameters.learningIntervals[0]) 49 | 50 | // Cards that were "easy" immediately leave the learning state. 51 | XCTAssertEqual(results[.easy]?.mode, .review) 52 | XCTAssertEqual(results[.easy]?.interval, schedulingParameters.easyGraduatingInterval) 53 | 54 | // Cards that were "good" graduate. 55 | XCTAssertEqual(results[.good]?.mode, .review) 56 | XCTAssertEqual(results[.good]?.interval, schedulingParameters.goodGraduatingInterval) 57 | } 58 | 59 | func testProgressFromNewToReview() throws { 60 | var item = PromptSchedulingMetadata() 61 | 62 | for _ in 0 ..< schedulingParameters.learningIntervals.count { 63 | // Answer the item as "good" 64 | try item.update(with: schedulingParameters, recallEase: .good, timeIntervalSincePriorReview: 0) 65 | } 66 | XCTAssertEqual(item.mode, .review) 67 | XCTAssertEqual(item.interval, schedulingParameters.goodGraduatingInterval) 68 | } 69 | 70 | func testScheduleReviewCard() { 71 | let reviewItem = PromptSchedulingMetadata( 72 | mode: .review, 73 | reviewCount: 5, 74 | interval: 4 * .day 75 | ) 76 | let delay: TimeInterval = .day 77 | let results = reviewItem.allPossibleUpdates(with: schedulingParameters, timeIntervalSincePriorReview: delay) 78 | XCTAssertEqual(results[.again]?.lapseCount, 1) 79 | XCTAssertEqual(results[.again]?.interval, schedulingParameters.learningIntervals.first) 80 | XCTAssertEqual(results[.again]?.mode, .learning(step: 0)) 81 | XCTAssertEqual(results[.again]!.reviewSpacingFactor, 2.3, accuracy: 0.01) 82 | 83 | XCTAssertEqual(results[.hard]?.lapseCount, 0) 84 | XCTAssertEqual(results[.hard]?.mode, .review) 85 | XCTAssertEqual(results[.hard]!.reviewSpacingFactor, 2.5 - 0.15, accuracy: 0.01) 86 | XCTAssertEqual(results[.hard]!.interval, reviewItem.interval * 1.2, accuracy: 0.01) 87 | 88 | XCTAssertEqual(results[.good]?.lapseCount, 0) 89 | XCTAssertEqual(results[.good]?.mode, .review) 90 | XCTAssertEqual(results[.good]!.reviewSpacingFactor, 2.5, accuracy: 0.01) 91 | XCTAssertEqual(results[.good]!.interval, (reviewItem.interval + delay / 2) * reviewItem.reviewSpacingFactor, accuracy: 0.01) 92 | 93 | XCTAssertEqual(results[.easy]?.lapseCount, 0) 94 | XCTAssertEqual(results[.easy]?.mode, .review) 95 | XCTAssertEqual(results[.easy]!.reviewSpacingFactor, 2.5 + 0.15, accuracy: 0.01) 96 | XCTAssertEqual(results[.easy]!.interval, (reviewItem.interval + delay) * reviewItem.reviewSpacingFactor * schedulingParameters.easyBoost, accuracy: 0.01) 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /Sources/SpacedRepetitionScheduler/PromptSchedulingMetadata.swift: -------------------------------------------------------------------------------- 1 | // Copyright © 2020 Brian Dewey. Covered by the Apache 2.0 license. 2 | 3 | import Foundation 4 | 5 | /// Information needed to determine the recommended time to review a prompt again. 6 | public struct PromptSchedulingMetadata: Hashable, Codable, Sendable { 7 | /// The learning state of this prompt. 8 | public var mode: PromptSchedulingMode 9 | 10 | /// How many times this prompt has been reviewed. 11 | public var reviewCount: Int 12 | 13 | /// How many times this prompt regressed from "review" back to "learning" 14 | public var lapseCount: Int 15 | 16 | /// The ideal amount of time until seeing this prompt again. 17 | public var interval: TimeInterval 18 | 19 | /// The multiplicative factor for increasing the delay for seeing this prompt again, if ``mode`` is `.review`. 20 | public var reviewSpacingFactor: Double 21 | 22 | /// Creates prompt metadata with specific values. 23 | public init( 24 | mode: PromptSchedulingMode = .learning(step: 0), 25 | reviewCount: Int = 0, 26 | lapseCount: Int = 0, 27 | interval: TimeInterval = 0, 28 | reviewSpacingFactor: Double = 2.5 29 | ) { 30 | self.mode = mode 31 | self.reviewCount = reviewCount 32 | self.lapseCount = lapseCount 33 | self.reviewSpacingFactor = reviewSpacingFactor 34 | self.interval = interval 35 | } 36 | 37 | enum SchedulingError: Error { 38 | /// Error when we try to say that recall was "hard" for an item in the `learning` state. This is not a valid scheduling option and should not be shown to a learner. 39 | case noHardRecallForLearningItems 40 | } 41 | 42 | /// Updates the receiver given the prompt was recalled with specified `recallEase` after `timeIntervalSincePastReview`. 43 | /// - Parameters: 44 | /// - schedulingParameters: Parameters used to compute the ideal time to review a prompt again. 45 | /// - recallEase: A value representing how easy the learner recalled the information associated with the prompt. 46 | /// - timeIntervalSincePriorReview: The duration of time since the prompt was last reviewed. 47 | /// - Returns: An updated value for ``PromptSchedulingMetadata`` 48 | public mutating func update( 49 | with schedulingParameters: SchedulingParameters, 50 | recallEase: RecallEase, 51 | timeIntervalSincePriorReview: TimeInterval 52 | ) throws { 53 | reviewCount += 1 54 | switch (mode, recallEase) { 55 | case (.learning, .again): 56 | moveToFirstStep(schedulingParameters: schedulingParameters) 57 | case (.learning, .easy): 58 | // Immediate graduation! 59 | mode = .review 60 | interval = schedulingParameters.easyGraduatingInterval 61 | case (.learning, .hard): 62 | // Not a valid answer -- no "hard" for something we're learning 63 | throw SchedulingError.noHardRecallForLearningItems 64 | case (.learning(let step), .good): 65 | // Move to the next step. 66 | if step < (schedulingParameters.learningIntervals.count - 1) { 67 | interval = schedulingParameters.learningIntervals[step + 1] 68 | mode = .learning(step: step + 1) 69 | } else { 70 | // Graduate to "review" 71 | mode = .review 72 | interval = schedulingParameters.goodGraduatingInterval 73 | } 74 | case (.review, .again): 75 | lapseCount += 1 76 | reviewSpacingFactor = max(1.3, reviewSpacingFactor - 0.2) 77 | moveToFirstStep(schedulingParameters: schedulingParameters) 78 | case (.review, .hard): 79 | interval *= 1.2 80 | reviewSpacingFactor = max(1.3, reviewSpacingFactor - 0.15) 81 | case (.review, .good): 82 | // Expand interval by factor, fuzzing the result, and ensuring that it at least moves forward 83 | // by the "hard" amount. 84 | interval = (interval + timeIntervalSincePriorReview / 2) * reviewSpacingFactor 85 | case (.review, .easy): 86 | interval = (interval + timeIntervalSincePriorReview) * reviewSpacingFactor * schedulingParameters.easyBoost 87 | reviewSpacingFactor += 0.15 88 | } 89 | } 90 | 91 | /// Updates the receiver given the prompt was recalled with specified `recallEase` after `timeIntervalSincePastReview`. 92 | /// - Parameters: 93 | /// - schedulingParameters: Parameters used to compute the ideal time to review a prompt again. 94 | /// - recallEase: A value representing how easy the learner recalled the information associated with the prompt. 95 | /// - timeIntervalSincePastReview: The duration of time since the prompt was last reviewed. 96 | /// - Returns: An updated value for ``PromptSchedulingMetadata`` 97 | public func updating( 98 | with schedulingParameters: SchedulingParameters, 99 | recallEase: RecallEase, 100 | timeIntervalSincePriorReview: TimeInterval 101 | ) throws -> PromptSchedulingMetadata { 102 | var copy = self 103 | try copy.update( 104 | with: schedulingParameters, 105 | recallEase: recallEase, 106 | timeIntervalSincePriorReview: timeIntervalSincePriorReview 107 | ) 108 | return copy 109 | } 110 | 111 | /// Returns a mapping of possible ``RecallEase`` values to updated ``PromptSchedulingMetadata`` values. 112 | /// - Parameters: 113 | /// - promptSchedulingMetadata: The current ``PromptSchedulingMetadata`` for a prompt. 114 | /// - timeIntervalSincePastReview: The duration of time since the prompt was last reviewed. 115 | /// - Returns: An array of key/value pairs associating a valid ``RecallEase`` to an updated ``PromptSchedulingMetadata`` if the prompt was recalled with that ease value. 116 | public func allPossibleUpdates( 117 | with schedulingParameters: SchedulingParameters, 118 | timeIntervalSincePriorReview: TimeInterval = 0 119 | ) -> [(key: RecallEase, value: PromptSchedulingMetadata)] { 120 | let result = RecallEase.allCases.compactMap { recallEase -> (RecallEase, PromptSchedulingMetadata)? in 121 | guard let updatedMetadata = try? self.updating(with: schedulingParameters, recallEase: recallEase, timeIntervalSincePriorReview: timeIntervalSincePriorReview) else { 122 | return nil 123 | } 124 | return (recallEase, updatedMetadata) 125 | } 126 | return result 127 | } 128 | 129 | private mutating func moveToFirstStep(schedulingParameters: SchedulingParameters) { 130 | // Go back to the initial learning step, schedule out a tiny bit. 131 | mode = .learning(step: 0) 132 | interval = schedulingParameters.learningIntervals.first ?? .minute 133 | } 134 | } 135 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------