Skip to content

Instantly share code, notes, and snippets.

@drcode
Created February 26, 2013 18:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drcode/5040636 to your computer and use it in GitHub Desktop.
Save drcode/5040636 to your computer and use it in GitHub Desktop.
(ns groupon.core
(require [clojure.string :refer [lower-case]]))
;;functional code
(defn delete-item [items n]
(vec (concat (take n items) (drop (inc n) items))))
(def commands [{:name "(A)dd"
:key :a
:needs [:text]
:function conj}
{:name "(E)dit"
:key :e
:needs [:index :text]
:function assoc}
{:name "(D)elete"
:key :d
:needs [:index]
:function delete-item}
{:name "(S)ort"
:key :s
:needs []
:function sort}
{:name "(R)everse"
:key :r
:needs []
:function reverse}
{:name "(Q)uit"}])
;;imperative code
(defn input []
(flush)
(read))
(defn get-text []
(print "New text:")
(input))
(defn get-index []
(println "Which item?")
(dec (input)))
(defn main-loop [items]
(dorun (map-indexed (fn [i item]
(println (inc i) item))
items))
(apply println (map :name commands))
(let [x (keyword (input))]
(when-not (= :q x)
(recur (let [{:keys [needs function]} (first (filter #(= (:key %) x) commands))]
(apply function
items
(for [need needs]
(case need
:text (get-text)
:index (get-index)))))))))
(defn -main []
(println "Welcome to List Keeper!")
(main-loop []))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment