Created
February 26, 2013 18:06
-
-
Save drcode/5040636 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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