on 3d07dec help sign in
58 lines 1.5 kB history raw
1(ns clojure-sample.core
2 (:gen-class))
3
4 (require '[clj-time.core :as t])
5 (require '[clj-time.format :as f])
6
7 ;; Product record
8 (defrecord Product [id name available price])
9
10 ;; Positional constructor
11 (def product1 (->Product "1" "T-Shirt 1" true 15.00))
13 ;; Map constructor
14 (def product2 (map->Product
15 {:id "2"
16 :name "T-Shirt 2"
17 :available true
18 :price 20.00}))
20 ;; Nested
21 (def product3 {:id "1"
22 :name "Product 1"
23 :available true
24 :sellers [{:id "1"
25 :name "Seller 1"
26 :stock 3},
27 {:id 2
28 :name "Seller 2"
29 :stock 5}]})
31 ;; Set
32 (def categories #{"shirts" "shoes" "belts"})
34 ;; List
35 (def wishlist '(1 2))
37 ;; Recursion
38 (defn factorial [value] (cond
39 (<= value 1) 1
40 :else (* value (factorial (- value 1)))))
42 (def basic-formatter (f/formatter "YYYY-MM-dd hh:mm:ss"))
43 (defn now [] (f/unparse basic-formatter (t/now)))
44 (defn log
45 ([] (println (now) "No message"))
46 ([message] (println (now) message)))
48 (defn -main
49 [& args]
50 (println (:id product1))
51 (println (:name product2))
52 (println (:name (get (:sellers product3) 0)))
53 (println (first categories))
54 (println wishlist)
55 (println (factorial 5))
56 (log)
57 (log "Message"))