Skip to content

Commit

Permalink
Finished "Collection" section of chapter 3.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrwizard82d1 committed Jul 22, 2014
1 parent d5541c7 commit 2584f8c
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 12 deletions.
20 changes: 10 additions & 10 deletions ch-03/src/ch_03/abstractions.clj
Expand Up @@ -7,7 +7,7 @@
;; evaluated. Because I had spent a couple of hours "fighting"
;; with how to eliminate this duplication, I opted to live with it.
(defmacro print-eval-symbol [sexpr]
`(println (str " " '~sexpr " = " ~sexpr)))
`(println (str " " '~sexpr " = " ~sexpr)))

(defn do-vector-ops
[]
Expand Down Expand Up @@ -47,25 +47,25 @@
m {:a 5 :b 6}
s #{1 2}
l '(1)]
(println " " '(into v [4 5]) "=" (into v [4 5]))
(println " " '(into m [[:c 7] [:d 8]]) "=" (into m [[:c 7] [:d 8]]))
(println " " '(into s [2 3 4 5 3 3 2]) "=" (into s [2 3 4 5 3 3 2]))
(println " " '(into l {:a 1 :b 2}))))
(print-eval-symbol (into v [4 5]))
(print-eval-symbol (into m [[:c 7] [:d 8]]))
(print-eval-symbol (into s [2 3 4 5 3 3 2]))
(print-eval-symbol (into l {:a 1 :b 2}))))

(defn do-all
[]
(println)
(println "Vector operations")
(println " Vector operations")
(do-vector-ops)
(println)
(println "Map operations")
(println " Map operations")
(do-map-ops)
(println)
(println "Set operations")
(println " Set operations")
(do-set-ops)
(println)
(println "List operations")
(println " List operations")
(do-list-ops)
(println)
(println "Into is built atop conj and seq")
(println " Into is built atop conj and seq")
(do-into))
59 changes: 59 additions & 0 deletions ch-03/src/ch_03/collection.clj
@@ -0,0 +1,59 @@
(ns ch-03.collection
(use [ch-03.abstractions :only [print-eval-symbol]]))

(defn swap-pairs
[a-seq]
(into (empty a-seq)
(interleave (take-nth 2 (drop 1 a-seq))
(take-nth 2 a-seq))))

(defn do-swap-pairs
[]
(println (str " "
'(swap-pairs (apply list (range 10)))
" = "
(swap-pairs (apply list (range 10)))))
(println (str " "
'(swap-pairs (apply vector (range 10)))
" = "
(swap-pairs (apply vector (range 10))))))

(defn map-map
[f m]
(into (empty m)
(for [[k v] m]
[k (f v)])))

(defn do-map-map
[]
(println (str " "
'(map-map inc (hash-map :z 5 :c 6 :a 0))
" = "
(map-map inc (hash-map :z 5 :c 6 :a 0))))
(println (str " "
'(map-map inc (sorted-map :z 5 :c 6 :a 0))
" = "
(map-map inc (sorted-map :z 5 :c 6 :a 0)))))

(defn do-count
[]
(let [v [1 2 3]
m {:a 1 :b 2}
s #{1 2 3 4}
l '(1)]
(print-eval-symbol (count v))
(print-eval-symbol (count m))
(print-eval-symbol (count s))
(print-eval-symbol (count l))))

(defn do-all
[]
(println)
(println " Swap pairs")
(do-swap-pairs)
(println)
(println " Map-map")
(do-map-map)
(println)
(println " Count is pretty boring")
(do-count))
6 changes: 4 additions & 2 deletions ch-03/src/ch_03/core.clj
@@ -1,10 +1,12 @@
(ns ch-03.core
(require [ch-03.intro :as intro]
[ch-03.abstractions :as abstractions])
[ch-03.abstractions :as abstractions]
[ch-03.collection :as collection])
(:gen-class))


(defn -main
[& args]
(intro/do-all)
(abstractions/do-all))
(abstractions/do-all)
(collection/do-all))

0 comments on commit 2584f8c

Please sign in to comment.