looks like leiningen 1.1 no longer includes current dir in classpath
This commit is contained in:
10
src/koans/about_conditionals.clj
Normal file
10
src/koans/about_conditionals.clj
Normal file
@@ -0,0 +1,10 @@
|
||||
(meditations
|
||||
"Your will face many decisions"
|
||||
(= __ (if (false? (= 4 5)) :a :b))
|
||||
|
||||
"Some of them leave you no alternative"
|
||||
(= __ (if (> 4 3) []))
|
||||
|
||||
"And in such a case you may have nothing"
|
||||
(= nil (if (nil? __) [:a :b :c])))
|
||||
|
12
src/koans/about_equalities.clj
Normal file
12
src/koans/about_equalities.clj
Normal file
@@ -0,0 +1,12 @@
|
||||
(meditations
|
||||
"We shall contemplate truth by testing reality, via equality."
|
||||
(= true false)
|
||||
|
||||
"To understand reality, we must compare our expectations against reality."
|
||||
(= 0 (+ 1 1))
|
||||
|
||||
"Sometimes we will ask you to fill in the values"
|
||||
(= __ (+ 1 1))
|
||||
|
||||
"You can test equality of many things"
|
||||
(= (+ 3 4) __ (+ 2 _)))
|
21
src/koans/about_functions.clj
Normal file
21
src/koans/about_functions.clj
Normal file
@@ -0,0 +1,21 @@
|
||||
(defn count-items [& items]
|
||||
(count items))
|
||||
|
||||
(meditations
|
||||
"Functions are often defined before they are used"
|
||||
(= __ (count-items :a :b :c :d))
|
||||
|
||||
"But they can also be defined inline"
|
||||
(= __ ((fn [n] (Math/pow n 10)) 2))
|
||||
|
||||
"Or using even shorter syntax"
|
||||
(= __ (#(Math/pow % 10)))
|
||||
|
||||
"Functions can beget others"
|
||||
(= __ (((fn []
|
||||
(fn [a b] (+ a b))))
|
||||
4 5))
|
||||
|
||||
"Higher-order functions take function arguments"
|
||||
(= 25 ((fn [f] (f 5))
|
||||
(fn [n] (__ __ __)))))
|
32
src/koans/about_higher_order_functions.clj
Normal file
32
src/koans/about_higher_order_functions.clj
Normal file
@@ -0,0 +1,32 @@
|
||||
(meditations
|
||||
"The map function relates a sequence to another"
|
||||
(= [__ __ __] (map (fn [x] (* 4 x)) [1 2 3]))
|
||||
|
||||
"You may create that mapping"
|
||||
(= [1 4 9 16 25] (map (fn [x] __) [1 2 3 4 5]))
|
||||
|
||||
"Or use the names of existing functions"
|
||||
(= __ (map nil? [:a :b nil :c :d]))
|
||||
|
||||
"A filter can be strong"
|
||||
(= __ (filter (fn [x] false) '(:anything :goes :here)))
|
||||
|
||||
"Or very weak"
|
||||
(= '(:anything :goes :here) (filter (fn [x] __) '(:anything :goes :here)))
|
||||
|
||||
"Or somewhere in between"
|
||||
(= [10 20 30] (filter (fn [x] __) [10 20 30 40 50 60 70 80]))
|
||||
|
||||
"Maps and filters may be combined"
|
||||
(= [10 20 30] (map (fn [x] __) (filter (fn [x] __) [1 2 3 4 5 6 7 8])))
|
||||
|
||||
"Reducing can increase the result"
|
||||
(= __ (reduce (fn [a b] (* a b)) [1 2 3 4]))
|
||||
|
||||
"You can start somewhere else"
|
||||
(= 2400 (reduce (fn [a b] (* a b)) __ [1 2 3 4]))
|
||||
|
||||
"Numbers are not the only things one can reduce"
|
||||
(= "longest" (reduce (fn [a b]
|
||||
(if (< __ __) b a))
|
||||
["which" "word" "is" "longest"])))
|
34
src/koans/about_lists.clj
Normal file
34
src/koans/about_lists.clj
Normal file
@@ -0,0 +1,34 @@
|
||||
(meditations
|
||||
"Lists can be expressed by function or a quoted form"
|
||||
(= '(__) (list 1 2 3 4 5))
|
||||
|
||||
"They are Clojure seqs (sequences), so they allow access to the first"
|
||||
(= __ (first '(1 2 3 4 5)))
|
||||
|
||||
"As well as the rest"
|
||||
(= __ (rest '(1 2 3 4 5)))
|
||||
|
||||
"The rest when nothing is left is empty"
|
||||
(= __ (rest '(100)))
|
||||
|
||||
"And construction by adding an element to the front is simple"
|
||||
(= __ (cons :a '(:b :c :d :e)))
|
||||
|
||||
"Conjoining an element to a list can be done in the reverse order"
|
||||
(= __ (conj '(:a :b :c :d :e) 0))
|
||||
|
||||
"You can use a list like a stack to get the first element"
|
||||
(= __ (peek '(:a :b :c :d :e)))
|
||||
|
||||
"Or the others"
|
||||
(= __ (pop '(:a :b :c :d :e)))
|
||||
|
||||
"But watch out if you try to pop nothing"
|
||||
(= __ (try
|
||||
(pop '())
|
||||
(catch IllegalStateException e "No dice!")))
|
||||
|
||||
"The rest of nothing isn't so strict"
|
||||
(= __ (try
|
||||
(rest '())
|
||||
(catch IllegalStateException e "No dice!"))))
|
48
src/koans/about_maps.clj
Normal file
48
src/koans/about_maps.clj
Normal file
@@ -0,0 +1,48 @@
|
||||
(meditations
|
||||
"Maps in clojure associate keys with values"
|
||||
(= __ (.size (hash-map)))
|
||||
|
||||
"There are two ways to create maps"
|
||||
(= {} (hash-map))
|
||||
|
||||
"A value must be supplied for each key"
|
||||
(= {:a 1} (hash-map :a __))
|
||||
|
||||
"The size is the number of entries"
|
||||
(= __ (.size {:a 1 :b 2}))
|
||||
|
||||
"You can look up the value for a given key"
|
||||
(= __ (get {:a 1 :b 2} :b))
|
||||
|
||||
"Maps can be used as lookup functions"
|
||||
(= __ ({:a 1 :b 2} :a))
|
||||
|
||||
"And so can keywords"
|
||||
(= __ (:a {:a 1 :b 2}))
|
||||
|
||||
"But map keys need not be keywords"
|
||||
(= __ ({2006 "Torino" 2010 "Vancouver" 2014 "Sochi"} 2010))
|
||||
|
||||
"You may not be able to find an entry for a key"
|
||||
(= nil (get {:a 1 :b 2} :c))
|
||||
|
||||
"You can find out if a key is present"
|
||||
(= __ (contains? {:a nil :b nil} :b))
|
||||
|
||||
"Or if it is missing"
|
||||
(= __ (contains? {:a nil :b nil} :c))
|
||||
|
||||
"Maps are immutable, but you can create a new, 'changed' version"
|
||||
(= {1 "January" 2 __} (assoc {1 "January" 2 "Febuary"} 2 "February"))
|
||||
|
||||
"You can also 'remove' an entry"
|
||||
(= {__ __} (dissoc {1 "January" 2 "February"} 2))
|
||||
|
||||
"Often you will need to get the keys (which will be in hash order)"
|
||||
(= (list __ __ __)
|
||||
(sort (keys {2006 "Torino" 2010 "Vancouver" 2014 "Sochi"})))
|
||||
|
||||
"Or the values"
|
||||
(= (list "Sochi" "Torino" __)
|
||||
(sort (vals {2006 "Torino" 2010 "Vancouver" 2014 "Sochi"}))))
|
||||
|
35
src/koans/about_runtime_polymorphism.clj
Normal file
35
src/koans/about_runtime_polymorphism.clj
Normal file
@@ -0,0 +1,35 @@
|
||||
(defn hello
|
||||
([] "Hello World!")
|
||||
([a] (str "Hello, you silly " a "."))
|
||||
([a & more] (str "Hello to this group: "
|
||||
(apply str
|
||||
(interpose ", " (concat (list a) more)))
|
||||
"!")))
|
||||
|
||||
(defmulti diet (fn [x] (:eater x)))
|
||||
(defmethod diet :herbivore [a] __)
|
||||
(defmethod diet :carnivore [a] __)
|
||||
(defmethod diet :default [a] __)
|
||||
|
||||
(meditations
|
||||
"Some functions can be used in different ways - with no arguments"
|
||||
(= __ (hello))
|
||||
|
||||
"With one argument"
|
||||
(= __ (hello "world"))
|
||||
|
||||
"Or with many arguments"
|
||||
(= __
|
||||
(hello "Peter" "Paul" "Mary"))
|
||||
|
||||
"Multimethods allow more complex dispatching"
|
||||
(= "Bambi eats veggies."
|
||||
(diet {:species "deer" :name "Bambi" :age 1 :eater :herbivore}))
|
||||
|
||||
"Different methods are used depending on the dispatch function result"
|
||||
(= "Simba eats animals."
|
||||
(diet {:species "lion" :name "Simba" :age 1 :eater :carnivore}))
|
||||
|
||||
"You may use a default method when no others match"
|
||||
(= "I don't know what Rich Hickey eats."
|
||||
(diet {:name "Rich Hickey"})))
|
32
src/koans/about_sequence_comprehensions.clj
Normal file
32
src/koans/about_sequence_comprehensions.clj
Normal file
@@ -0,0 +1,32 @@
|
||||
(meditations
|
||||
"Sequence comprehensions can bind each element in turn to a symbol"
|
||||
(= __
|
||||
(for [index (range 6)]
|
||||
index))
|
||||
|
||||
"They can easily emulate mapping"
|
||||
(= '(0 1 4 9 16 25)
|
||||
(map (fn [index] (* index index))
|
||||
(range 6))
|
||||
(for [index (range 6)]
|
||||
__))
|
||||
|
||||
"And also filtering"
|
||||
(= '(1 3 5 7 9)
|
||||
(filter odd? (range 10))
|
||||
(for [index __ :when (odd? index)]
|
||||
index))
|
||||
|
||||
"And they trivially allow combinations of the two transformations"
|
||||
(= '(1 9 25 49 81)
|
||||
(map (fn [index] (* index index))
|
||||
(filter odd? (range 10)))
|
||||
(for [index (range 10) :when __]
|
||||
__))
|
||||
|
||||
"More complex transformations can be formed with multiple binding forms"
|
||||
(= [[:top :left] [:top :middle] [:top :right]
|
||||
[:middle :left] [:middle :middle] [:middle :right]
|
||||
[:bottom :left] [:bottom :middle] [:bottom :right]]
|
||||
(for [row [:top :middle :bottom] column [:left :middle :right]]
|
||||
__)))
|
16
src/koans/about_sets.clj
Normal file
16
src/koans/about_sets.clj
Normal file
@@ -0,0 +1,16 @@
|
||||
(meditations
|
||||
"Sets are another important data structure in clojure"
|
||||
(= #{} (set nil))
|
||||
|
||||
"Remember that a set is a 'set'"
|
||||
(= __ (set '(1 1 2 2 3 3 4 4 5 5)))
|
||||
|
||||
"You can ask clojure for the union of two sets"
|
||||
(= __ (clojure.set/union #{1 2 3 4} #{2 3 5}))
|
||||
|
||||
"And also the intersection"
|
||||
(= __ (clojure.set/intersection #{1 2 3 4} #{2 3 5}))
|
||||
|
||||
"But don't forget about the difference"
|
||||
(= __ (clojure.set/difference #{1 2 3 4 5} #{2 3 5})))
|
||||
|
27
src/koans/about_vectors.clj
Normal file
27
src/koans/about_vectors.clj
Normal file
@@ -0,0 +1,27 @@
|
||||
(meditations
|
||||
"You can use vectors in clojure to create an 'Array' like structure"
|
||||
(= __ (.size (vec nil)))
|
||||
|
||||
"You can create a vector in two ways"
|
||||
(= [] (vec nil))
|
||||
|
||||
"And populate it in either of these ways"
|
||||
(= [1] (vec '(1)))
|
||||
|
||||
"But you can populate it with any number of elements at once"
|
||||
(= [1 __] (vec '(1 2)))
|
||||
|
||||
"And add to it as well"
|
||||
(= __ (conj (vec nil) 333))
|
||||
|
||||
"You can get the first element of a vector like so"
|
||||
(= :peanut (first [:peanut :butter :and :jelly]))
|
||||
|
||||
"And the last in a similar fashion"
|
||||
(= __ (last [:peanut :butter :and :jelly]))
|
||||
|
||||
"Or any index if you wish"
|
||||
(= __ (nth [:peanut :butter :and :jelly] 3))
|
||||
|
||||
"You can also slice a vector"
|
||||
(= __ (subvec [:peanut :butter :and :jelly] 1 3)))
|
Reference in New Issue
Block a user