From fb00963e11663b498ba514177a7279ed58225441 Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Sun, 22 Jan 2023 18:00:03 -0700 Subject: [PATCH] Refactor kiss-spaced-rep StudyEngine --- .../src/kiss_spaced_rep/ConsoleEngine.kiss | 30 +++++++++++++++++++ .../src/kiss_spaced_rep/KSR.kiss | 27 ++++++----------- .../src/kiss_spaced_rep/Main_.kiss | 4 ++- .../src/kiss_spaced_rep/StudyEngine.hx | 18 +++++------ projects/kiss-spaced-rep/study.sh | 4 ++- 5 files changed, 53 insertions(+), 30 deletions(-) create mode 100644 projects/kiss-spaced-rep/src/kiss_spaced_rep/ConsoleEngine.kiss diff --git a/projects/kiss-spaced-rep/src/kiss_spaced_rep/ConsoleEngine.kiss b/projects/kiss-spaced-rep/src/kiss_spaced_rep/ConsoleEngine.kiss new file mode 100644 index 00000000..5e5aaa87 --- /dev/null +++ b/projects/kiss-spaced-rep/src/kiss_spaced_rep/ConsoleEngine.kiss @@ -0,0 +1,30 @@ +(implements kiss_spaced_rep.StudyEngine) + +(defNew []) + +(method :Void clear [] + (doFor i (range 5) (Sys.println ""))) + +(method :Void print [text] + (Sys.print text)) + +(method :Void println [text] + (Sys.println text)) + +(method :Void showImage [path] + (assertProcess (case (Sys.systemName) + ("Windows" "cmd.exe") + ("Linux" "xdg-open") + ("Mac" "start") + (never otherwise)) + (case (Sys.systemName) + ("Windows" ["/C" path]) + ((or "Mac" "Linux") [path]) + (never otherwise)))) + +(method :Void delayForUserInput [cc] + (.readLine (Sys.stdin)) + (cc)) + +(method :Void getUserInput [resolve] + (resolve (.readLine (Sys.stdin)))) \ No newline at end of file diff --git a/projects/kiss-spaced-rep/src/kiss_spaced_rep/KSR.kiss b/projects/kiss-spaced-rep/src/kiss_spaced_rep/KSR.kiss index a1e08c8b..ae91a2d9 100644 --- a/projects/kiss-spaced-rep/src/kiss_spaced_rep/KSR.kiss +++ b/projects/kiss-spaced-rep/src/kiss_spaced_rep/KSR.kiss @@ -9,12 +9,13 @@ (var studyList []) (function :Void confirmShow [text :StudyEngine engine :Void->Void cc] - (engine.printCC text cc)) + (engine.print text) + (engine.delayForUserInput cc)) (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)))) + (engine.getUserInput ->:Void str (resolve (Std.parseInt str)))) (function :Void noConfirmShow [text :StudyEngine engine :Void->Void cc] (engine.println text) @@ -22,7 +23,7 @@ (function :Void typeScore [text :StudyEngine engine :Int->Void resolve] (engine.print "Your answer: ") - (engine.input ->:Void input + (engine.getUserInput ->:Void input (cond ((= text input) (engine.println "Correct!") @@ -39,7 +40,7 @@ (if (StringTools.startsWith line "?") (line.substr 1) line)))) - (engine.input ->:Void _ (cc))) + (engine.delayForUserInput cc)) (function :Void clozeScore [index :Array textLines :StudyEngine engine :Int->Void resolve] (basicScore (.substr (nth textLines index) 1) engine resolve)) @@ -56,9 +57,9 @@ (function :CardSide image [:String path &opt :String caption] (object - show ->:Void [engine cc] {(when caption (engine.print caption)) (engine.showImageCC path cc)} + show ->:Void [engine cc] {(when caption (engine.print caption)) (engine.showImage path) (engine.delayForUserInput cc)} - score ->:Void [engine :Int->Void resolve] {(when caption (engine.print caption))(engine.showImage path)(engine.print "Score (0-5): ")(engine.input ->:Void str (resolve (Std.parseInt str)))})) + score ->:Void [engine :Int->Void resolve] {(when caption (engine.print caption))(engine.showImage path)(engine.print "Score (0-5): ")(engine.getUserInput ->:Void str (resolve (Std.parseInt str)))})) (defMacroVar cardId 0) (defMacroVar groupId 0) @@ -137,18 +138,8 @@ score (clozeScore.bind ,i ,textLines))))))) (b.callSymbol "group" cardDecls))) -(function :Void studyAll [&opt :StudyEngine engine] - (unless engine - (set engine - (object - clear ->:Void (doFor i (range 5) (Sys.println "")) - print ->:Void text (Sys.print text) - println ->:Void text (Sys.println text) - showImage ->:Void path (assertProcess (case (Sys.systemName) ("Windows" "cmd.exe") ("Linux" "xdg-open") ("Mac" "start") (never otherwise)) (case (Sys.systemName) ("Windows" ["/C" path]) ((or "Mac" "Linux") [path]) (never otherwise))) - 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] {(assertProcess (case (Sys.systemName) ("Windows" "cmd.exe") ("Linux" "xdg-open") ("Mac" "start") (never otherwise)) (case (Sys.systemName) ("Windows" ["/C" path]) ((or "Mac" "Linux") [path]) (never otherwise)))(.readLine (Sys.stdin))(cc)} - input ->:Void resolve (resolve (.readLine (Sys.stdin)))))) +(function :Void studyAll [:StudyEngine engine] + (when studyList (let [nextIndex (Std.random studyList.length) nextToStudy (nth studyList nextIndex)] diff --git a/projects/kiss-spaced-rep/src/kiss_spaced_rep/Main_.kiss b/projects/kiss-spaced-rep/src/kiss_spaced_rep/Main_.kiss index 74045e2f..b9336d32 100644 --- a/projects/kiss-spaced-rep/src/kiss_spaced_rep/Main_.kiss +++ b/projects/kiss-spaced-rep/src/kiss_spaced_rep/Main_.kiss @@ -1,6 +1,8 @@ (loadFrom "kiss-spaced-rep" "src/kiss_spaced_rep/KSR.kiss") +(import kiss_spaced_rep.StudyEngine) + (savedVarFile (#value "cards")) (load (#value "cards")) -(studyAll) \ No newline at end of file +(studyAll (new (#symbol "engine"))) \ No newline at end of file diff --git a/projects/kiss-spaced-rep/src/kiss_spaced_rep/StudyEngine.hx b/projects/kiss-spaced-rep/src/kiss_spaced_rep/StudyEngine.hx index 5f0d2af1..ad8e27cd 100644 --- a/projects/kiss-spaced-rep/src/kiss_spaced_rep/StudyEngine.hx +++ b/projects/kiss-spaced-rep/src/kiss_spaced_rep/StudyEngine.hx @@ -2,13 +2,11 @@ package kiss_spaced_rep; typedef Continuation = Void -> Void; -typedef StudyEngine = { - clear: Void -> Void, - print: (String) -> Void, - println: (String) -> Void, - showImage: (String) -> Void, - printCC: (String, Continuation) -> Void, - printlnCC: (String, Continuation) -> Void, - showImageCC: (String, Continuation) -> Void, - input: (String->Void) -> Void -}; +interface StudyEngine { + function clear():Void; + function print(s:String):Void; + function println(s:String):Void; + function showImage(s:String):Void; + function delayForUserInput(cc:Continuation):Void; + function getUserInput(resolve:String->Void):Void; +} diff --git a/projects/kiss-spaced-rep/study.sh b/projects/kiss-spaced-rep/study.sh index c7a64a59..1c31e7a8 100644 --- a/projects/kiss-spaced-rep/study.sh +++ b/projects/kiss-spaced-rep/study.sh @@ -1,3 +1,5 @@ #! /bin/bash -haxe -D cards=$(pwd)/$1 build.hxml \ No newline at end of file +ENGINE=${2:-kiss_spaced_rep.ConsoleEngine} + +haxe -D cards="$(pwd)/$1" -D engine="$ENGINE" build.hxml \ No newline at end of file