Kiss-spaced-rep support flexible engine object

This commit is contained in:
2022-11-03 22:04:56 +00:00
parent 0a44a9a2b6
commit b1567e9f66
4 changed files with 59 additions and 37 deletions

View File

@@ -1,8 +1,8 @@
package kiss_spaced_rep; package kiss_spaced_rep;
typedef CardSide = { typedef CardSide = {
show: (Void->Void) -> Void, show: (StudyEngine, Void->Void) -> Void,
score: (Int->Void) -> Void score: (StudyEngine, Int->Void) -> Void
}; };
typedef Card = { typedef Card = {

View File

@@ -1,44 +1,43 @@
(import (import
datetime.DateTime datetime.DateTime
datetime.DateTimeInterval datetime.DateTimeInterval
kiss_spaced_rep.Card) kiss_spaced_rep.Card
kiss_spaced_rep.StudyEngine)
(var oneDayInterval (DateTimeInterval.create (DateTime.make 1970 1 1) (DateTime.make 1970 1 2))) (var oneDayInterval (DateTimeInterval.create (DateTime.make 1970 1 1) (DateTime.make 1970 1 2)))
(var halfDayInterval (DateTimeInterval.create (DateTime.make 1970 1 1 0) (DateTime.make 1970 1 1 12))) (var halfDayInterval (DateTimeInterval.create (DateTime.make 1970 1 1 0) (DateTime.make 1970 1 1 12)))
(var studyList []) (var studyList [])
(function :Void confirmShow [text :Void->Void cc] (function :Void confirmShow [text :StudyEngine engine :Void->Void cc]
(Sys.print text) (engine.printCC text cc))
(.readLine (Sys.stdin))
(function :Void basicScore [text :StudyEngine engine :Int->Void resolve]
(engine.println text)
(engine.print "Score (0-5): ")
(engine.input ->:Void str (resolve (Std.parseInt str))))
(function :Void noConfirmShow [text :StudyEngine engine :Void->Void cc]
(engine.println text)
(cc)) (cc))
(function :Void basicScore [text :Int->Void resolve] (function :Void typeScore [text :StudyEngine engine :Int->Void resolve]
(Sys.println text) (engine.print "Your answer: ")
(Sys.print "Score (0-5): ") (engine.input ->:Void input
(resolve (Std.parseInt (.readLine (Sys.stdin))))) (cond
((= text input)
(engine.println "Correct!")
(resolve 5))
(true
(engine.println "Wrong! Correct answer was $text")
(resolve 0)))))
(function :Void noConfirmShow [text :Void->Void cc] (function :Void clozeShow [index textLines :StudyEngine engine :Void->Void cc]
(Sys.println text)
(cc))
(function :Void typeScore [text :Int->Void resolve]
(Sys.print "Your answer: ")
(cond
((= text (.readLine (Sys.stdin)))
(Sys.println "Correct!")
(resolve 5))
(true
(Sys.println "Wrong! Correct answer was $text")
(resolve 0))))
(function :Void clozeShow [index textLines :Void->Void cc]
(doFor [idx line] (enumerate textLines) (doFor [idx line] (enumerate textLines)
(Sys.println (if (= idx index) "????" line))) (engine.println (if (= idx index) "????" line)))
(.readLine (Sys.stdin)) (engine.input ->:Void _ (cc)))
(cc))
(function :Void clozeScore [index textLines :Int->Void resolve] (function :Void clozeScore [index textLines :StudyEngine engine :Int->Void resolve]
(basicScore (nth textLines index) resolve)) (basicScore (nth textLines index) engine resolve))
(function :CardSide basicText [:String text] (function :CardSide basicText [:String text]
(object (object
@@ -69,7 +68,7 @@
(savedVar :Float ,cNextDate (DateTime.make)) (savedVar :Float ,cNextDate (DateTime.make))
(let [:DateTime nextDate ,cNextDate] (let [:DateTime nextDate ,cNextDate]
(localFunction :Void ,cStudy [:Void->Void cc] (localFunction :Void ,cStudy [:StudyEngine engine :Void->Void cc]
(localFunction :Void ,cScore [:Int quality] (localFunction :Void ,cScore [:Int quality]
(localVar &mut interval 0) (localVar &mut interval 0)
(cond (cond
@@ -90,11 +89,11 @@
(cc)) (cc))
(if (or #{DateTime.now() > nextDate;}# (#if debug true false)) (if (or #{DateTime.now() > nextDate;}# (#if debug true false))
(.show .front ,cObject (.show .front ,cObject engine
->:Void ->:Void
(.score .back ,cObject ,cScore)) (.score .back ,cObject engine ,cScore))
{ {
(print "skipping a card until $(nextDate.toString)") (engine.println "skipping a card until $(nextDate.toString)")
(cc) (cc)
})) }))
(studyList.push ,cStudy)) (studyList.push ,cStudy))
@@ -117,12 +116,22 @@
show ->:Void _ 0 show ->:Void _ 0
score (clozeScore.bind ,i ,textLines)))))) score (clozeScore.bind ,i ,textLines))))))
(function :Void studyAll [] (function :Void studyAll [&opt :StudyEngine engine]
(unless engine
(set engine
(object
clear ->:Void {}
print ->:Void text (Sys.print text)
println ->:Void text (Sys.println text)
printCC ->:Void [text cc] {(Sys.print text)(.readLine (Sys.stdin))(cc)}
printlnCC ->:Void [text cc] {(Sys.println text)(.readLine (Sys.stdin))(cc)}
showImageCC ->:Void [path cc] 0
input ->:Void resolve (resolve (.readLine (Sys.stdin))))))
(when studyList (when studyList
(let [nextIndex (Std.random studyList.length) (let [nextIndex (Std.random studyList.length)
nextToStudy (nth studyList nextIndex)] nextToStudy (nth studyList nextIndex)]
(studyList.splice nextIndex 1) (studyList.splice nextIndex 1)
(nextToStudy studyAll)))) (nextToStudy engine (studyAll.bind engine)))))
// TODO make a note macro that defines a card then defines it in reverse // TODO make a note macro that defines a card then defines it in reverse
// TODO make a cloze macro that makes a card with each group deleted // TODO make a cloze macro that makes a card with each group deleted

View File

@@ -0,0 +1,13 @@
package kiss_spaced_rep;
typedef Continuation = Void -> Void;
typedef StudyEngine = {
clear: Void -> Void,
print: (String) -> Void,
println: (String) -> Void,
printCC: (String, Continuation) -> Void,
printlnCC: (String, Continuation) -> Void,
showImageCC: (String, Continuation) -> Void,
input: (String->Void) -> Void
};

View File

@@ -1,3 +1,3 @@
#! /bin/bash #! /bin/bash
haxe -D cards=$(pwd)/examples/Nonsense.kiss build.hxml haxe -D cards=$(pwd)/examples/French.kiss build.hxml