Skip to content

Instantly share code, notes, and snippets.

@rsachdeva
Created June 14, 2013 14:58
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rsachdeva/5782538 to your computer and use it in GitHub Desktop.
Save rsachdeva/5782538 to your computer and use it in GitHub Desktop.
todo list (in clojure) for kids summer holidays tasks entered dynamically on command line!
(defn todo-prompt [question]
(println question)
(read-line))
;mutable with atom and swap!
(def todos (atom {}))
(defn todo-details [todos]
(let [task-code (todo-prompt "Task code?")
task-description (todo-prompt "Task description?")
task-days-given-to-complete (todo-prompt "Task days given to complete?")]
(swap! todos into {task-code {"task-description" task-description
"task-days-given-to-complete" task-days-given-to-complete}})))
(println "Welcome to task listing for summer holidays!")
(dotimes [n (read-string (todo-prompt "Number of tasks?"))]
(todo-details todos)
(println (str "tasks uptil now " @todos)))
;immutable with loop and recur
(defn immutable-todo-details []
(let [task-code (todo-prompt "Task code?")
task-description (todo-prompt "Task description?")
task-days-given-to-complete (todo-prompt "Task days given to complete?")]
{task-code {"task-description" task-description
"task-days-given-to-complete" task-days-given-to-complete} }))
(loop [n (read-string (todo-prompt "Number of tasks?"))
tasks {}]
(if (< n 1)
(println (str "final tasks uptil now in immutable way " tasks))
(do
(println (str "tasks uptil now in immutable way" tasks))
(recur (- n 1)
(conj tasks (immutable-todo-details))))))
;immutable with map and reduce
(defn for-map-todo-details [x]
(let [task-code (todo-prompt "Task code?")
task-description (todo-prompt "Task description?")
task-days-given-to-complete (todo-prompt "Task days given to complete?")]
{task-code {"task-description" task-description
"task-days-given-to-complete" task-days-given-to-complete} }))
(reduce conj (map for-map-todo-details (range (read-string (todo-prompt "Number of tasks?")))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment