Created
July 30, 2011 11:32
-
-
Save jjcomer/1115436 to your computer and use it in GitHub Desktop.
Clojure State Machine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns stateMachine) | |
(defn parse-integer [str] | |
(try (Integer/parseInt str) | |
(catch NumberFormatException nfe 0))) | |
(defn displayMenu | |
"This function displays the menu and gets the user's input" | |
[] | |
(println "Displaying the menu") | |
(println "Choose your option") | |
(case (parse-integer (read-line)) | |
1 :option1 | |
2 :option2 | |
3 :option3 | |
:error)) | |
(defn programLoop | |
"This fuction represents the main program loop" | |
[] | |
(letfn [ | |
(startState [] | |
#(getUserChoice)) | |
(getUserChoice [] | |
(case (displayMenu) | |
:option1 #(option1) | |
:option2 #(option2) | |
:option3 #(option3) | |
#(getUserChoice))) | |
(option1 [] | |
(println "Option 1") | |
#(startState)) | |
(option2 [] | |
(println "Option 2") | |
#(startState)) | |
(option3 [] | |
(println "Option 3") | |
nil)] | |
(trampoline startState))) | |
(programLoop) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment