Skip to content

Instantly share code, notes, and snippets.

@selfsame
Created August 8, 2015 22:41
Show Gist options
  • Select an option

  • Save selfsame/0dcbf71856d1735aac18 to your computer and use it in GitHub Desktop.

Select an option

Save selfsame/0dcbf71856d1735aac18 to your computer and use it in GitHub Desktop.
(declare look)
(def test-item
{:noun "chest" :material "wooden"
:closed false
:container
[{:noun "spoon" :material "tin"}
{:noun "bag" :material "leather"
:closed true
:container
[{:noun "coin" :material "copper"}
{:noun "coin" :material "gold"}]}]})
(rule describe [^:vector? o] {}
(unseq "it contains" (map write-name o)))
(rule describe [^empty? o] {} "it's empty")
(rule describe [o]
{o (a (non :closed) :container)}
"inside " (describe (:container o)))
(rule describe [o]
{o (a :closed :container)}
"it is closed")
(rule item-state [o]
{o :contents}
(if (:closed o) "closed" "open"))
(rule write-article [o] {} "the")
(rule write-adjectives [o] {}
(interpose ","
(unseq
(item-state o)
(:material o))))
(rule write-name [o] {} "thing")
(rule write-name [^:noun o] {} (:noun o))
(rule look [o] {}
"You don't see anything.")
(rule look [^map? o] {}
(unseq
"You look at"
(write-article o)
(write-adjectives o)
(write-name o) "."
(describe o)))
(look 7)
(look test-item)
(look (first (:container test-item)))
@selfsame
Copy link
Author

selfsame commented Aug 8, 2015


(def vowel? #{\a \e \i \o \u})

(declare describe)

(rule list-contents [col] {}
  (apply str (unseq 
    (interpose ", " (butlast col)) 
    " and " (last col))))

(rule description [^sequential? v] {} 
  (unseq "of" (list-contents (map describe v))))

(rule description [^empty? v] {} "")

(rule adjectives [^empty? v] {} "empty")
(rule adjectives [^nil? v] {} "")


(rule noun [v] {} (str (type v)))
(rule noun [^vector? v] {} "vector")
(rule noun [^set? v] {} "set")
(rule noun [^list? v] {} "list")
(rule noun [^map? v] {} "map")
(rule noun [^string? v] {} "string")
(rule noun [^keyword? v] {} "keyword")
(rule noun [^number? v] {} "number")
(rule noun [^integer? v] {}
  (or ({0 "zero" 1 "one" 2 "two" 3 "three" 4 "four" 5 "five" 
        6 "six" 7 "seven" 8 "eight" 9 "nine" 10 "ten"} v)
  (str v)))
(rule noun [^nil? v] {} "nil")

(rule article [v] {} "a")

(rule article [^string? v] {} 
  (if (vowel? 
      (first (clojure.string/lower-case 
             (clojure.string/trim v))))
  "an" "a"))
(rule article [^nil? v] {} "")


(rule describe [v] {}
  (let [subject (apply str (interpose " " (unseq (adjectives v) (noun v))))]
    (apply str (interpose " "
      (unseq
        (article subject)
        subject
        (description v))))))

(set? #{})


(describe #{1 [10]})
(describe [nil])
( describe '(nil [nil]))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment